C Programs

Basic C Programs pdf with Solution

#include<stdio.h>

void main()

{

inta,b,c;

printf("\n Enter The 1st Number:");

scanf("%d",&a);

printf("\n Enter The 2nd Number:");

scanf("%d",&b);

    c=a+b;

printf("\n Sum:%d+%d=%d",a,b,c);

}
INPUT A NUMBER AND CHECK THE NUMBER IS ODD OR EVEN:
#include<stdio.h>

void main()

{

                int a;

                printf("Enter a number:");

                scanf("%d",&a);

                if(a%2==0)

                {

                                printf("%d is an even number",a);

                }

                else

                {

                                printf("%d is an odd number",a);

                }

}
NESTED IF-ELSE PROGRAM(GRADATION SYSTEM):
#include<stdio.h>

void main()

{

int t, n;

float p;

printf("please input the marks obtained and number of subjects:");

scanf("%d %d",&t,&n);

p=t/n;

printf("parcentage is %f\n",p);

if (p>=80)

{

printf("first division");

}

else if(p>=60)

{

printf("second division");

}

else if(p>=40)

{

printf("third division");

}

else if(p>=30)

{

printf("pass");

}

else if(p<=29)

{

printf("fail");

}

}
FIND THE ROOTS OF A BINOMIAL EQUATION:
#include<stdio.h>

#include<math.h>

int main()

{

  float a,b,c;

  float d,root1,root2; 

  printf("Enter a, b and c of quadratic equation: ");

  scanf("%f%f%f",&a,&b,&c);

  d = b * b - 4 * a * c;

  if(d < 0)

  {

    printf("Roots are complex number.\n");

  }

  else if(d==0){

   printf("Both roots are equal.\n");



   root1 = -b /(2* a);

   printf("Root of quadratic equation is: %.3f ",root1);

   return 0;

  }

  else

  {

   root1 = ( -b + sqrt(d)) / (2* a);

   root2 = ( -b - sqrt(d)) / (2* a);

   printf("Roots of quadratic equation are: %f ,%f",root1,root2);

  }

  return 0;

}
LARGEST AMONG THREE NUMBERS:
#include<stdio.h>

void main()

