0% found this document useful (0 votes)
9 views

CPP Loop

Uploaded by

tsegayegeyesus9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

CPP Loop

Uploaded by

tsegayegeyesus9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

BY CHARA ZERIHUN

Email: [email protected]

C++ program to print all odd numbers from 1 to 100


using a for loop:
#include <iostream>
using namespace std;

int main()
{
for (int i = 1; i <= 100; i++)
{
if (i % 2 == 1)
{
cout << i << " ";
}
}

return 0;
}

// output of this program is:


1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75
77 79 81 83 85 87 89 91 93 95 97 99
BY CHARA ZERIHUN
Email: [email protected]

C++ program to print all odd numbers from 1 to 100


using a while loop:
#include <iostream>
using namespace std;

int main()
{
int i = 1;

while (i <= 100)


{
if (i % 2 == 1)
{
cout << i << " ";
}
i++;
}

return 0;
}
// output of this program is:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75
77 79 81 83 85 87 89 91 93 95 97 99
BY CHARA ZERIHUN
Email: [email protected]

Using a do-while loop:


#include <iostream>
using namespace std;

int main()
{
int i = 1;

do
{
if (i % 2 == 1)
{
cout << i << " ";
}
i++;
} while (i <= 100);

return 0;
}
//output of this program is:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75
77 79 81 83 85 87 89 91 93 95 97 99
BY CHARA ZERIHUN
Email: [email protected]

C++ program for Celsius to Fahrenheit and Fahrenheit to


Celsius conversion
#include<iostream>
using namespace std;
int main()
{
float fahrenheit,celsuis;
int choice;
cout<<" Enter 1: fahrenheit to celsuis \n Enter 2: celsuis to fahrenheit \n";
cin>>choice;
if(choice==1)
{
cout<<"Enter fahrenheit value\n";
cin>>fahrenheit;
celsuis = (fahrenheit - 32)*0.5556;
cout<<" it is "<<celsuis<< " degree celsuis"<<endl;
}
else if(choice==2)
{
cout<<"enter the celsuis value \n";
cin>>celsuis;
fahrenheit = (1.8*celsuis)+32;
cout<<"it is "<<fahrenheit<< " degree Fahrenheit"<<endl;
} else
cout<<"wrong choice";
return 0;
}

OUTPUT OF THIS PROGRAM IS:


Enter 1: fahrenheit to celsuis
Enter 2: celsuis to fahrenheit
1
Enter fahrenheit value
45
it is 7.2228 degree celsuis
BY CHARA ZERIHUN
Email: [email protected]

C++ tutorial to find the largest of two user input numbers


#include <iostream>
using namespace std;
int main()
{
int Num1;
int Num2;
cout << "Enter the first number : ";
cin >> Num1;
cout << "Enter the second number : ";
cin >> Num2;
if (Num1 > Num2)
{
cout << Num1 << " is greater than " << Num2<<endl;
}
else
{
cout << Num2 << " is greater than " << Num1<<endl;
}
return 0;
}
OUTPUT OF THIS PROGRAM IS:

Enter the first number : 12

Enter the second number : 54

54 is greater than 12
BY CHARA ZERIHUN
Email: [email protected]

Print Half-Pyramid Pattern of Stars (*)


#include<iostream>
using namespace std;
int main()
{
int i, j;
for(i=0; i<6; i++)
{
for(j=0; j<=i; j++)
cout<<"* ";
cout<<endl;
}
cout<<endl;
return 0;
}

OUTPUT OF THIS PROGRAM IS:


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

Print Inverted Half-Pyramid Pattern using Stars (*)


#include<iostream>
using namespace std;
int main()
{
int i, j;
for(i=0; i<6; i++)
{
for(j=i; j<6; j++)
cout<<"* ";
cout<<endl;
}
cout<<endl;
return 0;
}
BY CHARA ZERIHUN
Email: [email protected]
OUTPUT OF THIS PROGRAM IS:
******
*****
****
***
**
*

Print Full Pyramid Pattern of Stars (*)


#include<iostream>

using namespace std;


int main()
{ int i,j,rows,space;
cout<<"enter the no of rows: ";
cin>>rows;
for(i=1;i<=rows;i++)
{
for(space=rows;space>i;space--)
cout<<" ";
for(j=0;j<i;j++)
cout<<"* ";
cout<<endl;
}
return 0;

OUTPUT OF THIS PROGRAM IS:

enter the no of rows: 5

*
**
***
****
*****
BY CHARA ZERIHUN
Email: [email protected]

Print inverted of Full Pyramid Pattern of Stars (*)


#include<iostream>

using namespace std;


int main()
{ int i,j,rows,space;
cout<<"enter the no of rows: ";
cin>>rows;
for(i=rows;i>=1;i--)
{
for(space=i;space<rows;space++)
cout<<" ";
for(j=1;j<=i;j++)
cout<<"* ";
cout<<endl;
}
return 0;

OUTPUT OF THIS PROGRAM IS:

enter the no of rows: 5

*****
****
***
**
*
BY CHARA ZERIHUN
Email: [email protected]

You might also like