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.";
}
}

Enter any number and display the day of the week using switch case.

#include<iostream>
using namespace std;
int main()
{
int day;
cout<<"Enter the day of the week: ";
cin>>day;
switch(day)
{
case 1:
cout<<"Day is Monday";
break;
case 2:
cout<<"Day is Tuesday";
break;
case 3:
cout<<"Day is Wednesday";
break;
case 4:
cout<<"Day is Thursday";
break;
case 5:
cout<<"Day is Friday";
break;
case 6:
cout<<"Day is Saturday";
break;
case 7:
cout<<"Day is Sunday";
break;
default:
cout<<"Invalid Choice";
}

Write a program to accept the book name and selling price and find out the discount on selling price. If selling price is above 1000 discount is of 20% and if selling price is between 800-1000 discount is 15% and if selling price is less than 500 no discount is given.

#include<iostream>
using namespace std;
int main()
{
char name[25];
cout<<"Enter the name of the book- ";
cin>>name;
float price;
cout<<"Enter the price of the book- ";
cin>>price;
if(price>=1000)
{
cout<<"The discount on the book "<<name<<"is 20%";
}
if(price<1000 && price>=800)
{
cout<<"The discount on the book "<<name<<"is 15%";
}
if(price<800 && price>=500)
{
cout<<"Our sir did not mention how much discount has to be given on the book "<<name<<"!!!!";
}
if(price<500)
{
cout<<"No discount is given on the book "<<name;
}

}

Accept total sales and check if the sales is greater than 5000 commission is 12.5% and if sales is between 2000 and 5000 than the commission is 12% and if the sales is between 500 and 2000 than commission should be 10% ,otherwise no commission

#include<iostream>
using namespace std;
int main()
{
float sales;
cout<<"Enter your total sales: ";
cin>>sales;
if(sales>=5000)
{
cout<<"Your commission is 12.5%.";
}
if(sales<5000 && sales>=2000)
{
cout<<"Your commission is 12%.";
}
if(sales<2000 && sales>=500)
{
cout<<"Your commission is 10%.";
}
if(sales<500)
{
cout<<"You dont get any commission.";
}
}

Write a program to reverse a number.

#include<iostream>
using namespace std;
int main()
{
long int a,num,div,mod;
a=0;
cout<<"Enter any positive integer: ";
cin>>num;
while(div!=0)
{
mod=num%10;
div=num/10;
num=num/10;
a=a*10+mod;
}
cout<<"The reverse of the integer is : "<<a;

}

Write a program to convert temperature from Fahrenheit to Celsius?

/*Formula of Celsius = (f-32)*5/9 ; Fahrenheit = 9/5*C+32*/
#include<iostream>
using namespace std;
int main()
{
double f,c;
cout<<"Enter the temperature in farhenheit: ";
cin>>f;
c=(f-32)*5/9;
cout<<"The temperature in celsius is: "<<c;
}

Accept age from the user and check the age.If the age is greater than 50 print OLD and if it is less then and equal to 50 it should print YOUNG.

#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"Please enter your age: ";
cin>>age;
if(age>50)
{
cout<<"OLD";
}
else if(age>=0 && age<=50)
{
cout<<"YOUNG";
}
else
{
cout<<"Age cannot be negative.";
}
}

Write a program to accept a number and display a message indicating whether it is a positive or a negative number.

#include<iostream>
using namespace std;
int main()
{
float num;
cout<<"Enter you number: ";
cin>>num;
if(num>0)
{
cout<<num<<" is a positive number.";
}
else if(num==0)
{
cout<<num<<" is neither positive nor negative.";
}
else
{
cout<<num<<" is a negative number.";
}
}

Write a program to accept a year and find out whether the year is leap year or not.

#include<iostream>
using namespace std;
int main()
{
int year;
cout<<"Enter the year you want to check: ";
cin>>year;
if(year%4==0)
{
if(year%100==0 && year%400!=0)
{
cout<<year<<" is NOT a leap year.";
}
if(year%100==0 && year%400==0)
{
cout<<year<<" is a leap year.";
}
if(year%100!=0)
{
cout<<year<<" is a leap year.";
}

}
else
{
cout<<year<<" is NOT a leap year.";
}
}

Write a program to accept a number and to find out whether the number is divisible by 5 or not.

#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number: ";
cin>>num;
if(num%5==0)
{
cout<<num<<" is divisible by 5.";
}
else
{
cout<<num<<" is NOT divisible by 5.";
}
}

Accept three numbers from a user and find the greatest of the three.

#include<iostream>
using namespace std;
int main()
{
float a,b,c;
cout<<"Enter the first number: ";
cin>>a;
cout<<"Enter the second number: ";
cin>>b;
cout<<"Enter the third number: ";
cin>>c;
if(a>=b)
{
if(a>=c)
{
cout<<a<<" is the biggest number.";
}
else
cout<<c<<" is the biggest number.";
}
else if(b>=c)
{
cout<<b<<" is the biggest number.";
}
else
{
cout<<c<<" is the biggest number.";
}
}

Accept two numbers from a user and find the greatest of the two.

