1. Any integer is input by the user. Write a program to find out

1.
Any integer is input by the user. Write a program to find out whether it is an odd
number or even number.
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter any number : ";
cin>>a;
if(a%2==0)
cout<<"The number is even";
else
cout<<"The number is odd";
return 0;
}
2.
Write a program to calculate the total expenses. Quantity and price per item are
input by the user and discount of 10% is offered if the expense is more than 5000.
#include<iostream>
using namespace std;
int main()
{
int totalexp, qty, price, discount;
cout<<"Enter quantity:";
cin>>qty;
cout<<"Enter price:";
cin>>price;
totalexp=qty*price;
if(totalexp>5000)
{
discount=(totalexp*0.1);
totalexp=totalexp-discount;
}
cout<<"Total Expense is
Rs. "<<totalexp;
return 0;
}
3.
Any year is input by the user. Write a program to determine whether the year is a
leap year or not.
#include<iostream>
using namespace std;
int main()
{
int year;
cout<<"Enter the year : ";
cin>>year;
if( (year%400==0 || year%100!=0) &&(year%4==0))
cout<<"It is a leap year";
else
cout<<"It is not a leap year";
4.
return 0;
}
In a company an employee is paid as under:
If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA =
90% of basic salary.If his salary is either equal to or above Rs. 1500, then HRA =
Rs. 500 and DA = 98% of basic salary. If the employee's salary is input by the user
write a program to find his gross salary.
#include<iostream>
using namespace std;
int main()
{
float basic_salary, gross_salary, HRA, DA;
cout<<"Enter basic salary of Employee : ";
cin>>basic_salary;
if (basic_salary<1500)
{
HRA=0.1*basic_salary;
DA=0.9*basic_salary;
}
else
{
HRA=500;
DA=0.98*basic_salary;
}
gross_salary=basic_salary+HRA+DA;
cout<<"Gross salary is : "<<gross_salary;
5.
return 0;
}
Write a program to calculate the monthly telephone bills as per the following rule:
Minimum Rs. 200 for upto 100 calls.
Plus Rs. 0.60 per call for next 50 calls.
Plus Rs. 0.50 per call for next 50 calls.
Plus Rs. 0.40 per call for any call beyond 200 calls.
#include<iostream>
using namespace std;
int main()
{
int calls;
float bill;
cout<<"Enter number of calls : ";
cin>>calls;
if(calls<=100)
bill=200;
else if (calls>100 && calls<=150)
{
calls=calls-100;
bill=200+(0.60*calls);
}
else if (calls>150 && calls<=200)
{
calls=calls-150;
bill=200+(0.60*50)+(0.50*calls);
}
else
{
calls=calls-200;
bill=200+(0.60*50)+(0.50*50)+(0.40*calls);
}
cout<<" Your bill is Rs."<<bill;
return 0;
}
6.
Write a program to print number from 1 to 10.
#include<iostream>
using namespace std;
int main()
{
int i=1;
while(i<=10)
{
cout<<i<<"\n";
i++;
}
return 0;
}
7.
Write a program to calculate the sum of first 10 natural number.
#include<iostream>
using namespace std;
int main()
{
int i=1,sum=0;
while(i<=10)
{
sum+=i;
i++;
}
cout<<"Sum :"<<sum;
return 0;
8.
}
Two numbers are entered through the keyboard. Write a program to find the value
of one number raised to the power of another.
#include<iostream>
using namespace std;
int main()
{
int n,p,r=1;
cout<<"Enter the base number and exponent ";
cin>>n>>p;
for(int i=1;i<=p;i++)
r=r*n;
cout<<"Result :"<<r;
return 0;
}
9.
Write a program to reverse any given integer number.
#include<iostream>
using namespace std;
int main()
{
int n,t,r,rev=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
t=t/10;
rev=rev*10+r;
}
cout<<"Reverse of number "<<n<<" is "<<rev;
return 0;
}
10. Write a program to sum of digits of given integer number.
#include<iostream>
using namespace std;
int main()
{
int n,t,r,sum=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
sum+=r;
t=t/10;
}
cout<<"Sum of digits of number "<<n<<" is "<<sum;
return 0;
}
11. Write a program to check given number is prime or not.
#include<iostream>
using namespace std;
int main()
{
int n;
bool flag=false;
cout<<"Enter any number : ";
cin>>n;
for(int i=2;i<n;i++)
{
if(n%i==0)
{
flag=true;
break;
}
}
if(flag==false && n>1)
cout<<"Number is prime";
else
cout<<"Number is not prime";
return 0;
}
12. Write a program to calculate HCF of Two given number.
#include<iostream>
using namespace std;
int main()
{
int dividend, divisor, rem, hcf;
cout<<"Enter two numbers : ";
cin>>dividend>>divisor;
while(rem!=0)
{
rem=dividend%divisor;
if(rem==0)
hcf=divisor;
else
{
dividend=divisor;
divisor=rem;
}
}
cout<<"HCF is : "<<hcf;
return 0;
}
13. Write a program to print all Armstrong numbers between 1 and 500. If sum of cubes
of each digit of the number is equal to the number itself, then the number is called
an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
#include<iostream>
using namespace std;
int main()
{
int n,digit1,digit2,digit3;
for(int i=1;i<=500;i++)
{
digit1=i/100;
digit2=i/10 - digit1*10;
digit3=i%10;
if(digit1*digit1*digit1 + digit2*digit2*digit2 +
digit3*digit3*digit3 == i)
cout<<i<<endl;
}
return 0;
}
14. Write a program to print Fibonacci series of n terms where n is input by user
0 1 1 2 3 5 8 13 24 .....
#include<iostream>
using namespace std;
int main()
{
int f=0,s=1,t,n;
cout<<"Enter Number of terms of Series : ";
cin>>n;
cout<<f<<" "<<s<<" ";
for(int i=3;i<=n;i++)
{
t=f+s;
cout<<t<<" ";
f=s;
s=t;
}
return 0;
}
15. Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.
#include<iostream>
using namespace std;
int main()
{
int i,n,sign=-1;
float sum=0;
cout<<"Enter the value of n ";
cin>>n;
for(i=1;i<=n;i++)
{
sign *= -1;
sum += sign*1.0/i;
}
cout<<"log 2 : "<<sum;
return 0;
}
16. Write C++ program to print following pattern:
i)
**********
**********
**********
**********
ii)
*
**
***
****
*****
iii)
*
**
***
****
*****
iv)
*
***
*****
*******
*********
v)
1
222
33333
4444444
555555555
vi)
1
212
32123
4321234
543212345
//Solution of (i)
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=10;j++)
cout<<'*';
cout<<endl;
}
return 0;
}
//Solution of (ii)
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
cout<<'*';
cout<<endl;
}
return 0;
}
//Solution of (iii)
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<=i;k++)
cout<<'*';
cout<<endl;
}
return 0;
}
//Solution of (iv)
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<2*i;k++)
cout<<'*';
cout<<endl;
}
return 0;
}
//Solution of (v)
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<2*i;k++)
cout<<i;
cout<<endl;
}
return 0;
}
//Solution of (vi)
#include<iostream>
using namespace std;
int main()
{
int i,j,k,l;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=i;k>=1;k--)
cout<<k;
for(l=2;l<=i;l++)
cout<<l;
cout<<endl;
}
return 0;
}
17
Write a program to compute sinx for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the computation
should use all terms in the series up through the term involving x
sin x = x - x3/3! + x5/5! - x7/7!+x9/9! .........
#include<iostream>
using namespace std;
int main()
{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
for(i=1;i<=n;i+=2)
{
fact=1;
for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum+=sign*p/fact;
}
cout<<"sin "<<x<<"="<<sum;
return 0;
}