-->

Loop related programs using while,do..while and 'for' with 'if' structure

Here are collection of programs using loop. here,You have programs written in while,do..while and for loop separately. Some are done using all loops as a sample to make you understandable. Then programs preceded by them can be written in same manner.Some programs are with 'if structure' depending upon requirements by question.

Read them now.

you can use while,do..while and for loop

Q1) Write a program to print/display 1 to 10 natural numbers.
                                                                                                                     solution

Q2)   Write a program  to print/display 1 to n natural numbers.

                                                                                                                   solution

Q3) Write a program  to print/display all even numbers lying between 1 to n natural numbers.
                                                                                                                     solution

Q4) Write a program  to print/display all odd numbers lying between 1 and 100.
                                                                                                                     solution

Q5)   Write a program  to print/display 100 to 1 natural numbers.
                               
                                                                                                                       solution

Q6)Write a program  to input your name and address and to display them 40 times.
                                                                                                                     solution


Q7) Write a program to print/display 1,8,27... to 10th term.
                                                                                                                     solution
Q8) Write a program to print/display 2,4,6,8,10,12... to 15th term.
                                                                                                                     solution
q9)Write a program to display multiplication table of a number .
                                                                                                                     solution

q10)Write a program to print/display 1,6,11,16....nth term.
                                                                                                                     solution

q11)Write a program to print/display 1,111,1111,11111,.... to nth term.
                                                                                                                     solution
Q12)Write a program to get sum of 1 to 10 natural numbers.
                                                                                                                     solution
q13)Write a program  to get/sum of all even numbers lying between 1 to n natural numbers.
                                                                                                                     solution
q14)Write a program  to print/display all characters from a to z.
                                                                                                                     solution
q15)Write a program  to print/display all ascii values of letters from A to Z and a to z..
                                                                                                                     solution
Q16)Write a program to print/display 11111,1111,111,11,1 to nth term.
                                                                                                                     solution
Q17)Write a program to convert binary no. into decimal number system.
                                                                                                                     solution
Q18)Write a program to convert decimal number into binary.
                                                                                                                     solution
Q19)Write a program to convert decimal number into octal.
                                                                                                                     solution
Q20)Write a program to convert decimal number into Hexadecimal.
                                                                                                                     solution
q21)Write a program to convert hexadecimal number into decimal .
                                                                                                                     solution
q22)Write a program to convert octal no. into decimal number system.
                                                                                                                     solution
q23)Write a program to convert binary no. into octal number system.
                                                                                                                     solution
q24)Write a program to convert binary no. into hexadecimal number system.
                                                                                                                     solution
q25)Write a program to convert hexadecimal number into binary number system.
                                                                                                                     solution

q26)Write a program to get factorial value of a positive number
                 if number =5 then its factorial value=5x4x3x2x1=120
                                                                                                                     solution
q27)Write a program to print/display all factors of  number 'n'.
        e.g.    if n= 6 then factors are1,2,3,6.(all can divide it)
                                                                                                                    solution
q28)Write a program to print/display all perfect square numbers lying between 1 and 'n'.
        e.g.    if n= 20 then perfect squares are 1,4,9,16....
                                                                                                                    solution
q29)Write a program to print/display 1+1,1+2,1+3,1+4,1+5,....           to nth term.
                                                                                                                     solution
q30)Write a program to print/display 1+1/1,1+1/2,1+1/3,1+1/4,1+1/5,....           to nth term.
                                                                                                                     solution
q31)Write a program to test whether a number is prime or not.
                                                                                                                     solution
q32)Write a program to get sum of 1+1,1+2,1+3,1+4,1+5,....           to nth term.
                                                                                                                     solution
q33)Write a program to test whether a number is palindrome or not.
                                                                                                                     solution
                                                                                                                    solution
q34)Write a program to get series 0,1,1,2,3,5,8....15th term.
                      (it is called Fibonacci series)
                                                                                                                     solution