#include<iostream>
using namespace std;
int main()
{
float a,b;   
cout<<"Enter the first number: ";
cin>>a;
cout<<"Enter the second number: ";
cin>>b;
if(a>b)
{
cout<<a<<" is bigger than "<<b;
}
if(a==b)
{
cout<<a<<" is equal to "<<b;
}
if(a<b)
{
cout<<b<<" is bigger than "<<a;
}
}

Enter any number and display the month of the year using simple if construct.

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

Enter any number and display the day of week using simple if construct.

#include<iostream>
using namespace std;
int main()
{
int day=0;
cout<<"Enter the day of the week you want to check: ";
cin>>day;
if(day==1)
{
cout<<"Day 1 is Monday";
}
if(day==2)
{
cout<<"Day 2 is Tuesday";
}
if(day==3)
{
cout<<"Day 3 is Wednesday";
}
if(day==4)
{
cout<<"Day 4 is Thursday";
}
if(day==5)
{
cout<<"Day 5 is Friday";
}
if(day==6)
{
cout<<"Day 6 is Saturday";
}
if(day==7)
{
cout<<"Day 7 is Sunday";
}
if(day!=1 && day!=2 && day!=3 && day!=4 && day!=5 && day!=6 && day!=7)
{
cout<<"Invalid choice";
}
}

Write a program to create function power and calculate the power of any number.

#include<iostream>
using namespace std;
int main()
{
int number,power;
double result=1;
cout<<"Enter the number whose power you want to calculate :";
cin>>number;
cout<<"Enter the power :";
cin>>power;
if((number==0)&&(power==0))
{
cout<<"0 raised to the power 0 is not defined";
}
else if(power==0)
{
cout<<"Result = 1"; //Anything raised to the power 0 is 1
}
else if (power>0)
{
for(int i=1;i<=power;i++)
{
result=result*number;
}
cout<<"Result = "<<result;
}
else
{
power= (-1)*power;
for(int i=1;i<=power;i++)
{
result=result*number;
}
cout<<"Result = "<<(1/result);
}
}

Write a Program to swap two numbers without using third variable.

#include<iostream>
using namespace std;
int main()
{
int num1,num2;
cout<<"Enter 1st number: ";
cin>>num1;
cout<<"Enter 2nd number: ";
cin>>num2;
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
cout<<"The 1st number is: "<<num1<<endl;
cout<<"The 2nd number is: "<<num2;
}

Write a Program to swap two numbers.

#include<iostream>
using namespace std;
int main()
{
int num1,num2,temp;
cout<<"Enter 1st number: ";
cin>>num1;
cout<<"Enter 2nd number: ";
cin>>num2;
temp=num1;
num1=num2;
num2=temp;
cout<<"1st number is : "<<num1<<endl;
cout<<"2nd number is : "<<num2;
}

Enter any 4 digit number and print the sum of its 4 digits.

#include<iostream>
using namespace std;
int main()
{
int num,a,b,c,d,e,f,sum;
cout<<"Please enter a 4 digit number :";
cin>>num;
a=num%10;
b=num/10;
c=b%10;
d=b/10;
e=d%10;
f=d/10;
sum=a+c+e+f;
cout<<"The sum of the digits is "<<sum;
}

Enter any 4 digit number and check if it is a palindrome.

#include<iostream>
using namespace std;
int main()
{
int num,a,b,c,d,e,f;
cout<<"Please enter a 4 digit number :";
cin>>num;
a=num%10;
b=num/10;
c=b%10;
d=b/10;
e=d%10;
f=d/10;
if((a==f)&&(c==e))
{
cout<<"The given number is a palindrome";
}
else
{
cout<<"The given number is NOT a palindrome";
}
}

Ram's basic salary is input through the keyboard. His HRA is 50% of basic salary and DA is 20 % of basic salary. Calculate the gross salary.

#include<iostream>
using namespace std;
int main()
{
float basic_sal,hra,da,gross_sal;
cout<<"Please enter your basic salary :";
cin>>basic_sal;
hra=(float)50/100*basic_sal;
da=(float)20/100*basic_sal;
gross_sal=basic_sal+hra+da;
cout<<"Your gross salary is: "<<gross_sal;
}

Accept five subject marks and print the average.

#include<iostream>
using namespace std;
int main()
{
float average_marks,sub1,sub2,sub3,sub4,sub5;
cout<<"Enter the marks obtained in subject 1 : ";
cin>>sub1;
cout<<"Enter the marks obtained in subject 2 : ";
cin>>sub2;
cout<<"Enter the marks obtained in subject 3 : ";
cin>>sub3;
cout<<"Enter the marks obtained in subject 4 : ";
cin>>sub4;
cout<<"Enter the marks obtained in subject 5 : ";
cin>>sub5;
average_marks=(sub1+sub2+sub3+sub4+sub5)/5;
cout<<"Your average marks are : "<<average_marks;
}

Write a Program to accept two numbers and print the sum of two numbers.

#include<iostream>
using namespace std;
int main()
{
float num1,num2,sum;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
sum = num1 + num2 ;
cout<<"The sum of the 2 numbers is :"<<sum;
}

Enter any character and change the case of that character.

#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter the character: ";
cin>>ch;
if(ch>=65 && ch<=90)
{
ch=ch+32;
cout<<ch;
}
else if(ch>=97 && ch<=122)
{
ch=ch-32;
cout<<ch;
}
else
{
cout<<"Invalid Input.";
}

}