রবিবার, ১২ ডিসেম্বর, ২০১০

Lopping C Programing


1.(i)X and n are input through keyboard. Write a program to compute n!,  nCr, nPr
#include<stdio.h>
#include<conio.h>
long int fact(int n)
{
            int i,f=1;
            for (i=1;i<=n;i++)
                f*=i;
            return (f);
}
void main()
{
            int n,r;
            clrscr();
            aa:
              printf("Please enter a value for n & r(r<=n)");
              scanf("%d %d",&n,&r);
             if(r>n)
             {
               printf("r is greter than n. ");
               goto aa;
             }
            printf("\n %d!  = %d",n,fact(n));
            printf("\n %dP%d = %d",n,r,fact(n)/fact(n-r));
            printf("\n %dC%d = %d ",n,r,fact(n)/(fact(r)*fact(n-r)));
            getch();
}
1.(ii) X and n are input through keyboard. Write a program to compute xn
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
            int n;
            double x,result;
            clrscr();
            printf("Programme for calculation of 'x' to the power 'n' : ");
            printf("\nEnter a value for x : ");
            scanf("%lf",&x);
            printf("\nEnter a intiger value for n : ");
            scanf("%d",&n);
            result = pow(x, n);
            printf("\n%.2lf\ to the power %d is : %.2lf",x,n,result);
            getch();
}

2.Write a program to determine the GCD ( Greatest common divisor) and  LCM (least common multiple) of 3 numbers.
#include<stdio.h>
#include<conio.h>
int lcm(int r,int s);
int gcd(int p,int q);
void main()
{
            int a,b,c,d,e;
            clrscr();
            scanf("%d%d%d",&a,&b,&c);
d=gcd(a,b);
printf(“The gcd is %d”,gcd(c,d));
            e=lcm(a,b);
            printf("The lcm is %d",lcm(c,e));
            getch();
}
int lcm(int r,int s)
{
            int m,v;
            m=gcd(r,s);
            v=(r*s)/m;
            return v;
}
int gcd(int p,int q)
{
            int c;
            do{
            c=p%q;
            p=q;
            q=c;
            }
            while(c!=0);
            return p;
}
3.Find out the sum of each of the following series. n  is the input from user from user for series (iv) to
(i)           3 + 11 + 19 + .. .. + 1691.

#include<stdio.h>
#include<conio.h>
void main()
{
            int i,n;
            long sum = 0;
            clrscr();
            printf("Sum Of Series");
            printf("\n3+11+19+....+1691\n\n");
            for(i=3;i<=1691;i=i+8)
                 {
                 sum = sum+i;
                 }
            printf("sum = %ld",sum);
            getch();
}
}
(ii)7 + 20 + 33 + .. .   ( up to nth term )
#include<stdio.h>
#include<conio.h>
void main()
{
            long int a,n,i,j;
            long sum = 0,term;
            clrscr();
          printf("Sum of serises : ");
printf("\n\7 + 20 + 33 + .. .   ( up to nth term ));
printf("\n\nEnter a value for n : ");
            scanf("%d",&n);
            for(i=0;i<=n;i=i++)
                 {
                        term=5+(i*6);
sum = sum+i;
                 }
            printf("sum = %ld",sum);
            getch();
}

(iii)5 – 11 + 17 -   . . . ( up to   nth term )
#include<stdio.h>
#include<conio.h>
void main()
{
            int a,n,i,j,term;
            long sum = 0,sign =-1;
            clrscr();
            printf("Sum of serises : ");
            printf("\n\n5-11+17-....+(up to nth term) ");
            printf("\n\nEnter a value for n : ");
            scanf("%d",&n);
            for(i=0;i<=n;i=i++)
             {
                         term=(5+i*6)*sign;
sum = sum + term;
              }
            printf("\n\n5-11+17-....+(up to %dth term) = %ld",n,sum);
            getch();
}


(iv)1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + . . . + ( 1 + 2 + 3 + . . .  + n )
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,n;
            long int s = 0,ss = 0;
            clrscr();
            printf("Sum Of Series");
            printf("\n\n1+(1+2)+(1+2+3)+....+(1+2+3+....+n)");
            printf("\n\nEnter a positive value for 'n' : ");
            scanf("%d", &n);
            for(i=1;i<=n;i++)
                 {
                 s  = s+i;
                 ss = ss+s;
                 }
            printf("\nResult : \n\n1+(1+2)+(1+2+3)+....+(1+2+3+....+%d) = %ld",n,ss);
            getch();
}

