Thursday, December 4, 2025

Write a C Program, Input 3 Digit number and find given number is An Armstrong or Not Armstrong.

 

An Armstrong number is defined as the sum of power of each digit to a n digit number is equal to that number.

 

3-digit: 153, 370, 371, 407

4-digit: 1634, 8208, 9474

 

Example 1 (3-digit number):

Number: 153

Digits: 1,5,3

Number of digits: 3

Calculation: 13+53+33

                       1+125+27 = 153

 

Example 2 (4-digit number):

1Number: 1634

Digits: 1,6,3,4

Number of digits: 4

Calculation:  14+64+34+44

                       1+1296+81+256 =1634


#include<stdio.h>
main()
{
    int n,r,x,sum=0;
    printf("\nEnter Number : ");
    scanf("%d",&n);
    x=n;
    while(n!=0)
    {
r=n%10;
sum+=r*r*r;
n/=10;
    }
    printf("\n%d Sum of Cube = %d",x,sum);
    if(x==sum)
printf("\nThe Given %d no is Armstrong Number",x);
    else
printf("\nThe Given %d no is Not Armstrong Number",x);
}

Output



Write a C Program, Input a Number, and print Reverse of the given no. And also find no is Palindrome or not Palindrome.

#include<stdio.h>
main()
{
  int n,x,r=0,s=0;
  printf("\nEnter Number : ");
  scanf("%d",&n);
  x=n;
  while(n!=0)
  {
     r=n%10;
     s=s*10+r;
     n/=10;
  }
  printf("\Given Number is %d and Reverse Number is %d",x,s);
  if(x==s)
     printf("\nThe Given Number is Palindrome ");
  else
     printf("\nThe Given Number is Not Palindrome ");
}

Output





Write a C Program. Input Number and Print of Input Number.

#include<stdio.h>
main()
{
     int n,r,sum=0;
     printf("Input Number : ");
     scanf("%d",&n);
     while(n!=0)
     {
          r=n%10;
          sum+=r;
          n/=10;
      }
      printf("Sum=%d", sum);
}


Output

  


Monday, December 1, 2025

Input numbers. Calculate Sum and Average (Mean) with using do-while loop

#include<stdio.h>
main()
{
    int i=0,n,sum=0;
    float avg=0.0; 
    do
    {
       printf("Enter Numbers. (-999 Exit) : "); 
       scanf("%d",&n);
       if(n == -999)      
            break;  
       sum+=n;
       ++i;
    }while(1);  
    avg=sum/(float)i;
    printf("Sum=%d\nAvg=%f",sum,avg);
}

Output



Input numbers. Calculate Sum and Average (Mean) with using while loop

#include<stdio.h>
main()
{
     int i=0,n,sum=0;
     float avg=0.0;
     while(1)
      {
  printf("Enter Numbers. (-999 Exit) : ");
          scanf("%d",&n);
          if(n == -999)
                break;
          sum+=n;
          ++i;
       }
       avg=sum/(float)i;
       printf("Sum=%d\nAvg=%f\n",sum,avg);
}


Output



Input 10 numbers. Calculate Sum and Average with using do-while loop

#include<stdio.h>
main()
{
    int i=1,n,sum=0;
    float avg=0.0; 
    printf("\nInput 10 Numbers : ");
    do
    {
       scanf("%d",&n)       
       sum+=i;
       ++i;
    }while(i<=10);  
    avg=sum/10.0;
    printf("Sum=%d\nAvg=%f",sum,avg);
}

Output




Input 10 numbers. Calculate Sum and Average with using while loop

#include<stdio.h>
main()
{
     int i=1,n,sum=0;
     float avg=0.0;
     printf("\nInput 10 Numbers : "); 
     while(i<=10)  
     {                
         scanf("%d",&n);
         sum+=n;                     
         ++i;  
     }                 
     avg=sum/10.0;
     printf("Sum=%d\nAvg=%f",sum,avg);
}

Output

Sunday, November 30, 2025

Input 10 numbers. Calculate Sum and Average with using for loop

 #include<stdio.h>
