Programs

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

Program 1:

#include <iostream>
using namespace std;
int main()
{
int rows=10, num=1;

for(int i=1; i<=rows; i++)


{
for(int j=1; j<=i; j++)
{
cout<<num<<" ";
num++;
}
cout<<endl;
}
return 0;
}

Program 2:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int binary, decimal=0, base=1, remainder;
cout<<"Enter a binary number: ";
cin>>binary;
while(binary>0)
{
remainder = binary % 10;
decimal = decimal + remainder * base;
base = base * 2;
binary = binary / 10;
}
cout<<"\nThe decimal equivalent is: "<<decimal<<endl;
return 0;
}

Program 3:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num, onum, remainder, n=0, result=0;
cout<<"All three digit Armstrong numbers are: "<<endl;
for(num=100; num<=999; num++)
{
onum = num;
while(onum!=0)
{
onum /= 10;
n++;
}
onum = num;
while(onum!=0)
{
remainder = onum % 10;
result += pow(remainder, n);
onum /= 10;
}

if(result == num)
{
cout<<num<<endl;
}
n = 0;
result = 0;
}
return 0;
}

Program 4:
#include <iostream>
using namespace std;
int main()
{
int num, digit, count=0;
cout<<"Enter an integer of 5 digits or fewer: ";
cin>>num;
while(num>0)
{
digit = num % 10;
if(digit==9)
{
count++;
}
num /= 10;
}
cout<<"\nThe number of 9s in the integer is: "<<count<<endl;
return 0;
}

Program 5:
#include <iostream>
using namespace std;
int main()
{
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
if ((i+j)%2==0)
{
cout << "*";
} else
{
cout << " ";
}
}
cout<<"\n";
}
return 0;
}

Program 6:
#include <iostream>
using namespace std;
void fact()
{
int x=1, fact=1, n;
cout<<"Enter a number: ";
cin>>n;
while(x<=n)
{
fact = fact * n;
n--;
}
cout<<"\nFactorial of given number is: "<<fact<<endl;
}
int main()
{
fact();
return 0;
}

Program 7:
#include <iostream>
using namespace std;
int main()
{
int current_population, growth_rate;
int year1, year2, year3, year4, year5;
cout<<"Enter the current world population: ";
cin>>current_population;
cout<<"\nEnter the annual world population growth rate in percent: ";
cin>>growth_rate;
year1 = current_population + current_population * (growth_rate / 100.0);
year2 = year1 + year1 * (growth_rate / 100.0);
year3 = year2 + year2 * (growth_rate / 100.0);
year4 = year3 + year3 * (growth_rate / 100.0);
year5 = year4 + year4 * (growth_rate / 100.0);
cout<<"\nEstimated population after 1 year: "<<year1<<endl;
cout<<"\nEstimated population after 2 years: "<<year2<<endl;
cout<<"\nEstimated population after 3 years: "<<year3<<endl;
cout<<"\nEstimated population after 4 years: "<<year4<<endl;
cout<<"\nEstimated population after 5 years: "<<year5<<endl;
return 0;
}

You might also like