Skip to main content

Posts

Showing posts from June, 2011

Generate the following Pyramids . 1 2 2 3 3 3 4 4 4 4

PROCEDURE:- 1.enter the number of rows n 2.set i to 1 3.repeat steps 4 to 8 until i<=n 4.set j to 1 5.repeat steps 6 and 7 until j<=i 6.print the value of j 7.increment j by 1 8. go to new line ,increment i by 1   CODE:- # include<stdio.h> void main() { int i,j,n; printf(“enter value for n”); scanf(“%d”,&n); for(i=1;i<=n;i++)   {    for(j=1;j<=i; j++)      {       printf(“ %3d”,j);       }     printf(“\n”);   }   } Input:- enter value for n 3 Output:- 1 2 3  3  

Checking whether a number is Palindrome.

PROCEDURE:- 1.input an integer value n 2.set sum to 0 3.repeat steps 4 to 6 until n is greater than 0 4. find n modulo 10 and assign it to r 5.set sum=sum*10+r 6.new value of n is equal to n/10 7.if n and sum values are equal then print n is palindrome 8.else print n is not palindrome CODE:- #include<stdio.h> void main() { int n,r,s=0,m; printf(“enter value for n”); scanf(“%d”,&n); m=n; while(n>0) {     r=n%10;     s=s*10+r;     n=n/10; } if(s= =m) printf(“ %d is a palindrome”,m); else printf(“%d is not a palindrome”,m); } Input:- enter value for n  121 Output:- 121 is a palindrome

Evaluate the cos(x) series(x-x3/3!+x5/5!-……..)

PROCEDURE:- input values for degrees x and the number of terms to be added n convert the given degrees to radians substitute in the series to find the sum print the sum CODE:- #include<stdio.h> void main() { int i=1,n; float sum,t,x,y; printf(“enter the number of terms and degrees”) scanf(“%d %f”,&n,&y); x=y*3.142/180; sum=x; t=x; while(i<n) { t=((-t)*x*x)/((2*i)*(2*i+1)); sum=sum+t; i++; } printf(“ cos(%f)=%f”,y,sum); } Input:- enter the number of terms and degrees 10 60 Output:- cos(60)=0.499999