{

int a,b,c;

printf("Enter three numbers");

scanf("%d %d %d",&a,&b,&c);

if(a>b)

{

if(a>c)

{

printf("%d is largest",a);

}

else

{

printf("%d is largest",c);

}

}

else

{

if(b>c)

{

printf("%d is largest",b);

}

else

{

printf("%d is largest",c);

}

}
SUM OF 1+2+3+. . .+n:
#include<stdio.h>

void main()

{

                int s=0,n,i;

                printf("Enter the n th term:");

                scanf("%d",&n);

                for(i=1;i<=n;i++)

                {

                                s=s+i;

                }

                printf("s=%d",s);

}
SUM OF 1+3+5+. . .+n:
#include<stdio.h>

#include<conio.h>

int main()

{

    int n,i,j,s;

    printf("\n Enter how many terms:");

    scanf("%d",&n);

    s=0;

    for(i=1,j=1;j<=n;i=i+2,j++)

    {

                     s=s+i;

    }

    printf("\n Sum of the Series is=%d",s);

    getch();

    return 0;

}
FACTORIAL OF A NUMBER:
#include<stdio.h>

#include<conio.h>

int main()

{

    int i,n,f,a;

    printf("\n Enter a Number:\n");

    scanf("%d",&n);

    f=1;

    for(i=1;i<=n;i++)

     {

                     f=f*i;

     }

     printf("Factorial=%d",f);

     getch();

     return 0;

}
PRINT THE FOLLOWING PATTERN :
                 **********
                 **** ****
                 ***    ***
                 **      **
                 *       *
#include<stdio.h>

void main()

{

                int i,j,k,l,m,n,p,x=1;

                printf("please enter the nth term");

                scanf("%d",&n);

               

                for(i=1;i<=2*n-1;i++)

                {

                                printf("*");

                }

               

                printf("\n");

                m=n-1;

               

                for(j=1;j<=m;j++)

                {

                                for(k=j;k<=m;k++)

                                {

                                                printf("*");

                                }

                                for(l=1;l<=x;l++)

                                {

                                                printf(" ");

                                }

                                x=x+2;

                                for(p=j;p<=m;p++)

                                {

                                                printf("*");

                                }

                                printf("\n");

                }

}
CHECK A NUMBER IS PRIME OR NOT:
#include<stdio.h>

#include<conio.h>

int main()

{

    int f,i,n;

    printf("\n Please Enter the Number: ");

    scanf("%d",&n);

    f=0;

    for(i=2;i<=n/2;i++)

    {

                       if(n%i==0)

                       {

                                 f=1; break;

                       }

    }

                       if(f==0)

                               printf("\n %d is a Prime Number",n);

                       else

                               printf("\n %d is Not a Prime Number",n);

    getch();

    return 0;

}
FIBONACCI SERIES:
#include<stdio.h>

#include<conio.h>

void main()

{

    int i,n,t1,t2,t3;

    printf("\n Enter the n th Term:");

    scanf("%d",&n);

    t1=0;

    t2=1;

    if(n==1)

            printf("%d",t1);

    else if(n==2)

            printf("%d %d",t1,t2);

    else

        {

            printf("%d %d",t1,t2);

            for(i=3;i<=n;i++)

            {



                             t3=t1+t2;

                             t1=t2;

                             t2=t3;

                             printf("\t %d",t3);

            }

        }

        getch();

}
PRINT THE PRIME NUMBERS UP TO n:
#include<stdio.h>

int main()

{

   int n, i = 3, count, c;

   printf("Enter the number of prime numbers required\n");

   scanf("%d",&n);

   if ( n >= 1 )

   {

      printf("First %d prime numbers are :\n",n);

      printf("2\n");

   }

   for ( count = 2 ; count <= n ;  )

   {

      for ( c = 2 ; c <= i - 1 ; c++ )

      {

         if ( i%c == 0 )

            break;

      }

      if ( c == i )

      {

         printf("%d\n",i);

         count++;

      }

      i++;

   }

   return 0;

}
LINEAR SEARCH:
#include <stdio.h>

#include<conio.h>

void main()

{

   int array[100],s,c,n;

   printf("Enter the number of elements in array\n");

   scanf("%d",&n);

   printf("Enter %d integerS\n", n);

   for (c = 0; c < n; c++)

   scanf("%d", &array[c]);

   printf("Enter the number to search\n");

   scanf("%d", &s);

   for (c = 0; c < n; c++)

   {

      if (array[c] == s)        

      {

         printf("Search successful for %d in %d position.\n", s, c+1);

         break;

      }

   }

   if (c == n)

    printf("%d is not present in array.\n", s);

    getch();

}
DECIMAL TO BINARY CONVERSION:
#include<stdio.h>

void main()

{

    int d,quotient;



    int binaryNumber[100],i=1,j;

    printf("Enter any decimal number: ");

    scanf("%d",&d);

    quotient = d;

    while(quotient!=0)

                                {

         binaryNumber[i++]= quotient % 2;

         quotient = quotient / 2;

    }

    printf("Binary value of decimal number %d: ",d);

    for(j = i -1 ;j> 0;j--)

    printf("%d",binaryNumber[j]);

}
CHECK A NUMBER IS ARMSTRONG OR NOT:
#include<stdio.h>

#include<conio.h>

void main()

{

int x,m,r,s=0;

printf("Enter a number");

scanf("%d",&x);

m=x;

while (x!=0)

{

                                r=x%10;

                                s=s+r*r*r;

                                x=x/10;

}

if (s==m)

{

                                printf("The number is amstronge");

}

else

{

                                printf("The number is not amstronge");

}

getch();

}
BUBBLE SORT:
#include <stdio.h>

#include<conio.h>

int main()

{

  int array[100], n, c, d, swap;



  printf("Enter number of elements\n");

  scanf("%d", &n);



  printf("Enter %d integers\n", n);



  for (c = 0; c < n; c++)

    scanf("%d", &array[c]);



  for (c = 0 ; c < ( n - 1 ); c++)

  {

    for (d = 0 ; d < n - c - 1; d++)

    {

      if (array[d] > array[d+1])

      {

        swap       = array[d];

        array[d]   = array[d+1];

        array[d+1] = swap;

      }

    }

  }



  printf("Sorted list in ascending order:\n");



  for ( c = 0 ; c < n ; c++ )

     printf("%d\n", array[c]);

  return 0;

  getch();

}
REVERSE THE ARRAY:
#include<stdio.h>

#include<conio.h>

int main()

{

    int x[50],i,n;

    printf("\n Enter the Number of terms:");

    scanf("%d",&n);

    printf("\n Enter the Numbers:");

    for(i=0;i<n;i++)

    {

                    scanf("%d",&x[i]);

    }

    printf("\n The Arrey is=");

    for(i=n-1;i>=0;i--)

    {

                    printf("%d",x[i]);

    }

    getch();

    return 0;

}
MATRIX ADDITION:
#include<stdio.h>

void main()

{

                                int i,j,a[2][2],b[2][2],c[2][2];

                            printf("Enter the elements of the first matrix one by one:");

                                for(i=0;i<=1;i++)

                                {

                                                for(j=0;j<=1;j++)

                                                {

                                                                scanf("%d",&a[i][j]);

                                                }

                                }

                                printf("The first matrix is:\n");

                                for(i=0;i<=1;i++)

                                {

                                                for(j=0;j<=1;j++)

                                                {

                                                                printf("%d\t",a[i][j]);

                                                }

                                                printf("\n");

                                }

                       printf("Enter the elements of the second matrix one by one:");

                                for(i=0;i<=1;i++)

                                {

                                                for(j=0;j<=1;j++)

                                                {

                                                                scanf("%d",&b[i][j]);

                                                }

                                }

                                printf("The second matrix is:\n");

                                for(i=0;i<=1;i++)

                                {

                                                for(j=0;j<=1;j++)

                                                {

                                                                printf("%d\t",b[i][j]);

                                                }

                                                printf("\n");

                                }

                                for(i=0;i<=1;i++)

                                {

                                                for(j=0;j<=1;j++)

                                                {

                                                                c[i][j]=a[i][j]+b[i][j];

                                                }

                                }

                                                printf("Sum of two matrix is\n");

                                                for(i=0;i<=1;i++)

                                {

                                                for(j=0;j<=1;j++)

                                                {

                                                                printf("%d\t",c[i][j]);

                                                }

                                                printf("\n");

                                }

}
MATRIX MULTIPLICATION:
#include <stdio.h>

int main()

{

  int m, n, p, q, c, d, k, sum = 0;

  int first[10][10], second[10][10], multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");

  scanf("%d%d", &m, &n);

  printf("Enter the elements of first matrix\n");

  for (c = 0; c < m; c++)

    for (d = 0; d < n; d++)

      scanf("%d", &first[c][d]);

  printf("Enter the number of rows and columns of second matrix\n");

  scanf("%d%d", &p, &q);

  if (n != p)

    printf("Matrices with entered orders can't be multiplied with each other.\n");

  else

  {

    printf("Enter the elements of second matrix\n");

    for (c = 0; c < p; c++)

      for (d = 0; d < q; d++)

        scanf("%d", &second[c][d]);

    for (c = 0; c < m; c++)

 {

      for (d = 0; d < q; d++)

{

        for (k = 0; k < p; k++)

 {

          sum = sum + first[c][k]*second[k][d];

        }

        multiply[c][d] = sum;

        sum = 0;

      }

    }

    printf("Product of entered matrices:-\n");



    for (c = 0; c < m; c++) {

      for (d = 0; d < q; d++)

        printf("%d\t", multiply[c][d]);

      printf("\n");

    }

  }

  return 0;

}
CALL BY VALUE:
#include<stdio.h>

void main()

{

                                int a=30,b=35;

                                swap(a,b);

                                printf("a=%d,b=%d",a,b);

}

                                void swap(int x,int y)

                                {

                                                int temp;

                                                temp=x;

                                                x=y;

                                                y=temp;

                                }

}
CALL BY ADDRESS:
#include<stdio.h>

void main()

{

                                int a=30,b=35;

                                swap(&a,&b);

                                printf("a=%d,b=%d",a,b);

}

                                void swap(int *x,int *y)

                                {

                                                int temp;

                                                temp=*x;

                                                *x=*y;

                                                *y=temp;

                                }

}
COUNT THE NUMBER OF VOWEL, CONSONANT AND SPACES IN THE STRING:
#include <stdio.h>

int main()

{

    char line[150];

    int i, vowels, consonants, spaces;

    vowels = consonants = spaces = 0;

    printf("Enter a line of string: ");

    scanf("%[^\n]", line);

    for(i=0; line[i]!='\0'; ++i)

    {

        if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||

           line[i]=='o' || line[i]=='u' || line[i]=='A' ||

           line[i]=='E' || line[i]=='I' || line[i]=='O' ||

           line[i]=='U')

        {

            ++vowels;

        }

        else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))

        {

            ++consonants;

        }

        else if (line[i]==' ')

        {

            ++spaces;

        }

    }

    printf("Vowels: %d",vowels);

    printf("\nConsonants: %d",consonants);

printf("\nSpaces: %d", spaces);

    return 0;

}

5
Leave a Reply

avatar
3 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
Jewel HermenauEarnest DillenburgTanmoyRitam Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Ritam
Guest
Ritam

Thanks a lot. It really helped me. Keep updating the content like this.

Earnest Dillenburg
Guest

I think this web site has some really wonderful information for everyone : D.

Jewel Hermenau
Guest

Simply a smiling visitant here to share the love (:, btw outstanding layout.