q35)Write a program to reverse a number.
                       123=321                                                                                solution

q36)Write a program to get sum of all digits present in a number.
                   e.g    123=3+2+1=6                                                                   solution
q37)Write a program to get/display digits which are in even position present in a number.
                   e.g    123=2                                                                               solution

q38)Write a program to get/display digits which are odd positioned in a number.
                   e.g    123=1 and 3                                                                      solution

q39)Write a program to get/display digits which are even in a number.
                   e.g    12345=2 and 4                                                                   solution
q40)Write a program to get/display digits which are odd  in a number.
                   e.g    12345=1 and 3 and 5                                                          solution
q41)Write a program to read age of any 100 persons and count the number of persons in the age group between 20 and 40.

                                                                                                                       solution

Q42)Write a program to get 0.9,0.99,0.999,0.9999.....nth terms.
                                                                                                                       solution

         

q43)Write a program to get sum of (2x3)/5+(4x5)/7+(6x7)/9+,....           to nth term.
                                                                                                                     solution

q44)Write a program to get sum of 12x1+23x3+34x5+..............................nth terms.
                                                                                                                    solution

q45)Write a program to get sum of 1+3/4+7/16+15/64..............................nth terms.
                                                                                                                    solution

q46)Write a program to get sum of x+ x2/2+x3/3+x4/4+.............................nth terms.
                                                                                                                    solution

q47)Write a program to count number of digits present in a number.
         e..g. in 453=3 digits                                                                           solution
   

continue with nested loop




A program to get marks of any 5 subjects and to get total marks with percentage and division.

//Write a program to get marks of any 5 subjects and to get total marks with percentage and division.
#include<stdio.h>
#include<conio.h>
void main()
{
int marks_english,marks_math,marks_account,marks_cscience,marks_economics;
int total_marks;
float percentage;
printf("enter marks of all subjects \n");
printf("enter marks for english\n");
scanf("%d",&marks_english);
printf("enter marks for mathematics\n");
scanf("%d",&marks_math);
printf("enter marks for account\n");
scanf("%d",&marks_account);
printf("enter marks for cscience\n");
scanf("%d",&marks_cscience);
printf("enter marks for economics\n");
scanf("%d",&marks_economics);
if(marks_english>=40 && marks_math>=40 && marks_account>=40 && marks_cscience>=40 && marks_economics>=40)
  {
     printf("u have passed!!\n and \n");
      total_marks=marks_english+marks_math+marks_account+marks_cscience+marks_economics;
      percentage=(float) total_marks/5;
       if(percentage>=75)
           {
               printf("you have got %f percentage with distinction!!\n",percentage);
            }
      elseif(percentage>=60 && percentage<75 )
           {
               printf("you have got %f percentage with first division!!\n",percentage);
            }
      elseif(percentage>=55 && percentage<60 )
           {
               printf("you have got %f percentage with second division!!\n",percentage);
            }
       elseif(percentage>=40 && percentage<55 )
           {
               printf("you have got %f percentage with third division!!\n",percentage);
            }
   }
    
else
 {
printf("sorry, you have failed the exam.\n"); 
 printf("so there is no total and percentage with division for you.\n Try hard next time.\n");   
  } 
printf("thank you!!!!\n");
getch();
}

Write a program to read annual salary of an employee and decide tax withheld as follows.

//Write a program to read annual salary of an employee and decide tax withheld as follos.
//         salary                                          tax
//            <=100000                                0%
//           upto 150000                              15%
//          above 150000                             25%
#include<stdio.h>
#include<conio.h>
void main()
{
float annual_salary;
float total_tax;
printf("enter total annual salary \n");
scanf("%f",&total_sales);
if(annual_salary<=100000)
    {
         total_tax=0; 
         printf("total tax to be paid=Rs. 0\n");
    }
elseif(annual_salary>100000 &&annual_salary<=150000)
    {
         total_tax=annual_salary*0.15
         printf("total tax to be paid=Rs. %f\n",total_tax);
    }
elseif(annual_salary>150000)
    {
          total_tax=annual_salary*0.25
          printf("total tax to be paid=Rs. %f\n",total_tax);
    } 
printf("thank you!!!!\n");
getch();
}

