All Basic Loop Programs in One C Program

#include<stdio.h>

int main()

{

int i,n,m,sum1=0,sum2=0,sum3=0,a,ogl,rem,rev=0,b,sum4=0,c,r1,r2;

long int fact=1;

printf("enter n value: ");

scanf("%d",&n);

printf("Enter m value for table: ");

scanf("%d",&m);

printf("Enter a value to get reverse of it : ");

scanf("%d",&a);

printf("Enter b value to get sum of it's digits : ");

scanf("%d",&b);

do {

       printf("Enter c value to get sum of it's 1st & last digit : ");

       scanf("%d",&c);

       if (c<9) printf("Enter minimum 2 digit number.\n");

      }while (c<9);

printf("\nNatural numbers upto %d: ",n);

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

{printf("\n%d",i);}

    printf("\nOdd numbers upto %d: ",n);

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

{printf("\n%d",i);}

    printf("\nEven numbers upto %d: ",n);

for (i=2;i<=n;i=i+2)

{printf("\n%d",i);}

    printf("\nMultipication table with %d upto %d: ",m,n);

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

{printf("\n%d",m*i);}

printf("\nReverse numbers from %d: ",n);

for (i=n;i>=1;i--)

{printf("\n%d",i);}

printf("\nSum of natural numbers upto %d: ",n);

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

{sum1=sum1+i;

  printf("\nsum : %d",sum1);}

  

printf("\nSum of odd natural numbers upto %d: ",n);

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

{sum2=sum2+i;

  printf("\nsum : %d",sum2);}

  

  printf("\nSum of even natural numbers upto %d: ",n);

    for (i=2;i<=n;i=i+2)

{sum3=sum3+i;

  printf("\nsum : %d",sum3);}

  

    for (;n>=1;n--)

{fact=fact*n;}

  printf("\nfactorial of n : %ld",fact);

  

  ogl=a;

    for (rev=0;a>0;)

{rem=a%10;

  rev=rev*10+rem;

  a=a/10;}

  printf("\nReverse of a is: %d",rev);

  if (rev==ogl) printf("\na is palindrome.");

  else             printf("\na is not a palindrome");

  

    for (;b>0;)

  {rem=b%10;

   sum4=sum4+rem;

   b=b/10;}

   printf("\nSum of digits in b number: %d",sum4);

   

  r1=c%10;

    for (;c>0;)

  {r2=c%10;

   c=c/10;}

   printf("\nSum of 1st & last digits in c: %d",r1+r2);    

}

Comments

Popular posts from this blog

Single Program Using All Automatic String Functions

C program for subtraction of two matrices (2D Arrays)

C program for Multiplication of Two matrices