echo '' ;
C Programming Language

C – Examples

Working on C programming examples offers numerous benefits for both beginners and experienced programmers. Here are some advantages of using C examples:

working with C examples is an effective way to learn, practice, and reinforce programming skills. Whether you’re a beginner or an experienced programmer, examples serve as valuable resources for mastering C and building a solid foundation for further exploration in the field of computer programming.

C Quiz

C Quiz-1
C Quiz-2
C Quiz-3
C Quiz-4
C Quiz-5
C Quiz-6

C program examples

Here you can find many c program examples from Git

Examples
C Examples – Hello World
C Examples – Addition
C Example – Division: C Program to Compute Quotient and Remainder
C Examples – Time Delay
C Program to Find the Size of int, float, double, and char: Data Type-Size of int, float, double and char.c
C Program to Demonstrate the Working of Keyword long: Keyword-long
Odd or Even:
-> Odd or Even Using Conditional Operator..c
-> Odd or Even.c
C Program to Check Whether a Character is Vowel or Consonant: Vowel or consonant.c
C Program to Find GCD of two Numbers Greatest Common Divisor
-> GCD_Using_call_by_value.c
-> GCD_Using_while_loop.c
-> GCD_Using-while_loop_and_if_else.c
C Program to Check Whether a Number is Prime or Not 
-> Prime_Number_Checker.c
-> Prime_numbers_Lister_from_2_to_100.c
C Program to Check Whether a Number is Palindrome or Not:
-> Reverse_Number_and_Palindrome_Checker.c
C Program to read excel and store in File: File Example_Read Excel Data.c
C Program to Display its own Source Code as Output: Produces its own source code as its output.c
What is the output of the following program (Structure Understanding)
C Tutorial -Examples
-> Example_2.1.c
-> Example_2.2.c
-> Example_2.3.c
C Program for Array: Char_Array.c
C Program Swap Numbers
Shell Sort.c
Smallest and Largest

Example Codes List-1

Please send the mail for the below example code.

  • C Program to Make a Simple Calculator Using switch…case
  • C Program to Display Prime Numbers Between Intervals Using Function
  • C Program to Check Prime or Armstrong Number Using User-defined Function
  • C Program to Check Whether a Number can be Expressed as a Sum of Two Prime Numbers
  • C Program to Find the Sum of Natural Numbers using Recursion
  • C Program to Find G.C.D Using Recursion
  • C program to Reverse a Sentence Using Recursion
  • C program to calculate the power using recursion
  • C Program to Calculate Average Using Arrays
  • C Program to Find Largest Element of an Array
  • C Program to Calculate Standard Deviation
  • C Program to Add Two Matrix Using Multi-dimensional Arrays
  • C Program to Multiply to Matrix Using Multi-dimensional Arrays
  • C Program to Find Transpose of a Matrix
  • C Program to Multiply two Matrices by Passing Matrix to a Function
  • C Program to Access Elements of an Array Using Pointer
  • C Program to Find Largest Number Using Dynamic Memory Allocation
  • C Program to Find the Frequency of Characters in a String
  • C program to count the number of vowels, consonants, and so on
  • C Program to Remove all Characters in a String Except Alphabet
  • C Program to Find the Length of a String
  • C Program to Concatenate Two Strings
  • C Program to Copy String Without Using strcpy()
  • C Program to Sort Elements in Lexicographical Order (Dictionary Order)
  • C Program to Store Information of a Student Using Structure

Example Codes List-2

Please send the mail for the below example code.

  • C Program to Add Two Distances (in inch-feet) System Using Structures
  • C Program to Add Two Complex Numbers by Passing Structure to a Function
  • C Program to Calculate Difference Between Two Time Periods
  • C Program to Store Information of Students Using Structure
  • C Program to Store Information Using Structures with Dynamically Memory Allocation
  • C Program to Write a Sentence to a File
  • C Program to Read a Line From a File and Display it
  • C program to Find Mean and Standard Deviation in Array Element
  • C Program to Find all Roots of a Quadratic Equation
  • C Program to Check Leap Year
  • C Program to Check Whether a Number is Positive or Negative
  • C Program to Check Whether a Character is an Alphabet or not
  • C Program to Calculate the Sum of Natural Numbers
  • C Program to Find LCM of two Numbers
  • C Program to Display Characters from A to Z Using Loop
  • C Program to Count Number of Digits in an Integer
  • C Program to Reverse a Number
  • C Program to Calculate the Power of a Number
  • C Program to Generate Multiplication Table
  • C Programming Code To Create Pyramid and Pattern
  • Day Finder (Two dates difference)
  • C Program to Display Factors of a Number
  • C Program to Display Prime Numbers Between Two Intervals


Other Example Codes

Count set bits

C Program to count set bits in an integer

  • C Program to Count the Number of Bits set to One using Bit-wise Operations
  • Write an efficient program to count the number of 1s in the binary representation of an integer

Code

#include <stdio.h>     