Write a program to input cost price and selling price and determine whether there is loss or profit.

//Write a program to input cost price and selling price and determine whether there is loss or profit.
#include<stdio.h>
#include<conio.h>
void main()
{
float selling_price,cost_price;
printf("enter cost price \n");
scanf("%f",&cost_price);
printf("enter selling price \n");
scanf("%f",&selling_price);
if(selling_price>cost_price)
    {
          printf("there is profit\n");
          printf(" and profit is=%f\n",selling_price-cost_price);
    }
  else
    {
         printf("there is loss");
         printf(" and loss is=%f\n",cost_price-selling_price);

      }
printf("thank you!!!!\n");
getch();
}

Write a program to read age of person and find out the category among following according to age.

//Write a program to read age of person  and find out the category among following according to age.
//           category                                         age
//            child                                            0 to 12
//            teenage                                         13 to 19
//           adult life                                      20 to 30
//           mature life                                   31 to 50
//          old age                                         over 50

#include<stdio.h>
#include<conio.h>
void main()
{
float age_person;
printf("enter your age \n");
scanf("%f",&age_person);
if(age_person>=0 && age_person<=12)
    {
          printf("Your are child\n");
    }
elseif(age_person>12 && age_person<=19)
    {
          printf("Your are teenage\n");
    }
elseif(age_person>19 && age_person<=30)
    {
          printf("Your are in adult life\n");
    }    
elseif(age_person>30 && age_person<=50)
    {
          printf("Your are passing your mature life now\n");
    }
elseif(age_person>50)
    {
          printf("Your are now an old person\n");
    }
printf("thank you!!!!\n");
getch();
}

A company pays its employee on hourly basis.If an employee works for 8hrs hours he gets 100/hr and 120/hr for additional hours.Write a program to read working hours of an employee and calculate total salary.

//A company pays its employee on hourly basis.If an employee works for 8hrs hours he gets  100/hr
//and 120/hr for additional hours.
//Write a program to read working hours of an employee and calculate total salary.
#include<stdio.h>
#include<conio.h>
void main()
{
float total_working_hrs,extra_hrs;
float total_salary;
printf("enter your total working hours \n");
scanf("%f",&total_working_hrs);
if(total_working_hrs<=8)
    {
           total_salary=total_working_hrs*100
          printf("total salary=Rs. %f\n",total_salary);
    }
elseif(total_working_hrs>8)
    {
         extra_hrs=total_working_hrs-8;
          total_salary=extra_hrs*120+8*100;
         printf("the total salary earned=%f\n",total_salary);
      }

getch();
}


A program to display name of day on the basis of entered number . for example, 2 for Monday.

//Write a program to display name of day on the basis of entered number.
//. for example, 2 for Monday.                        
#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
printf("enter number for day\n");
scanf("%d",&choice);
 switch(choice)
          {
                  case 1:
                            printf("Today is Sunday\n");
                           
                 break;
               case 2:
                            printf("Today is Monday\n");
                           
                 break;

               case 3:
                            printf("Today is Tuesday\n");
                           
                 break;
            case 4:
                            printf("Today is Wednesday\n");
                           
                 break;
            case 5:
                            printf("Today is Thursday\n");
                           
                 break;
           case 6:
                            printf("Today is Friday\n");
                           
                 break;


           case 7:
                            printf("Today is Saturday\n");
                           
                 break;
          default:
                            printf("sorry, not a valid input(beyond 7!!!!\n");
                            printf("thank u, terminating....\n");
          }
getch();
}

note:
here, you can use 'char' data type instead 'int' and you can input data before switch statement as well.