main()
{
     int i,n,sum=0;
     float avg=0.0;
     printf("\nInput 10 Numbers : ");
     for(i=1;i<=10;++i)
     {
  scanf("%d",&n);
          sum+=n;
     }
     avg=sum/10.0;
     printf("Sum=%d\nAvg=%f",sum,avg);
}

Output



C break and continue statements.

break and continue are jump statements used to alter the flow of execution within loops and switch statements used In C programming.


break Statement:

The break statement is used to immediately terminate the innermost for, while, do-while loop, or switch statement. 

Ex.

#include<stdio.h>
main()
{
     int i;
     for(i=1;i<=100;++i)
     {  
         if(i>=51)
             break;
         printf("\t%d",i);
     }
}


Output





continue Statement:

The continue statement is used to skip the remaining statements within the current iteration of a for, while, or do-while loop and proceed to the next iteration. It does not terminate the entire loop. 


Ex.


#include<stdio.h>
main()
{
     int i;
     for(i=1;i<=100;++i)
     {  
        if(i>=41 && i<=59)
           continue;
        printf("\t%d",i);
     }
}


Output



Tuesday, November 25, 2025

Write a C Program Print 1 To 100 Numbers using do-while Loop

 #include<stdio.h>
main()
{
     int i=1;
     do
     {
          printf("%d\t",i);
          ++i;
     }while(i<=100);
}


Output




Write a C Program Print 1 To 100 Numbers using while Loop

 

#include<stdio.h>
main()
{
     int i=1;
     while(i<=100)
     {
          printf("%d\t",i);
          ++i;
     }
}


Output



Write the C Program. Print the following format ( K Type)

 
          KKKKK
        KKKK
        KKK
        KK
        K
        K
        KK
        KKK
        KKKK
        KKKKK


#include<stdio.h>
main( )
{
    int i,j;
    for(i=5;i>=1;--i)
    {
for(j=1;j<=i;++j)
   printf("K");
        printf("\n");
    }
    for(i=1;i<=5;++i)
    {
        for(j=1;j<=i;++j)
   printf("K");
        printf("\n");
    }
}


        Output

                


Write the C Program. Print the following format (Numbers in K Type)

 

     12345
    1234
    123
    12
    1
    1
    12
    123
    1234
    12345

#include<stdio.h>
main( )
{
    int i,j;
    for(i=5;i>=1;--i)
    {
for(j=1;j<=i;++j)
           printf("%2d",j);
        printf("\n");
    } 
    for(i=1;i<=5;++i)
    {
        for(j=1;j<=i;++j)
           printf("%2d",j);
        printf("\n");
    }
}


            Output




Write the C Program. Print the following format


    1
    2 3
    4 5 6
    7 8 9 10
    11 12 13 14 15


#include<stdio.h>
main( )
{
    int i,j,k=1;
    for(i=1;i<=5;++i)
    {
for(j=1;j<=i;++j)
{
   printf("%3d",k);
   ++k;
}
        printf("\n");
    }
}


            Output

            


Write the C Program. Print the following format

 Write the C Program. Print the following format  

    *****
    ****
    ***
    **
    *


#include<stdio.h>
main( )
{
    int i,j;
    for(i=5;i>=1;--i)
    {
for(j=i;j>=1;--j)
   printf("*");
        printf("\n");
    }
}


                    Output




Write the C Program. Print the following format

 

Write the C Program. Print the following format  

5 4 3 2 1    
4 3 2 1     
3 2 1      
2 1       
1        




#include<stdio.h>
main( )
{
    int i,j;
    for(i=5;i>=1;--i)
    {
for(j=i;j>=1;--j)
           printf("%2d",j);
        printf("\n");
    }
}


        Output

    




Write the C Program. Print the following format

 Write the C Program. Print the following format  

    1
    12
    123
    1234
    12345


     
     


#include<stdio.h>
main( )
{
    int i,j;
    for(i=1;i<=5;++i)
    {
        for(j=1;j<=i;++j)
           printf("%2d",j);
        printf("\n");
    }
}

            Output



Monday, November 24, 2025

Write the C Program. Print the following format

 Write the Program Print the following format
        12345        
        12345       
        12345       
        12345       
        12345 


