Skip to main content

Posts

Showing posts with the label c

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++; ...

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)             {       ...

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)        {   ...

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 b...

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...

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 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