C program examples with output


C frequently asked program examples and output
1
Write a c program which takes password from user.
Code
#include<stdio.h>
#define MAX 500

int main(){

    char password[MAX];
    char p;
    int i=0;

    printf("Enter the password:");
   
    while((p=getch())!= 13){
         password[i++] = p;
         printf("*");
    }

    password[i] = '\0';

    if(i<6)
         printf("\nWeak password");

    printf("\nYou have entered: %s",password);

    return 0;
}

Sample output:
Enter the password: *******
You have entered: fgt67m,
2
Write a simple code for linear search in c programming language.
Code
#include<stdio.h>
int main(){

    int a[10],i,n,m,c=0;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements of the array: ");
    for(i=0;i<=n-1;i++){
         scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);
    for(i=0;i<=n-1;i++){
         if(a[i]==m){
             c=1;
             break;
         }
    }
    if(c==0)
         printf("The number is not in the list");
    else
         printf("The number is found");

    return 0;
}

Sample output:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found
3

Bubble sort using c program.

Code
#include<stdio.h>
int main(){

  int s,temp,i,j,a[20];

  printf("Enter total numbers of elements: ");
  scanf("%d",&s);

  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);

  //Bubble sorting algorithm
  for(i=s-2;i>=0;i--){
      for(j=0;j<=i;j++){
           if(a[j]>a[j+1]){
               temp=a[j];
              a[j]=a[j+1];
              a[j+1]=temp;
           }
      }
  }

  printf("After sorting: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);

  return 0;
}

Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9
After sorting:  0 2 6 9 11
4

Write a c program to find out the sum of series 1 + 2 + …. + n.

Code
#include<stdio.h>
int main(){
    int n,i;
    int sum=0;

    printf("Enter the n i.e. max values of series: ");
    scanf("%d",&n);

    sum = (n * (n + 1)) / 2;

    printf("Sum of the series: ");

    for(i =1;i <= n;i++){
         if (i!=n)
             printf("%d + ",i);
         else
             printf("%d = %d ",i,sum);
         }
    return 0;
}

Sample output:
Enter the n i.e. max values of series: 5
Sum of the series: 1 + 2 + 3 + 4 + 5 = 15

Mathematical Formula:
Sum of the series 1 + 2 + 3 + … + n = n (n+1)/2
5
C program to reverse a number using recursion.
Code
#include<stdio.h>
int main(){
    int num,reverse;

    printf("Enter any number: ");
    scanf("%d",&num);

    reverse=rev(num);
    printf("Reverse of number: %d",reverse);
    return 0;
}

int rev(int num){
    static sum,r;

    if(num){
         r=num%10;
         sum=sum*10+r;
         rev(num/10);
    }
    else
         return 0;

    return sum;
}

Sample output:
Enter any number: 456
Reverse of number: 654

6
C program to reverse a number using for loop.
Code
#include<stdio.h>
int main(){
    int num,r,reverse=0;

    printf("Enter any number: ");
    scanf("%d",&num);

    for(;num!=0;num=num/10){
         r=num%10;
         reverse=reverse*10+r;
    }

    printf("Reversed of number: %d",reverse);
    return 0;
}

Sample output:
Enter any number: 123
Reversed of number: 321
7
Program for find out the sum of digits in c using recursion.
Code
#include<stdio.h>

int getSum(int);
int main(){
  int num,sum;
  printf("Enter a number: ");
  scanf("%d",&num);

  sum = getSum(num);

  printf("Sum of digits of number:  %d",sum);
  return 0;
}

int getSum(int num){

    static int sum =0,r;

    if(num!=0){
      r=num%10;
      sum=sum+r;
      getSum(num/10);
    }

    return sum;
}

Sample output:
Enter a number: 45
Sum of digits of number:  9
8
Sum of digits of a number in c using for loop.
Code
#include<stdio.h>
int main(){
  int num,sum=0,r;
  printf("Enter a number: ");
  scanf("%d",&num);

  for(;num!=0;num=num/10){
      r=num%10;
      sum=sum+r;
  }
  printf("Sum of digits of number:  %d",sum);
  return 0;
}

Sample output:
Enter a number: 567
Sum of digits of number:  18
9
Count the digits of a given number in c language using recursion.
Code
#include<stdio.h>