(v)1 + 22/2! + 32/3! + . . . + n2/n!
#include<stdio.h>
#include<conio.h>
long int fact(int x)
{
            printf("Please enter a float & a intger number : ");
            scanf("%f%d",&a,&b);
            c=prd(a,b);
            printf("Product = %.3f",c);
            getch();
}
float prd(float x,int y)
{
            float d;
            d =x*y;
            return d;
}

 (vi)2 * 7 * 12 * . . .  * 37
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,n;
            long int sum = 1;
            clrscr();
            printf("Sirise : 2*7*12*....*37");
            for(i=2;i<=37;i=i+5)
              {
              sum = i*sum;
              }
            printf("\n\nResult = %ld",sum);
            getch(); 
  }
4. Write a program to determine all prime numbers within the range [a . . . b] where a & b are input through keyboard.
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,j,a,n;
            clrscr();
            printf("Determine of Prime number\n\nEnter the lower and upper range : \n");
            scanf("%d%d",&a,&b);
            printf("\nPrime numbers are (range  :  %d to %d) : \n\n",a,b);
            for(i=a;i<=b;i++)
               {
               for(j=2;j<=i;j++)
               {
               if(i%j==0)
               break;
               }
               if(i==j)
               printf("%4d",i);
            }
            getch();
}
5.  Construct the following table. Here n is input from the user.
          1        2        3        . . .     n
          2        4        6        . . .     2n
          3        6        9        . . .     3n
                .         .         .         . . .     .
          n        2n      3n      . . .     nn
#include<stdio.h>
#include<conio.h>
void main()
{
                int i,j,n;
                clrscr();
                printf("Please enter a value : ");
                scanf("%d",&n);
                for(j=1;j<=n;j++){
                  for(i=1;i<=n;i=i+1)
                  {
                  printf("%4d",i*j);
                }
                  printf("\n");}

       getch();    }
7. Write a program to find out first n Fibonacci number where n is the input from user.
#include<stdio.h>
#include<conio.h>
void main()
{
            long int f0=0,f1=1,f;
            int i,n;
            clrscr();
            printf("First n Fibonacci Number : \nEnter a value for n : ");
            scanf("%d",&n);
            for(i=1;i<=n;i++)
               {
                 f=f0+f1;
                 printf("%5d",f);
                 f1=f0;
                 f0=f;
               }
            getch();
}
8. Write a program to show the following triangle/rectangle of “*” s or numbers. Take n as inpt from user to determine the number of rows of the structure. (eg: n = 5 )
                          *
                   ***
                 *****
      *******
              *********
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,j,n;
            clrscr();
            printf("How many pirimids in your pirimids ? : ");
            scanf("%d",&n);
            for(i=0;i<n;i++)
             {
               for(j=0;j<=i;j++)
                {
                  gotoxy(10+j,10+i);
                  printf("*");
                }
               for(j=0;j<=i;j++)
                {
                  gotoxy(10-j,10+i);
                  printf("*");
                }
               printf("\n");
             }
            getch();
}
                                                              1
                                                121
         12321
       1234321
     123454321
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,j,n;
            clrscr();
            printf("How many pirimids in your pirimids ? : ");
            scanf("%d",&n);
            for(i=1;i<=n;i++)
             {
               for(j=1;j<=(n-i);j++)
                {
                  printf("  ");
                }
               for(j=1;j<=i;j++)
                {
                  printf("%2d",j);
                }
               for(j=(i-1);j>=1;j--)
                {
                  printf("%2d",j);
                }
             printf("\n");
             }
             getch();
}



                                                *****