int main()
{
    int count,i = 9;
    while (i)
    {
        count += i & 1;
        i >>= 1;
    }
    printf(" Total number of 1st in a given number is : %d", count);
    return 0;
}

Output:

Total number of 1st in a given number is : 2

Print numbers from 1 to N

Print numbers from 1 to N without the help of loops

#include <stdio.h>
#define N 10
int main()
{
    static unsigned int AEW=1;
    if(AEW<=N)
    {
        printf("%d\n",AEW++);
        main();
    }
    return 0;
}

Floyd’s Triangle

//Floyds_Triangle
#include<stdio.h>
#include<conio.h>
void main()
{
	int i,j,a,k=1;
	printf("enter the no of lines" );
	scanf("%d",&a);
	for(i=1;i<=a;i++)
	{
		for(j=1;j<=i;j++)
		{
			printf("%d\t",k);
			k++;
		}
		printf("\n");
	}
	getch();

}

/* Output
enter the no of lines
10
1
2       3
4       5       6
7       8       9       10
11      12      13      14      15
16      17      18      19      20      21
22      23      24      25      26      27      28
29      30      31      32      33      34      35      36
37      38      39      40      41      42      43      44      45
46      47      48      49      50      51      52      53      54      55
*/


Pascal triangle

Code:

#include<stdio.h> 
long fact(int); 
int main()
{ 
   int line,i,j; 
    printf("Enter the no. of lines: "); 
    scanf("%d",&line); 
    for(i=0;i<line;i++){ 
         for(j=0;j<line-i-1;j++) 
             printf(" "); 
         for(j=0;j<=i;j++) 
             printf("%ld ",fact(i)/(fact(j)*fact(i-j))); 
         printf("\n"); 
    } 
    return 0; 
} 
 
long fact(int num)
{ 
    long f=1; 
    int i=1; 
    while(i<=num){ 
         f=f*i; 
         i++; 
  } 
  return f; 
 }

Output:

Enter the no. of lines: 8 
        1 
      1 1 
     1 2 1 
    1 3 3 1 
   1 4 6 4 1 
  1 5 10 10 5 1 
 1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 


Armstrong Number

C Program to Check Armstrong Number

Definition of Armstrong number or what is an Armstrong number: 

  • Definition according to c programming point of view: Those numbers which sum of the cube of its digits is equal to that number are known as Armstrong numbers. For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153 . Other Armstrong numbers: 370,371,407 etc. 
  • In general definition:  Those numbers which sum of its digits to power of number of its digits is equal to that number are known as Armstrong numbers.  

Example 1: 153  

Total digits in 153 is 3  
And 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Example 2: 1634  

Total digits in 1634 is 4 
And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634

Examples of Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725 

Code 1 :

  1. Warp to check a number is Armstrong
  2. C program to check whether a number is Armstrong or not
  3. Simple c program for Armstrong number
  4. Armstrong number in c with output
#include<stdio.h>
#include<math.h>
 
int main()
{
    int n,m=0,p=0,x,y;
    printf("Enter any number: ");
    scanf("%d",&n);
    y=n;
    while(y!=0){
        y=y/10;
        p++;
    }
    y=n;
    while(n!=0)
    {
        x=n%10;
        m+=pow(x,p);
        n=n/10;
    }
    if(y==m)
        printf("The given number is an armstrong number");
    else
        printf("The given number is not an armstrong number");
 
    return 0;
}

/* Output

Enter any number: 371
The given number is an armstrong number
*/

while loop (Code 2)

#include<stdio.h> 
int main()
{ 
    int num,r,sum=0,temp; 
    printf("Enter a number: "); 
    scanf("%d",&num); 
    temp=num; 
    while(num!=0){ 
         r=num%10; 
         num=num/10; 
         sum=sum+(r*r*r); 
    } 
    if(sum==temp) 
         printf("%d is an Armstrong number",temp); 
    else 
         printf("%d is not an Armstrong number",temp); 
     return 0; 
}  
/* Sample output: 
Enter a number: 153 
153 is an Armstrong number 
*/

Armstrong number in c using for loop (Code 3)

#include<stdio.h> 
int main(){ 
    int num,r,sum=0,temp; 
    printf("Enter a number: "); 
    scanf("%d",&num); 
    for(temp=num;num!=0;num=num/10){ 
        r=num%10; 
        sum=sum+(r*r*r); 
    } 
    if(sum==temp) 
         printf("%d is an Armstrong number",temp); 
    else 
         printf("%d is not an Armstrong number",temp); 
    return 0; 
} 
 /*
Sample output: 
Enter a number: 370 
370 is an Armstrong number 
Logic of Armstrong number in c 
*/

C Program to Display Armstrong Number Between Two Intervals

Code 1 : C program for Armstrong number generation