int countDigits(num);
int main(){
  int num,count;

  printf("Enter a number: ");
  scanf("%d",&num);

  count = countDigits(num);

  printf("Total digits is:  %d",count);
  return 0;
}

int countDigits(int num){
    static int count=0;

     if(num!=0){
          count++;
         countDigits(num/10);
    }

    return count;
}

Sample output:
Enter a number: 1234567
Total digits is:  7
10
C code to count the total number of digit using for loop.
Code
#include<stdio.h>
int main(){
  int num,count=0;

  printf("Enter a number: ");
  scanf("%d",&num);

  for(;num!=0;num=num/10)
      count++;

  printf("Total digits is:  %d",count);

  return 0;
}

Sample output:
Enter a number: 456
Total digits is:  3
11
How do you write a program which produces its own source code as its output in c language?
Code
#include<stdio.h>

int main(){
    FILE *fp;
    char c;

    fp = fopen(__FILE__,"r");

    do{
         c= getc(fp);
         putchar(c);
    }
    while(c!=EOF);

    fclose(fp);
   
    return 0;
}

Output:
#include<stdio.h>

int main(){
    FILE *fp;
    char c;

    fp = fopen(__FILE__,"r");

    do{
         c= getc(fp);
         putchar(c);
    }
    while(c!=EOF);

    fclose(fp);
   
    return 0;
}
12
Multiplication tables by c program.
Code
#include<stdio.h>
int main(){
  int r,i,j,k;
  printf("Enter the number range: ");
  scanf("%d",&r);
  for(i=1;i<=r;i++){
      for(j=1;j<=10;j++)
           printf("%d*%d=%d ",i,j,i*j);
      printf("\n");
  }
  return 0;
}

Sample Output:
Enter the number range: 5

1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10

2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 4*10=40
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50
13

Program in c to print 1 to 100 without using loop.

Code
#include<stdio.h>

int main(){
    int num = 1;

    print(num);

    return 0;
}
int print(num){
    if(num<=100){
         printf("%d ",num);
         print(num+1);
    }
}

Sample output:
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
14

C program to convert decimal to roman number.

Code
#include<stdio.h>

void predigits(char c1,char c2);
void postdigits(char c,int n);

char roman_Number[1000];
int i=0;

int main(){

    int j;
    long int number;
   
    printf("Enter any natural number: ");
    scanf("%d",&number);
   
    if(number <= 0){
         printf("Invalid number");
         return 0;
    }

    while(number != 0){

         if(number >= 1000){
             postdigits('M',number/1000);
             number = number - (number/1000) * 1000;
         }
         else if(number >=500){
             if(number < (500 + 4 * 100)){
                 postdigits('D',number/500);
                 number = number - (number/500) * 500;
             }
             else{
                 predigits('C','M');
                 number = number - (1000-100);
             }
         }
         else if(number >=100){
             if(number < (100 + 3 * 100)){
                 postdigits('C',number/100);
                 number = number - (number/100) * 100;
             }
             else{
                 predigits('L','D');
                 number = number - (500-100);
             }
         }
         else if(number >=50){
             if(number < (50 + 4 * 10)){
                 postdigits('L',number/50);
                 number = number - (number/50) * 50;
             }
             else{
                 predigits('X','C');
                 number = number - (100-10);
             }
         }
         else if(number >=10){
             if(number < (10 + 3 * 10)){
                 postdigits('X',number/10);
                 number = number - (number/10) * 10;
             }
             else{
                 predigits('X','L');
                 number = number - (50-10);
             }
         }
         else if(number >=5){
             if(number < (5 + 4 * 1)){
                 postdigits('V',number/5);
                 number = number - (number/5) * 5;
             }
             else{
                 predigits('I','X');
                 number = number - (10-1);
             }
         }
         else if(number >=1){
             if(number < 4){
                 postdigits('I',number/1);
                 number = number - (number/1) * 1;
             }
             else{
                 predigits('I','V');
                 number = number - (5-1);
             }
         }
    }

    printf("Roman number will be: ");
    for(j=0;j<i;j++)
         printf("%c",roman_Number[j]);

    return 0;

}

void predigits(char c1,char c2){
    roman_Number[i++] = c1;
    roman_Number[i++] = c2;
}