*****
*****
*****
*****
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,j,n;
            clrscr();
            printf("Enter a int number : ");
            scanf("%d",&n);
            for(j=1;j<=n;j++)
             {
               for(i=1;i<=n;i=i+1)
                {
                  printf("*");
                }
               printf("\n");
             }
            getch();
}
                 *
                        ***
                      *****
             *******
                   *********
            *******
                      *****
                        ***
                 *
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,j,n;
            clrscr();
            printf("How many pirimids in your pirimids ? : ");
            scanf("%d",&n);
            for(i=1;i<=n;i++)
             {
               for(j=1;j<=(n-i);j++)
                {
                  printf(" ");
                }
               for(j=1;j<=i;j++)
                {
                  printf("*");
                }
               for(j=(i-1);j>=1;j--)
                {
                  printf("*");
                }
             printf("\n");
             }
            for(i=(n-1);i>=1;i--)
              {
                for(j=(n-i);j>=1;j--)
                  {
                        printf(" ");
                  }
                for(j=1;j<=i;j++)
                  {
                        printf("*");
                  }
                for(j=(i-1);j>=1;j--)
                  {
                        printf("*");
                  }
                printf("\n");
             }
             getch();
}
                                                                1
                                                121
         12321
       1234321
     123454321
                                                   1234321
                       12321
           121
             1 
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,j,n;
            clrscr();
            printf("How many rows in your pirimids ? : ");
            scanf("%d",&n);
            for(i=1;i<=n;i++)
             {
               for(j=1;j<=(n-i);j++)
                {
                  printf("  ");
                }
               for(j=1;j<=i;j++)
                {
                  printf("%2d",j);
                }
               for(j=(i-1);j>=1;j--)
                {
                  printf("%2d",j);
                }
             printf("\n");
             }
             for(i=(n-1);i>=1;i--)
              {
                for(j=(n-i);j>=1;j--)
                  {
                        printf("  ");
                  }
                for(j=1;j<=i;j++)
                  {
                        printf("%2d",j);
                  }
                for(j=(i-1);j>=1;j--)
                  {
                        printf("%2d",j);
                  }
                printf("\n");
            }
             getch();
}

                                   *********
            *******
                      *****
                        ***
                 *
        ***
                      *****
             *******
                   *********
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,j,n;
            clrscr();
            printf("How many pirimids in your pirimids ? : ");
            scanf("%d",&n);
            for(i=n;i>=1;i--)
             {
               for(j=(n-i);j>=1;j--)
                {
                  printf(" ");
                }
               for(j=1;j<=i;j++)
                {
                  printf("*");
                }
               for(j=(i-1);j>=1;j--)
                {
                  printf("*");
                }
             printf("\n");
             }
             for(i=2;i<=n;i++)
             {
               for(j=1;j<=(n-i);j++)
                {
                  printf(" ");
                }
               for(j=1;j<=i;j++)
                {
                  printf("*");
                }
               for(j=(i-1);j>=1;j--)
                {
                  printf("*");
                }
             printf("\n");
             }
             getch();
}
    123454321
                                         1234321
                     12321
           121
             1 
                                               121
         12321
       1234321
     123454321
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,j,n;
            clrscr();
            printf("How many pirimids in your pirimids ? : ");
            scanf("%d",&n);
            for(i=n;i>=1;i--)
             {
               for(j=(n-i);j>=1;j--)
                {
                  printf(" ");
                }
               for(j=1;j<=i;j++)
                {
                  printf("%d",j);
                }
               for(j=(i-1);j>=1;j--)
                {
                  printf("%d",j);
                }
             printf("\n");
             }
             for(i=2;i<=n;i++)
             {
               for(j=1;j<=(n-i);j++)
                {
                  printf(" ");
                }
               for(j=1;j<=i;j++)
                {
                  printf("%d",j);
                }
               for(j=(i-1);j>=1;j--)
                {
                  printf("%d",j);
                }
             printf("\n");
             }
             getch();
}




9.Write a program to print out all Armstrong number between 1 and 10000. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = ( 1*1*1) + ( 5*5*5*) + (3*3*3).
#include<stdio.h>
#include<conio.h>
void main()
{
            Long int a,b,i,n;
            long sum;
            clrscr();
            printf("Armstrong number numbers between 1 and 10000 are :\n\n\n\n");
            for(n=2;n<=10000;n++)
            {
                 b=n;
                 sum=0;
                 for(i=1;n!=0;i++)
                 {
                          a=n%10;
                          sum=(a*a*a)+sum;
                          n=n/10;
                 }
                 if(b==sum)
                 printf("%5d",b);
                 n=b;
            }
            getch();
}















কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন