C programs with solutions

/*————- VARIABLE DECLARATION AND ASSIGNMENT —————*/

 

# include <stdio.h>
# include <conio.h>
void main()
    {
    /*……….DECLARATIONS……………………….*/

    float      x, p ;
    double     y, q ;
    unsigned   k ;

    /*……….DECLARATIONS AND ASSIGNMENTS…………*/

    int        m = 54321 ;
    long int   n = 1234567890 ;

    /*……….ASSIGNMENTS………………………..*/

    x = 1.234567890000 ;
    y = 9.87654321 ;
    k = 54321 ;
    p = q = 1.0 ;

    /*……….PRINTING…………………………..*/

    printf("m = %d\n", m) ;
    printf("n = %ld\n", n) ;
    printf("x = %.12lf\n", x) ;
    printf("x = %f\n", x) ;
    printf("y = %.12lf\n",y) ;
    printf("y = %lf\n", y) ;
    printf("k = %u  p = %f  q = %.12lf\n", k, p, q) ;
    getch();
    }

 

/*————-INVESTMENT PROBLEM USING SCANF—————*/

 

# include <stdio.h>
# include <conio.h>
void main()
  {
      int    year, period ;
      float  amount, inrate, value ;

      printf("Input amount, interest rate, and period\n\n") ;
      scanf ("%f %f %d", &amount, &inrate, &period) ;
      printf("\n") ;
      year = 1 ;

      while( year <= period )
      {
        value = amount + inrate * amount ;
        printf("%2d  Rs %8.2f\n", year, value) ;
        amount = value ;
        year = year + 1 ;

      }
      getch();
  }

 

 
/*—– USING ARITHMETIC OPERATORS—–*/

# include <stdio.h>
# include <conio.h>
void main()
{
int a, b, c, d;

a = 15;
b = 10;
c = ++a – b;

printf(“a = %d  b = %d  c = %d\n”,a, b, c);

d = b++ +a;
printf(“a = %d  b = %d  d = %d\n”,a, b, d);

printf(“a/b = %d\n”, a/b);
printf(“a%%b = %d\n”, a%b);
printf(“a *= b = %d\n”, a*=b);
printf(“%d\n”, (c>d) ? 1 : 0);
printf(“%d\n”, (c<d) ? 1 : 0);
getch();
}

 

 /*—– EVALUATING EXPRESSIONS—–*/

# include <stdio.h>
# include <conio.h>
void main()
   {
       float  a, b, c, x, y, z;

       a = 9;
       b = 12;
       c = 3;

       x = a - b / 3 + c * 2 - 1;
       y = a - b / (3 + c) * (2 - 1);
       z = a -(b / (3 + c) * 2) - 1;

       printf("x = %f\n", x);
       printf("y = %f\n", y);
       printf("z = %f\n", z);
       getch();
   }

 

 

 
/*——————- Sum of n terms of 1/n ——————-*/

# include <stdio.h>
# include <conio.h>
void main()
   {
       float  sum, n, term ;
       int    count = 1 ;

       sum = 0 ;
       printf("Enter value of n\n") ;
    scanf("%f", &n) ;
       term = 1.0/n ;
       while( count <= n )
       {
        sum = sum + term ;
        count++ ;
       }
       printf("Sum = %f\n", sum) ;
       getch();
   }

 

 
/*——————-USING CASTS——————-*/

# include <stdio.h>
# include <conio.h>
void main()
   {                                                           
       float sum;
       int n;

       sum = 0;

       for( n = 1; n <= 10; ++n)
       {
      sum = sum + 1/(float)n ;
      printf("%2d  %6.4f\n", n, sum) ;
       }
       getch();
   }

 

 
/*———- PROGRAM USING IF STATEMENT ——–*/
#include <stdio.h>
#include <conio.h>
void main()
   {
       int count, i;
       float weight, height;
       count = 0;
       printf("Enter weight and height for 10 boys...!!!\n");

       for (i =1; i <= 10; i++)
       {
      scanf("%f %f", &weight, &height);
      if (weight < 50 && height > 170)
         count = count + 1;
       }
       printf("Number of boys with weight < 50 kg\n");
       printf("and height > 170 cm = %d\n", count);
       getch();
   }

 

 

 

Scroll Up