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 



 


Tuesday, November 11, 2025

Input Fahrenheit Temperatures. Conversion into Celsius. C=(f-32)*5/9

 #include<stdio.h>
main()
{
    float f,c=0.0;
    printf("\nEnter Fahrenheit Temperatures : ");
    scanf("%f",&f);
    c=(f-32)*5.0/9.0;
    printf("\nCelsius = %f",c);
}



Result




Input Item No, Item Name, Quantity & Rate, Calculate Bill

 

#include<stdio.h>
main()
{
    int itno,qty;
    float rate,bill=0.0;
    char itname[10];
    printf("\nEnter Item No, Item Name, Qty. & Rate  : ");
    scanf("%d%s%d%f",&itno,itname,&qty,&rate);
    bill=qty*rate;
    printf("\nItem No = %d\nItem Name = %s\nBill = %.2f",itno,itname,bill);
}



Result