#include<stdio.h>
main()
{
    int i,j;
    for(i=1;i<=5;++i)
    {
          for(j=1;j<=5;++j)
       printf("%2d",j);
          printf("\n");
    }
}

                       Output



Fibonacci Sequence up to 10

 First few numbers: 0,1,1,2,3,5,8,13,21,34


Sequence and Formula:

The sequence starts with f=0 &  f1=1             

 Every subsequent number is by adding the two numbers before it.

f2= 0+1 -> 1  1+1 ->2  1+2 -> 3 ........


   f2=f1+f        
   f=f1 
   f1=f2







 #include<stdio.h>
main()
{
       int f=0,f1=1,f2=0,i;
       printf("\n%d\t%d",f,f1);
       for(i=3;i<=10;++i)
      {
            f2=f1+f;
            printf("\t%d",f2);
            f=f1;
            f1=f2;
      }
}


Output


Print Prime Numbers from 1 To 100.

 #include<stdio.h>
main()
{
   int i,n,t=0;
   for(n=1;n<=100;++n)
   {
         for(i=1;i<=n;++i)
        {
            if(n%i==0)
      ++t;
        }
         if(t==2)
       printf("\t%d",n);
         t=0;
   }
}


Output



Input Number and Give Number is Prime or Not Prime Number.

 #include<stdio.h>
main()
{
   int i,n,t=0;
   printf("\nEnter Number : ");
   scanf("%d",&n);
   for(i=1;i<=n;++i)
   {
     if(n%i==0)
       ++t;
   }
   if(t==2)
      printf(" %d no is Prime", n);
   else
      printf(" %d no is Not Prime", n);
}


       Output




Print the factorial of given Number.

 


#include<stdio.h>
main()
{
  int i,n;
  unsigned long int fact=1;
  printf("\n\nInput Number : ");
  scanf("%d",&n);
  for(i=1;i<=n;++i)
     fact*=i;
  printf("Factorial of  %d is %ld ",n, fact);
}

                  Output



Input Number and Print Multiplication Table.

 #include<stdio.h>
main()
{
    int i,n;
    printf("\nEnter Number to Maltiplication Table : ");
    scanf("%d",&n);
    for(i=1;i<=10;++i)
      printf("\n%d X %d = %d",n,i,n*i);
}

                     Output



Thursday, November 20, 2025

Loops in C. for, while and do.. while

 

Loops in C are control flow statements that facilitate the repeated execution of a block of code based on a specified condition. C provides three primary types of loops: for loop, while loop and do-while loop.

 

for loop

Simple for loop


  Syntax 

       for(initial value; condition; increment/decrement)

       {             

          statements;

       }

 

The for statement followed three statements in the parenthesis separated by semi-colon (;).

Initialization of the variables to execute once.

Conditional statement: It will check the condition, if it is true then body part of the loop will be executed.

Increment/decrement statements: After the condition check, increment/decrement statement is executed and once again the condition is evaluated.


Ex.

     int i;
     for(i=1;i<=10;++i)
           printf("%3d",i);


output 1  2  3  4  5  6  7  8  9  10  





Nested loop

 Loop in loop is called nested loop

  

Syntax   

       for(initial value; condition; increment/decrement)

              for(initial value; condition; increment/decrement)

                        statement;




   while
 
The while is an entry controlled loop. The condition is evaluated and if the condition is true then the executed of the loop.                     
                                             
Syntax

   ------------                          
   while(condition)                         
   {                                          
         statement 1;             
         statement 2;             
         -----------                     
         statement n;             
   }                                          
    --------------                                  
 
 
Ex.
   
         int i=1;
        while(i<=10)
        {
           printf("%3d",i);
           ++i;
        }


         output 1  2  3  4  5  6  7  8  9  10 


do - while

  The do while is exit controlled loop. The program proceeds to execute the body of the loop first. At the end of the loop, condition is teste. It is true then executed once again.
                                  

Syntax
           
  -------------
  do
   {                                          
         statement 1;             
         statement 2;             
         -----------                     
         statement n; 
   }while(condition);
    -----------------                              
 

 Ex.
     
        int i=1;
        do
        {
               printf("%3d",i);
               ++i;
        }while(i<=10)

        output 1  2  3  4  5  6  7  8  9  10