Friday, 5 July 2013

Write a program to accept 10 numbers and display total number of odd and even numbers.

#include<iostream>
using namespace std;
int main()
{
int numbers[10],odd,even;
odd=even=0;
cout<<"Enter the 10 numbers: \n";
for(int i=0;i<10;i++)
{
cout<<"number "<<i+1<<" :";
cin>>numbers[i];
if(numbers[i]%2==0)
{
even=even+1;
}
else
{
odd=odd+1;
}

}
cout<<"Total number of even numbers are: "<<even<<endl;
cout<<"Total number of odd numbers are: "<<odd;
}

Write a program of countdown and display blast.

#include<iostream>
using namespace std;
int main()
{
for(int i=10;i>=0;i--)
{
cout<<i<<endl;
}
cout<<"Blast!";
}

Produce the following triangle type output where the number of rows of the triangle are entered by the user.




#include<iostream>
using namespace std;
int main()
{
//The user can get as many rows of this triangle as he wants
//For n>9, one should use '\t' instead of " " to see that the algorithm works perfectly
int n;
cout<<"Enter the number of rows of such a triangle you want: ";
cin>>n;
for(int r=1;r<=n;r++)
{
for(int space1=2*n-2;space1>=2*r-1;space1--)
{
cout<<" ";
}
for(int c1=1;c1<=r;c1++)
{
cout<<c1<<" ";
}
if(r==1)
{
goto end;
}
else
{
for(int c2=r-1;c2>=1;c2--)
{
cout<<c2<<" ";
}
}
end:
cout<<endl;
}
}

Display all palindrome numbers from 1 to 1000.

#include<iostream>
using namespace std;
int main()
{
/* we will divide finding the palindromes based on the number of digits(n) a number has*/
//Case 1: n=1 ;All 1 digit numbers are palindromes
cout<<"The palindromes from 1 to 1000 are: \n ";
for(int i=1;i<=9;i++)
{
cout<<i<<"\t";
}
cout<<endl;
//Case 2: n=2
for(int j=10;j<100;j++)
{
int num1=j%10;
int num2=j/10;
if(num1==num2)
{
cout<<j<<"\t";
}
}
cout<<endl;
//Case3: n=3
for(int k=100;k<1000;k++)
{
int n1=k%10;
int n2=k/100;
if(n1==n2)
{
cout<<k<<"\t";
}
}
}

Print the sum of first n natural numbers where n is the number entered by the user.

#include<iostream>
using namespace std;
int main()
{
long int sum=0;
int n;
cout<<"Enter the number upto which you want to find the sum: ";
cin>>n;
for(int num=1;num<=n;num++)
{
sum=sum+num;
}
cout<<"The sum of first "<<n<<" natural numbers is "<<sum;
}

Print the sum of first 100 natural numbers.

#include<iostream>
using namespace std;
int main()
{
int sum=0;
for(int num=1;num<=100;num++)
{
sum=sum+num;
}
cout<<"The sum of first 100 natural numbers is: "<<sum;
}

Enter any number and display the month of the year using switch case.

#include<iostream>
using namespace std;
int main()
{
int month;
cout<<"Enter the month of the year you want to check: ";
cin>>month;
switch(month)
{
case 1:
cout<<"Month 1 is January";
break;
case 2:
cout<<"Month 2 is February";
break;
case 3:
cout<<"Month 3 is March";
break;
case 4:
cout<<"Month 4 is April";
break;
case 5:
cout<<"Month 5 is May";
break;
case 6:
cout<<"Month 6 is June";
break;
case 7:
cout<<"Month 7 is July";
break;
case 8:
cout<<"Month 8 is August";
break;
case 9:
cout<<"Month 9 is September";
break;
case 10:
cout<<"Month 10 is October";
break;
case 11:
cout<<"Month 11 is November";
break;
case 12:
cout<<"Month 12 is December";
break;
default:
cout<<"Invalid Entry.";
}
}