Evaluate the sin(x) series(1-x2/2!+x4/4!-….

PROCEDURE:- 1.input values for degrees x and the number of terms to be added n 2.convert the given degrees to radians 3.substitute in the series to find the sum 4.print the sum   CODE:- #include<stdio.h> void main() { int i=1,n,y; float sum,t,x; printf(“enter the number of terms and degrees”) scanf(“%d %d”,&n,&y); x=y*3.142/180; sum=1; t=1; while(i<n) {       t=((-t)*x*x)/((2*i)*(2*i-1));        sum=sum+t;        i++; } printf(“ sin(%d)=%f”,y,sum); } Input:- enter the number of terms and degrees 10 30 Output:-sin(30)=0.49999

Sum of the series to 25 terms 1+1/4+1/9+.....

PROCEDURE:- 1.set sum to 0 and set i to 1 2.repeat step 3 until i less than or equal 25 incrementing i by 1 3.set sum=sum+1/square(i) 4.print sum   CODE:- #include<stdio.h> void main() { int i;   float sum=0; for(i=1;i<=25;i++) sum=sum+(1.0/(i * i)); printf(“ sum of the series is = %f”,sum); }| Output:- sum of the series is =1.605724  

Generate twin primes below 100.( (3,5), (5,7), ……………).

PROCEDURE:- 1.set n to 2 2.check the first prime 3.check the next prime by incrementing the n value 4. check the diff. of second prime to first prime (they are twin prime if the difference is 2) 6.repeat the process in steps 2 to  4 so as to generate all twin primes below 100   CODE:- #include<stdio.h> void main( ) {   /* n value is 2 because we are checking 1-100 twin primes, 1 is not prime,        b can have any value initially,here it is 2, later on b value will be a value of  a*/   int i,b=2,n=2,a,ctr;  while(n<100)  {     ctr=0;   i=2;      while(i<n)   /* logic for to check non prime numbers */      {        if(n%i = =0)         {      ctr=1; break;         }             i++;     }    if(ctr= =0)     {       a=n;           /* assining the value of n to a */      if((a-b)= = 2)         {           printf(“( %d , %d) \n”,b,a);          }       b=a;  /* assining the value of a to b( it will assign only the prime no’s (i.e) the value of  a ) */    

Check whether a given number is Fibonacci prime or not.

PROCEDURE:- 1.input an integer n 2. first check whether it is prime or not 3. if it prime check whether it is in the Fibonacci series 4.the above check can be made by generating series and comparing each number with n 5.if n is found in the series then n is Fibonacci prime 6.else it is not Fibonacci prime   CODE:- #include<stdio.h> void main(){ int n,i=2,k=0,a,b,i,m; printf(“enter the number”); scanf(“%d”,&n); while(i<=n/2) { if(n%i = = 0)   {     k=1; break;   } i++; } if(k!=1) { a=0; b=1; i=1; m=n; while(i<=m) {   c=a+b;    if(m= =c)     {      printf(“ %d is Fibonacci prime”,n);    break;     } a=b; b=c; i++; } if(m!=c) printf(“%d  Not Fibonacci prime”,n); } else printf(“not prime “); } Input:- enter the number  13 Output:- 13 is Fibonacci prime  

Generate Fibonacci series below 100( 0 ,1, 1, 2 ,3 ,5,8………..)

PROCEDURE:- 1.set i to 3, set a=0,b=1, 2. repeat steps 3 to 5 until i is less than or equal 100 incrementing i by 1 each time 3. compute c=a+b 4.print value of c 5. set a=b,b=c   CODE:- #include<stdio.h> void main() { int i=3, a=0,b=1 ,c; printf(“Fibonacci series below 100 are :\n”); printf(“%3d %3d”,a,b); while(i<=100)     {        c=a+b;        printf(“%3d”,c);        a=b;        b=c;        i++;      } } Output:- Fibonacci series below 100 are              0 1 1 2 3 5 8 13………………

Generate all perfect nos. below 100.

PROCEDURE:-        1.set n=1        2.repeat steps 3 to 6 until n reaches 100 incrementing n by 1 each time         3.set sum to 0        4.start finding modulo value for  n with i=1 to n-1 value incrementing i by 1 each time        5.if at any point remainder is 0 add i to sum        6.if sum is same as n then  print n CODE:- #include<stdio.h> void main() { int n ,i,s; printf(“the perfect numbers below 100 are \n”); for(n=1;n<=100;n++) { s=0; for(i=1;i<n;i++) { if (n % i = = 0)    s=s+i; } if (n = = s) printf(“%3d “,n); } } Output:-6,28

Checking whether a number is Fibonacci.

PROCEDURE:- 1.enter an integer value 2. set a=0,b=1,c=a+b,flag=0 3.repeat steps 4 ,5 until c is less than or equal to n 4.if n and c are same print that n is Fibonacci number 5.else set a=b,b=c, c=a+b 6 if n is not equal to c it is not Fibonacci number   CODE:- #include<stdio.h> void main() { int n, a=0,b=1 ,c,flag=0; printf(“enter a number”); scanf(“%d”,&n); do {     c=a+b;     if(n= = c)       {         flag=1;         break;        }      a=b;      b=c;   }while(c<=n); if(flag = = 1) printf(“%d is Fibonacci number”,n); else printf(“%d is not Fibonacci number”,n); } Input:- enter a number  8 Output:- 8 is Fibonacci number

Checking whether a number is perfect.

PROCEDURE:-        1.input an interger value n        2. set sum to 0        2.start finding modulo value for n with i=1 to n-1 value incrementing i by 1 each time        3.if at any point remainder is 0 add i to sum        4.if sum is same as the original input n then n is perfect number        5.else n is not perfect number CODE:- #include<stdio.h> void main() { int n ,i,s=0 printf(“enter a number “); scanf(“%d”,&n); for(i=1;i<n;i++)   {     if (n % i = = 0)    s=s+i;    } if (n = = s) printf(“the given number %d is perfect number “,n); else printf(“the given number %d is not a perfect number “,n); } Input:- enter a number  6 Output:- the given number 6 is perfect number

Checking whether a number is Prime.

PROCEDURE:-        1.input an interger value n        2.start  finding modulo value for  n with i=2 to n-1 value incrementing i by 1 each time        3.if at  any point remainder becomes 0        4.print that number is not prime        5.else print that the number is prime CODE:- #include<stdio.h> void main() { int n , i,flag =0; printf(“enter a number “); scanf(“%d”,&n); for(i=2;i<n;i++) { if (n % i = = 0)   {    flag=1;    break;    } } if (flag = = 0) printf(“the given number %d is prime number “,n); else printf(“the given number %d is not a prime number “,n); } Input:- enter a number  5 Output:- the given number 5 is prime number

Generate all primes below 100.

PROCEDURE:-                   1.set n=1        2. repeat steps 3 to 6 until n reaches 100        3.start finding modulo value for  n with i=2 to n-1 value incrementing i by 1 each time        4.if at  any point remainder becomes 0        5.don’t print that n        5.else print n        6.increment  n by 1 CODE:- #include<stdio.h> void main() { int  i,flag,n ; printf(“primes below 100 are\n”); for(n=1;n<=100;n++) {    flag=0;    for(i=2;i<n;i++)       {           if (n % i = = 0)             {              flag=1;             break;              }         }     if (flag = = 0)     printf(“%3d “,n);   } } Output:-1,2,3,5,7,11,13,17,19, 23,………………97

Find G.C.D.(Greatest Common Divisor) of two numbers.

PROCEDURE:-        1.input two interger values a and b        2.if a is greater than b        3.set n=b        4.else set n=a        5.repeat steps  6 and 9 until n is greater than 0        6 find a modulo n and b modulo n, if modulo value is 0        7. print gcd value as n        8.stop        9else decrement n by 1 CODE:- #include<stdio.h> void main() { int a,b,n; printf(“enter two numbers for a,b to find GCD”); scanf(“%d %d “,&a,&b); if(a>b)        n=b; else        n=a; do {        if(a%n = = 0 && b%n = = 0)        {    printf(“ GCD for %d and %d is  %d”,a,b,n);    break;        }        else        n=n-1;   }while(n>0); } Input:- enter two numbers for  a ,b to find GCD  16 24 Output:- GCD for 16 and 24 is  8

Number system conversion.(Decimal to Binary)

PROCEDURE:-        1.enter integer value n set sum to 0 and f to 1        2.repeat  step 3 to 6 until n is greater than 0        3. find the modulo 2 for n and store result in r        4.set sum to sum+r*f        5.set f to f*10        6. find new value of n by dividing it by 2        7. print sum CODE:- #include<stdio.h> void main() {   long int n,m,s=0,r,f=1;   printf(“enter number in decimal system \n”);   scanf(“%ld”,&n);   m=n;   While (n>0)   {     r=n%2;     s=s+r*f;     f=f*10;     n=n/2;   } printf(“the binary value for the decimal number %ld  is %ld”,m,s); } Input:- enter number in decimal system 7 Output:- the binary value for the decimal number 7  is 111

Find factorial of a number.

PROCEDURE:-        1.input an integer value n        2.set fact to 1 and i to 1        3.repeat step 4,5 until i less than or equal to n        4.set fact to fact*i        5.increment i by 1        6.print fact CODE:- #include<stdio.h> void main() { long int n, fact=1; int i; printf(“enter value for n”); scanf(“%ld”,&n); for(i=1;i<=n;i++)        fact=fact *i ; printf(“ factorial for the number %ld is %ld”,n,fact); } Input:- enter value for n 5 Output:- factorial for the number 5 is 120

Sum of digits of a number down to a single digit.

PROCEDURE:-        1.enter integer value n        2.repeat  step 3 to 8 until n is greater than 9        3.set sum to 0        4.repeat  step 5 to 7 until n is greater than        5. find the modulo 10 for n and store result in r        6.add r to previous sum        7. find new value of n by dividing it by 10        8 set  n to sum        9. print sum CODE:- #include<stdio.h> void main() {   int n,m,sum,r;   printf(“enter value for n”);   scanf(“%d”,&n);   m=n;   while(n>9)   {     sum=0;     while(n>0)      {        r=n%10;        sum=sum+r;        n=n/10;      }    n=sum; } printf(“sum of %d down to single digit is %d”,m,sum); } Input:- enter value for n 137 Output:- sum of 137 down to single digit is 2  

Reversing a number

PROCEDURE:-        1.enter integer value n        2.repeat  step 3 to 5 until n is greater than 0        3. find the modulo 10 for n and store result in r        4.print r        5. find new value of n by dividing it by 10 CODE:- #include<stdio.h> void main() {   int n,r;   printf(“enter value for n”);   scanf(“%d”,&n);   while(n>0)   {     r=n%10;     printf(“%d”,r);     n=n/10;   } } Input:- enter value for n 124 Output:- 421

Find Sum of even and odd in a series of numbers.

PROCEDURE:-        1. enter an integer value n, set oddsum and evensum to 0        2. repeat step 3 and 4 until  all the n different values are read        3.read an integer find modulo 2 of that if result is 0 add this to previous evensum        4.if result of modulo is 1 add the integer to previous oddsum        5.print the oddsum and evensum CODE:- #include<stdio.h> void main() {   int n,x,i=1;sume=0,sumo=0;   printf(“enter value for n”);   scanf(“%d”,&n);   printf(“enter %d different values”,n);   while(i<=n)   {     scanf(“%d”,&x);     if(x%2= =0)       sume=sume+x;     else       sumo=sumo+x;     i++   } printf(“Even sum =%d \n Odd sum = %d”, sume,sumo); } Input:- enter value for n 4                enter 4 different values  10 11 12 13 Output:- Even sum =22  Odd sum =24  

Sum and average of n different numbers.

PROCEDURE:- input an integer value n and set sum to 0 repeat step 3 until  all the n different values are read read an integer add this to previous sum print the sum CODE:- #include<stdio.h> void main() {   int n,x,i=1;sum=0;   printf(“enter value for n”);   scanf(“%d”,&n);   printf(“enter %d different values”,n);   while(i<=n)   {     scanf(“%d”,&x);     sum=sum+x;     i++;   }  printf(“sum of %d numbers is  %d”, n, sum); } Input:- enter value for n 4               enter 4 different values   20 20 20 20 Output:- sum of 4 numbers is 80

Generate the electricity bill based on the specifications: Domestic >100 units Rs 1.50 101 – 300 units Rs 2.00 301 – 500 units Rs 2.50 L.T.Consumer 501 – 1000 units Rs 4.00 H.T.Consumer 1001- 2000 units Rs 5.00

PROCEDURE:-        1.input no. of units consumed        2.according to the above specifications find the amount to be paid        3.print the above result CODE:- #include<stdio.h> void main() {   int units;   float price;   printf(“enter the number of units “);   scanf(“%d”,&units);   if(units<100)   price=units*1.50;   else if(units>100&&units<=300)   price=units*2.00;   else if(units>300&&units<=500)   price=units*2.50;   else if (units>500&&units<=1000)   price=units*4.00;   else if(units>1000&&units<=2000)   price=units*5.00;   printf(“the total bill is %f”,price); } Input:- enter the number of units              320 Output:- the total bill is 800.00

Write a C program to read a number and to print the number in words.(EX:=384=three eight four)in switch case.

PROCEDURE:-        1.input an integer        2.reverse the given integer        3. repeat steps 4 to 6 reversed integer is greater than 0        4. find modulo 10 value        5. print the result of step 4 in words        6. find new value of reversed number by dividing by 10. CODE:- #include<stdio.h> void main() {   int a,rev=0,n;   printf(“enter a number”);   scanf(“%d”,&a);   while(a>0)    {      n=a%10;      rev=rev*10+n;      a=a/10;     } while(rev>0) {   n=rev%10;   switch(n)    {        case 1: printf(“one”);  break;    case 2: printf(“two”);  break;        case 3: printf(“three”); break;  case 4: printf(“four”); break;        case 5: printf(“five”);  break;       case 6: printf(“six”);   break;        case 7: printf(“seven”);break;       case 8: printf(“eight”); break;        case 9: printf(“nine”);  break;       case 0: printf(“zero”);   break; }   rev=rev/10; } } Input:- Enter a number   123 Output:- one two three

Find greatest of 3 numbers.(Using conditional operator)

PROCEDURE:- 1.input values for a,b,and c. 2.check whether a is greater than b and c 3.if so a will be greatest 4.if not c will be greatest 5.if a is not greater than b and c 6.then check whether b is greater than c 7.if yes b is greatest 8.if no c is greatest   CODE:- #include<stdio.h> void main() {   int a,b,c,y;   printf(“enter values for a,b,c”);   scanf(“%d %d %d “,&a,&b,&c);   y=(a>b)?(((a>c)?a:c) : ((b>c)?b:c))   printf(“%d is the greatest “,y); } Input:- enter values for a,b,c                a=6, b=8, c=7 Output:- b =8 is geatest

Greatest of 3 numbers.( Nested IF-ELSE)

PROCEDURE:- input values for a,b,and c. check whether a is greater than b and c if so a will be greatest if not c will be greatest if a is not greater than b and c then check whether b is greater than c if yes b is greatest if no c is greatest CODE:- #include<stdio.h> void main() { int a,b,c; printf (“enter values for a,b,c”); scanf(“%d %d %d”,&a,&b.&c); if(a>b)      {         if(a>c)            printf(“ %d is the greatest “,a);         else            printf(“%d is the greatest”,c);         }  else {            if(b>c)             printf(“%d is the greatest”,b);           else              printf(“%d is the greatest”,c);          } } Input:- enter values for a,b,c                a=5, b=6, c=7 Output:- 7 is geatest

To convert Upper case character to lower case and vice-versa.

PROCEDURE:-        1.input a character        2.if the given character is small case convert it to upper case        3.else if the given character is upper case convert it to lower case        4. after conversion print the character CODE:- #include<stdio.h> #include<ctype.h> void main() { char c; printf(“enter a character”);  c=getchar();  if (islower(c)) putchar(toupper(c));  else putchar(tolower(c)); } Input:- enter a character                 a Output:- A Input:- enter a character                A Output:- a

Swap Two numbers (without using temporary variable)

PROCEDURE:-        1.input values for a and b        2. compute a= a+b        3. compute b =a-b        4. compute a=a-b        5.print value of a and b after swapping CODE:- #include<stdio.h> void main() {   int a,b,t;   printf(“enter value for a,b”);   scanf(“%d %d”,&a,&b);   printf(“before swapping  a=%d   b=%d”,a,b);   a=a+b;   b=a-b;   a=a-b;   printf(“after swapping  a=%d    b=%d”,a,b); } Input:- enter value for a,b                 a=5,   b=6 Output:- after swapping  a=6   b=5

Swap two numbers.( using temporary variable).

PROCEDURE:-        1.input values for a and b        2. store value of a in t        3. store value of b in a        4. store value of t in b        5.print value of a and b after swapping CODE:- #include<stdio.h> void main() {   int a,b,t;   printf(“enter value for a,b”);   scanf(“%d %d”,&a,&b);   printf(“before swapping  a=%d   b=%d”,a,b);   t=a;   a=b;   b=t;   printf(“after swapping  a=%d    b=%d”,a,b); } Input:- enter value for a,b                 a=5,   b=6 Output:- after swapping  a=6   b=5

Find Sum to first n numbers (using goto).

PROCEDURE:- input value for n set initial values for sum as 0 and I as 1 add I to sum and increment I by 1 if I is less than or equal to n goto step 3 print the sum CODE:- #include<stdio.h> void main() {   int n, i=1, sum=0;   printf(“enter value for n”);   scanf(“%d”,&n);   abc:        sum=sum+i;        i=i+1;        if (i<=n) goto abc;    printf(“sum upto %d numbers is %d”,n,sum); } Input:- enter value for n                  n=5 Output:- sum upto 5 numbers is 15

Find Value of S=ut+1/2*a*t**2.

PROCEDURE:-        1.enter values for u,a,t to find distance        2.find distance with the formulae ut+1/2at 2        3.print the above result CODE:- #include<stdio.h> #include<conio.h> void main() {   float u,t,a,S;   clrscr();   printf(“enter values u,t,a”);   scanf(“%f %f %f”, &u,&t,&a);   S=(u*t)+(0.5*a*t*t);   printf(“\n  S = %f”, S); } Input:- enter values u,t,a               U=10,t=4,a=4.9 Output:- S =79.200

Calculate Simple Interest

PROCEDURE:- input values for principal amount, time,rate of interest find simple interest with the formulae (p*t*r)/100 print the above result CODE:- #include<stdio.h> #include<conio.h> void main() {   int p,t,r;   float si;   printf(“enter values p,t,r”);   scanf(“%d %d %d”, &p,&t,&r);   si=(p*t*r)/100.0;   printf(“\n  Simple interest is = Rs .%f”, si); } Input: - enter values p,t,r               P=10000, t=10, r=3 Output:- Simple interest is =Rs.3000

Convert Celcious temperature to Farenheit.

PROCEDURE:- input value for celcious find farenheit value with the formulae (9/5)c+32 print the above result CODE:- #include<stdio.h> void main() { float c,f; printf(“enter a value in celcious\n”); scanf(“%f”,&c); f=(9.0/5)*c+32; printf(“temperature in farenheit is %f”,f); } Input:- enter a value in celcious                35 Output:- temperature in farenheit is 95.0000

To find area of triangle

PROCEDURE:-        1.Input values for three sides of triangle        2.find s value with the formulae (a+b+c)/2        3.find area with the formulae sqrt(s(s-a)(s-b)(s-c))        4.print the above result CODE:- #include<stdio.h> #include<conio.h> #include<math.h> void main() {   int a,b,c;   float s, area;   printf(“enter values for three sides of triangle”);   scanf(“%d %d %d”, &a,&b,&c);   s=(a+b+c)/2.0;   area=sqrt(s*(s-a)*(s-b)*(s-c)) ;   printf(“\n area of triangle with sides a=%d, b=%d, c=%d  is  %f”, a,b,c,area); } Input :- enter values for three sides of triangle             a=2, b=2, c=2 Output:- area of triangle with sides a=2, b=2, c=2  is  1.732

To find volume of a cylinder: l*b*h

PROCEDURE:-        1: input values for length,breadth,height        2: find the product (length*breadth*height)        3:print the above result CODE:- #include<stdio.h> #include<conio.h> void main() {      int l,b,h, vol;      printf(“enter values for length, breadth,height of cylinder”);      scanf(“%d %d %d”, &l,&b,&h);      vol=l*b*h;      printf(“\n volume of cylinder is %d”, vol); } Input:- enter values for length, breadth,height of cylinder        A = 5, B=5, C=5   Ouput:- volume of cylinder is 125