Write these programs using C 1 Using while loop display the
Write these programs using C++
Solution
1)a)//print numbers from 1 to 10 using while loop
//preprocessor directives
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
//initialize i value as 1
int i=1;
//loop till i value becomes equal to 1
while(i<=10)
{
//print value of i
cout<<i<<\"\\t\";
//incrment i
i++;
}
getch();
}
1.b)//print numbers from 10 to 1 using while loop
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i=10;
//iterate till i value becomes equal to 1 from 10
while(i>=1)
{
//print value of i
cout<<i<<\"\\t\";
//decrement value of i
i--;
}
getch();
}
2.//calculate average income using do while loop
//preprocessor directives
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i=1,sum_income=0,average_income=0;
do
{
//enter day based income
cout<<\"Enter the income earned in \\t\"<< i <<\"\\t day\ \";
cin>>day_income;
//calculation of total income earned in a week
sum_income=sum_income+day_income;
}while(i<=7);
//calculation of average income
average_income=(sum_income/i);
cout<<\"\ \"<<average_income<<\"\ \";
getch();
}
3.a) using for loop print numbers 4 8 12 16 20 24
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i;
//iterate till i value becomes equal to 24 from 4
for(i=4;i<=24;)
{
//print value of i
cout<<i<<\"\\t\";
//increment value of i by 4
i=i+4;
}
getch();
}
3.b) using for loop print numbers 5 10 15 20 25 30
//preprocessor directives
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i;
//iterate till i value becomes equal to 30 from 5
for(i=5;i<=30;)
{
//print value of i
cout<<i<<\"\\t\";
//increment value of i by 5
i=i+5;
}
getch();
}