void postdigits(char c,int n){
    int j;
    for(j=0;j<n;j++)
         roman_Number[i++] = c;
   
}

Sample output:
Enter any natural number: 87
Roman number will be: LXXXVII
15

C program for addition of binary numbers

Code
#include<stdio.h>

int main(){

    long int binary1,binary2;
    int i=0,remainder = 0,sum[20];

    printf("Enter any first binary number: ");
    scanf("%ld",&binary1);
    printf("Enter any second binary number: ");
    scanf("%ld",&binary2);

    while(binary1!=0||binary2!=0){
         sum[i++] =  (binary1 %10 + binary2 %10 + remainder ) % 2;
         remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
         binary1 = binary1/10;
         binary2 = binary2/10;
    }

    if(remainder!=0)
         sum[i++] = remainder;

    --i;
    printf("Sum of two binary numbers: ");
    while(i>=0)
         printf("%d",sum[i--]);

   return 0;
}

Sample output:
Enter any first binary number: 1100011
Enter any second binary number: 1101
Sum of two binary numbers: 1110000
16
C program c program to convert binary to octal.
Code

#include<stdio.h>
int main(){
   
    long int binaryNumber,octalNumber=0,j=1,remainder;

    printf("Enter any number any binary number: ");
    scanf("%ld",&binaryNumber);

    while(binaryNumber!=0){
         remainder=binaryNumber%10;
        octalNumber=octalNumber+remainder*j;
        j=j*2;
        binaryNumber=binaryNumber/10;
    }

    printf("Equivalent octal value: %lo",octalNumber);

    return 0;
}

Sample output:
Enter any number any binary number: 1101
Equivalent hexadecimal value: 15
17
C program to extract last two digits of a given year and print it.
Code
#include<stdio.h>
int main(){
  int yyyy,yy;

  printf("Enter a year in for digits: ");
  scanf("%d",&yyyy);

  yy = yyyy % 100;
  printf("Last two digits of year is: %02d",yy);

  return 0;
}

Sample Output:
Enter a year in for digits: 1985
Last two digits of year is: 85
18
Code to find the ASCII values of given character in c.
Code
#include<stdio.h>

int main(){
  
    char c;

    printf("Enter any character: ");
    scanf("%c",&c);

    printf("ASCII value of given character: %d",c);
        
    return 0;
}

Sample output:
Enter any character: a
ASCII value of given character: 97
19

Write a scanf function in c which accept paragraph from user.

Code
#include<stdio.h>
#define MAX 500

int main(){

    char arr[MAX];

    printf("Enter any paragraph which can include spaces or new line.\n");
    printf("To exit press the tab key.\n");
    scanf("%[^\t]s",arr);

    printf("You had entered: \n");
    printf("%s",arr);

    return 0;
}

Sample output:
Enter any paragraph which can include spaces or new line.
To exit, press the tab key.
C is powerful language.
I am learning c from
cquestionbank.blogspot.com

You had entered:
C is powerful language.
I am learning c from
cquestionbank.blogspot.com
20

Write a c program to find the area of a triangle.

Code


Formula of area of any triangle:

Area = √(s*(s-a)*(s-b)*(s-c))
Where s = (a + b + c)/2

#include<stdio.h>
#include<math.h>
int main(){
    float a,b,c;
    float s,area;
   
    printf("Enter size of each sides of triangle");
    scanf("%f%f%f",&a,&b,&c);
   
    s = (a+b+c)/2;
    area = sqrt(s*(s-a)*(s-b)*(s-c));
   
    printf("Area of triangle is: %.3f",area);
    return 0;
}

Sample output:
Enter size of each sides of the triangle: 2 4 5
Area of triangle is: 3.800

5 comments:

suresh said...

thanks

Pure Thoughts said...

Its great, thanks a lot.

Pure Thoughts said...

Its great, thanks a lot.

Unknown said...

Superb

guna said...

use this program

#include
#include
#define MAX 500

int main(){

char password[MAX];
char p;
int i=0,c;

printf("Enter the password:");
for( int j=0;j<13;j++)
{
scanf("%c",&password[j]);

}
c=strlen(password);
while(i<c)
{

printf("*");
i++;
}
if(c<6)
printf("\nWeak password");
else
printf("strong");

printf("\nYou have entered: %s",password);

return 0;
}