#include<stdio.h> 
int main()
{ 
    int num,r,sum,temp; 
    int min,max; 
    printf("Enter the minimum range: "); 
    scanf("%d",&min); 
    printf("Enter the maximum range: "); 
    scanf("%d",&max); 
    printf("Armstrong numbers in given range are: "); 
    for(num=min;num<=max;num++)
  { 
         temp=num; 
         sum = 0; 
         while(temp!=0)
   { 
             r=temp%10; 
             temp=temp/10; 
             sum=sum+(r*r*r); 
         }
         if(sum==num) 
          printf("%d ",num); 
    }
    return 0; 
}
/*
Sample output: 
Enter the minimum range: 1 
Enter the maximum range: 200 
Armstrong numbers in given range are: 1 153 
*/

Code 2 :

  1. C program to print Armstrong numbers from 1 to 500
  2. C program for finding Armstrong numbers
#include<stdio.h> 
int main(){ 
    int num,r,sum,temp; 
    for(num=1;num<=500;num++){ 
         temp=num; 
         sum = 0; 
         while(temp!=0){ 
             r=temp%10; 
             temp=temp/10; 
             sum=sum+(r*r*r); 
         } 
         if(sum==num) 
             printf("%d ",num); 
   } 
    return 0; 
} 
/*
Output: 
1 153 370 371 407 
*/


Pyramid and Structure

C Programming Code To Create Pyramid and Structure

  • Reference: https://cprogrammingcodes.blogspot.in/p/pyramid.html
  • Generate a following @ triangle : GitHub code Triangle_Pyramid_2.c
  • Generate a following #’s triangle : GitHub code Triangle_Pyramid_3.c

Generate a following structure : GitHub code Square_Structure.c

@
@
@
@
@

Generate a following #’s triangle : GitHub code Triangle_Pyramid_1.c

#

###
##
#


Year, Week, Days

Given Number of days into the year, week, days.

  • This below program is compiled in gcc version 4.6.3 at online c compiler repl.it
#include<stdio.h>
 
int main()
{
    int y,w,d,a;
    printf("Enter total number of days:");
    scanf("%d",&d);
 
    y=d/365;
    a=d%365;
    w=a/7;
    d=a%7;
    printf("\nYears=%d\nWeeks=%d\nDays=%d",y,w,d);
    
    return 0;
}

/* Output 
Enter total number of days: 85

Years=0
Weeks=12
Days=1   
*/

Reverse Number

#include<stdio.h>
 
int main()
{
    long n,rev=0,d;
    printf("Enter any number:");
    scanf("%ld",&n);
    
    while(n!=0)
    {
        d=n%10;
        rev=(rev*10)+d;
        n=n/10;
    }
    
    printf("The reversed number is %ld",rev);
 
    return 0;
}
/* Output
Enter any number:16789
The reversed number is 98761
*/

Take Away-1

  • Conceptual Understanding: C examples provide a practical demonstration of theoretical concepts. They help learners visualize how different programming constructs work, reinforcing their understanding.
  • Syntax Familiarity: Examples showcase the syntax of the C language in real-world scenarios. This aids in becoming familiar with the syntax and structure of C code.
  • Problem-Solving Practice: Examples often involve solving specific problems or implementing algorithms. This type of practice enhances problem-solving skills and encourages creative thinking.
  • Quick Learning: Examples offer a quick and concise way to learn specific aspects of C programming. Instead of going through lengthy explanations, learners can directly see how certain code snippets function.
  • Variety of Applications: C is a versatile language used in system programming, embedded systems, game development, and more. Examples cover a wide range of applications, exposing learners to different domains where C is employed.
  • Code Reusability: Learners can reuse and modify example code for their projects. Understanding and adapting existing code is a valuable skill in programming.

Take Away-2

  • Error Analysis: Examples may intentionally include common errors or pitfalls, helping learners recognize and understand these issues. This contributes to developing strong debugging skills.
  • Best Practices: Examples often follow coding best practices. Studying well-structured and well-commented examples teaches learners how to write clean, maintainable, and efficient code.
  • Memory Management Understanding: C examples frequently involve manual memory management using pointers. Working with such examples helps learners understand memory allocation, deallocation, and pointer manipulation.
  • Real-World Relevance: Examples are often based on real-world scenarios, making the learning experience more practical and applicable to actual programming tasks.
  • Community Learning: Sharing and discussing examples with the programming community can provide valuable insights and alternative approaches. It fosters a collaborative learning environment.
  • Portfolio Development: Implementing C examples and building small projects based on them can contribute to the development of a programming portfolio. This is beneficial for showcasing skills to potential employers or collaborators.
  • Preparation for Interviews: Understanding and practicing C examples can be advantageous during technical interviews where problem-solving and coding skills are evaluated.
  • Foundation for Advanced Concepts: Examples often introduce foundational concepts that serve as building blocks for more advanced programming topics. A strong understanding of basic concepts is crucial for tackling complex problems.


Next

C – Basic
C – Operator
C – Decision making, Branching, Looping
C – Functions
C – Storage Class
C – Extern
C – Array
C – Pointer
C – Memory Allocation
C – Structure
C – Union
C – Structure and Union
C – Macros
C – Preprocessor and Macros
C – File Management
C – Coding Standard
C – Compilers
C – Compiling Process
C File Management
C Library
C Example

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.