Looping C programming

  • x and n are input through keyboard.Write a program to compute xn, n!, nCr, nPr

    xn
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int x,n,res=1;
    int i;
    printf(“Enter the value of x?\n”);
    scanf(“%d”,&x);
    printf(“Enter the value of n?\n”);
    scanf(“%d”,&n);
    for(i=0;i<n;i++)

    res=res*x;
    printf(“Answer:%d”,res);

    getch();
    }

    n!
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    int n,i,
    res=1;
    clrscr();
    printf(“Enter the n=”);
    scanf(“%d”,&n);
    for(i=n;i>=1;i–)
    res=res*i;
    printf(“n!=%d”,res);
    getch();
    }nCr
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int n,r,sum=1,i,k,p,res=1,x,rem=1,f,ncr;
    printf(“Enter the value of n:”);
    scanf(“%ld”,&n);
    printf(“Enter the value of r:”);
    scanf(“%ld”,&r);
    p=n-r;
    for(i=n;i>=1;i–)
    {
    sum=sum*i;
    }
    for(k=p;k>=1;k–)
    {
    res=res*k;
    }
    for(x=r;x>=1;x–)
    {
    rem=rem*x;
    }
    f=res*rem;
    ncr=sum/f;
    printf(“\nncr=%ld”,ncr);
    getch();
    }
    nPr#include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int n,r,sum=1,i,k,p,res=1,l;
    printf(“Enter the value of n:”);
    scanf(“%ld”,&n);
    printf(“Enter the value of r:”);
    scanf(“%ld”,&r);
    p=n-r;
    for(i=n;i>=1;i–)

    sum=sum*i;

    for(k=p;k>=1;k–)
    {
    res=res*k;
    }
    l=sum/res;
    printf(“\nnpr=%ld”,l);
    getch();
    }

  • Write a program to determine the GCD and LCM of 3 numbers.
    GCD#include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a,b,c,rem;
    printf(“Enter the value of a?\n”);
    scanf(“%d”,&a);
    printf(“Enter the value of b?\n”);
    scanf(“%d”,&b);
    printf(“Enter the value of c?\n”);
    scanf(“%d”,&c);
    while(b>0)
    {
    rem=a%b;
    a=b;
    b=rem;
    }
    while(c>0)
    {
    rem=a%c;
    a=c;
    c=rem;
    }
    printf(“GCD is:%d”,a);
    getch();
    }
    LCM
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a,b,c,i;
    printf(“Enter the value of a?\n”);
    scanf(“%d”,&a);
    printf(“Enter the value of b?\n”);
    scanf(“%d”,&b);
    printf(“Enter the value of c?\n”);
    scanf(“%d”,&c);
    for(i=1; ;i++)
    {
    if(i%a==0&&i%b==0&&i%c==0)
    break;
    }
    printf(“LCM is:\n%d”,i);
    getch();
    }
  • Find out the sum of each of the following series. n is the input from user for series (4)
    1. 3+11+19+………+1691.
    2. 7+20+33+………(up to 100th term)
    3. 5-11+17-……….(up to 75th term)
    4. 2*7*12*………..*37

    1.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int x=3,sum=0;
    for(x=3;x<=1691;x=x+8)
    {
    sum=sum+x;

    }
    printf(“\n\nSUM:%ld”,sum);
    getch();
    }
    2.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int x=7,sum=0,i;
    for(i=1;i<=100;i++)
    {
    sum=sum+x;
    x=x+13;
    }
    printf(“\n\nSUM:%ld”,sum);
    getch();
    }
    3.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int x=5,sum=0,i;
    for(i=1;i<=75;i++)
    {
    sum=sum+x;
    if(x>0)
    x=(x+6)*-1;
    else
    x=(x-6)*-1;
    }
    printf(“\n\nSUM:%ld”,sum);
    getch();
    }.
    4.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int x=2,sum=1;
    for(x=2;x<=37;x=x+5)
    {
    sum=sum*x;

    }
    printf(“\n\nSUM:%ld”,sum);
    getch();
    }

  • Write  a program to determine all prime numberswithin the range [a…….b]
    where a & b are input through keyboard.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int num,num1,i,n,l;
    scanf(“%d”,&num);
    scanf(“%d”,&num1);
    n=2;
    l=num;
    for(i=l;i<=num1;i++)
    {
    while(n<=i/2)
    {
    if(i%n==0)
    break;
    ++n;
    }
    if(n>i/2 && i!=2)
    printf(” %d”,i);
    n=2;
    }
    getch();
    }

  • 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()
    {
    clrscr();

    int i,n,k;
    scanf(“%d”,&n);

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

    printf(“\t%d”,k*i);

    printf(“\n”);
    }
    getch();
    }

  • Write a program to find out first n perfect number where n is the input from user.

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    clrscr();
    int i,num,sum=0;
    printf(“Enter a num”);
    scanf(“%d”,&num);
    for(i=1;i<=num/2;i++)
    if(num%i==0)
    sum=sum+i;
    if(sum==num)
    printf(“The number is a perfect number”);
    else
    printf(“The number is not a perfect number”);
    getch();
    }

  • Write a program to find first n Fibonacci number where n is the input from user.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int n,a,b,sum,i,rem;
    a=0;
    b=1;
    printf(“Enter the value of n?\n”);
    scanf(“%d”,&n);
    printf(“The fibonacci numbers are:”);
    for(i=1;i<=n;i++)
    {
    sum=a+b;
    rem=sum;
    a=b;
    b=rem;
    printf(“\n%d\n”,sum);
    }
    getch();
    }

  • Write a program to show the following triangle/rectangle of  “*” or numbers.Take n as inpute from user to determine the number of rows of the structure.(eg. n=5)
    1.
    #include<stdio.h>
    #include<conio.h>
    void main()                                                                                   {                                                                                        clrscr();

    int i,k,n;
    scanf(“%d”,&n);

    int c=1;                                                                  
    int s=n-1;

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

    for(k=1;k<=s;k++)
    printf(” “);
    s–;

    for(k=1;k<=c;k++)
    printf(“*”);

    c=c+2;
    printf(“\n”);
    }

    getch();
    }

    2.
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    clrscr();

    int i,k,n;
    scanf(“%d”,&n);
    int c=1;
    int s=n-1;

    for(i=1;i<=n;i++)
    {
    for(k=1;k<=s;k++)
    printf(” “);
    s–;                                                              

    for(k=1;k<=c;k++)

    if(k==1||k==c)
    printf(“*”);
    else if(i==n)
    printf(“*”);
    else
    printf(” “);

    c=c+2;

    printf(“\n”);
    }
    getch();
    }
    4.
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    clrscr();

    int i,k,n;
    scanf(“%d”,&n);

    int c=1;                                                             
    int s=n-1;

    for(i=1;i<=n;i++)
    {
    for(k=1;k<=s;k++)
    printf(“*”);

    for(k=1;k<=c;k++)
    printf(“*”);

    c=c;
    printf(“\n”);
    }

    getch();
    }
    5.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int i,n,k;
    scanf(“%d”,&n);
    for(i=1;i<=n;i++)                                                       
    {
    for(k=1;k<=n;k++)
    { if(k==1||k==n)
    printf(“*”);
    else if(i==1||i==n)
    printf(“*”);
    else
    printf(” “);
    }
    printf(“\n”);
    }

    getch();
    }

    7.
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    clrscr();

    int i,k,n;
    scanf(“%d”,&n);
    int c=1;                                                                    
    int s=n-1;

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

    for(k=1;k<=s;k++)
    printf(” “);
    s–;

    for(k=1;k<=c;k++)
    printf(“*”);

    c=c+2;
    printf(“\n”);
    }
    c=c-4;
    s=1;
    for(i=1;i<=n;i++)
    {

    for(k=1;k<=s;k++)
    printf(” “);
    s++;

    for(k=1;k<=c;k++)
    printf(“*”);

    c=c-2;
    printf(“\n”);
    }

    getch();
    }

    8.
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    clrscr();

    int i,k,n;
    scanf(“%d”,&n);
    int c=1;
    int s=n-1;
    for(i=1;i<=n;i++)                                                     
    {
    for(k=1;k<=s;k++)
    printf(” “);
    s–;

    for(k=1;k<=c;k++)
    printf(“*”);

    c=c+2;
    printf(“\n”);
    }
    c=c-4;
    s=1;
    for(i=1;i<=n;i++)
    {

    for(k=1;k<=s;k++)
    printf(” “);
    s++;

    for(k=1;k<=c;k++)
    printf(“*”);

    c=c-2;
    printf(“\n”);
    }
    getch();
    }

    10.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();

    int i,k,n;
    scanf(“%d”,&n);

    int c=(2*n)-1;
    int s=0;

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

    for(k=1;k<=s;k++)
    printf(” “);
    s++;

    for(k=1;k<=c;k++)
    printf(“*”);

    c=c-2;
    printf(“\n”);
    }
    c=3;
    s=n-2;
    n=n-1;
    for(i=1;i<=n;i++)
    {

    for(k=1;k<=s;k++)
    printf(” “);
    s–;

    for(k=1;k<=c;k++)
    printf(“*”);

    c=c+2;
    printf(“\n”);
    }

    getch();
    }

  • Write a program to print out all Armstrong numbers between 1 and 10000.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int num,sum=0,k,rem,i;
    for(i=1;i<=10000;i++)
    {
    num=i;
    sum=0;

    for(k=1;k<=i;k++)
    {
    rem=num%10;
    sum=sum+(rem*rem*rem);
    num=num/10;
    }

    if(sum==i)
    printf(“\n%d\n”,i);
    }
    getch();
    }

  • Write a program to calculate how many 5 digit numbers can be created if the following terms apply:
    1. the leftmost digit is even
    2. the second digit is odd
    3. the third digit is a non even prime
    4. the fourth and fifth are two random digits not used before in the number.

      #include<stdio.h>
      #include<conio.h>

      long int prime(long int n);

      void main()
      {
      clrscr();
      long int a,b,c,d,e,i,rem,num,count=0;
      for(i=20000;i<=99999;i++)
      {
      rem=i%10;
      e=rem;
      num=i/10;
      rem=num%10;
      d=rem;
      num=num/10;
      rem=num%10;
      c=rem;
      num=num/10;
      rem=num%10;
      b=rem;
      num=num/10;
      a=num;
      if(a%2==0&&b%2!=0&&prime(c)&&d!=a&&d!=b&&d!=c&&e!=d&&e!=a&&e!=b&&e!=c)
      count++;

      }
      printf(” \nRESULT=%ld”,count);
      getch();
      }

      long int prime (long int n)
      {       int m;
      m=2;
      while(m<=n/2)
      {
      if(n%m==0)
      break;
      ++m;
      }
      if(m>n/2&&n!=2&&n!=1&&n!=0)
      return 1;
      else
      return 0;
      }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s