"/nenter Temperature in Farenheit: ": #Include #Include
"/nenter Temperature in Farenheit: ": #Include #Include
PRACTICE SET-1
Q1: - Write a program which accept temperature in Farenheit and print it in centigrade
Solution: -
#include<iostream.h>
#include<conio.h>
void main()
{
float F,C;
cout<< "\nEnter temperature in Farenheit : ";
cin>>F;
C=5*(F-32)/9;
cout<<"Temperature in celcius is : "<<C;
getch();
}
Q2: - Write a program which accept principle, rate and time from user and print the simple interest
#include<iostream.h>
#include<conio.h>
void main()
{
int p,r,t,i;
cout<<"Enter Principle : ";
cin>>p;
cout<<"Enter Rate : ";
cin>>r;
cout<<"Enter Time : ";
cin>>t;
i=(p*r*t)/100;
cout<<"Simple interest is : "<<i;
getch();
}
Q3: - Write a program which accepts a character and display its ASCII value.
#include<iostream.h>
#include<conio.h>
void main()
{ char ch;
cout<< "\nEnter any character : ";
cin>>ch;
cout<<"ASCII equivalent is : "<<(int)ch;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,temp;
cout<<"\nEnter two numbers : ";
cin>>a>>b;
temp=a;
a=b;
b=temp;
cout<<"\nAfter swapping numbers are : ";
cout<<a<<" "<<b;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
float r,area;
cout<< "\nEnter radius of circle : ";
cin>>r;
area = 3.14*r*r;
cout<<"Area of circle : "<<area;
getch();
}
Q6: -Write a program to check whether the given number is positive or negative (using ? : ternary operator ).
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
cout<<"Enter any non-zero Number : ";
cin>>a;
(a>0)?cout<<"Number is positive":cout<<"Number is negative";
getch();
}
Q7: - Write a program to check whether the given number is even or odd (using ? : ternary operator ).
#include<iostream.h>
#include<conio.h>
void main()
{ int a;
cout<<"Enter the Number : ";
cin>>a;
(a%2==0)?cout<<"Number is even":cout<<"Number is odd";
getch();
}
Q8: - Write a program to swap value of two variables without using third variable.
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
cout<<"\nEnter two numbers : ";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\nAfter swapping numbers are : ";
cout<<a<<" "<<b;
getch();
}
Q9: -Write a program which input three numbers and display the largest number using ternary operator.
#include <iostream.h>
#include <conio.h>
void main()
{
int a,b,c,greatest;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
greatest=(a>b&&a>c)?a:(b>c)?b : c;
cout<<"Greatest number is "<<greatest;
getch();
}
By: - Mr. S.U. KHAN, PGT(COMP) from KV, CRPF Mokamaghat
4
5
Q10: - Write a program which accepts amount as integer and display total number of Notes of Rs. 500, 100, 50, 20,
10, 5 and 1.
For example, when user enter a number, 575,
the results would be like this... 500: 1
100: 0
50: 1
20: 1
10: 0
5: 1
1: 0
#include<iostream.h>
#include<conio.h>
void main()
{
int amt,R500,R100,R50,R20,R10,R5,R1;
cout<<"Enter amount : ";
cin>>amt;
R500=amt/500;
amt=amt%500;
R100=amt/100;
amt=amt%100;
R50=amt/50;
amt=amt%50;
R20=amt/20;
amt=amt%20;
R10=amt/10;
amt=amt%10;
R5=amt/5;
amt=amt%5;
R1=amt;
cout<<"Rs.500 : "<<R500<<"\nRs.100 : "<<R100<<"\nRs. 50 : "<<R50<<
"\nRs. 20 : "<<R20<<"\nRs. 10 : "<<R10<<"\nRs. 5 : "<<R5<<"\nRe. 1 : "<<R1;
getch();
}
Q11: - Write a program which accepts a character and display its next character.
#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
cout<< "\nEnter any character : ";
cin>>ch;
ch++;
cout<<"Next character is : "<<ch;
getch();
}
Q12: - Write a program which accepts days as integer and display total number of years, months and days in
it.
for example : If user input as 856 days the output should be 2 years 4 months 6 days.
#include<iostream.h>
#include<conio.h>
void main()
{ int days,y,m,d;
cout<<"Enter no. of days : ";
cin>>days;
y=days/365;
days=days%365;
m=days/30;
d=days%30;
cout<<"Years : "<<y<<"\nMonths : "<<m<<"\nDays : "<<d;
getch();
}
Q13: - Any integer is input by the user. Write a program to find out whether it is an odd number or even
number.
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
cout<<"Enter any number : ";
cin>>a;
if(a%2==0)
cout<<"The number is even";
else
cout<<"The number is odd";
getch();
}
Q14: - Find the absolute value of a number entered by the user.
#include<iostream.h>
#include<conio.h>
int main()
{
int a;
cout<<"Enter any number:";
cin>>a;
if(a>0)
cout<<"The absolute value of number is:"<<a;
else
cout<<"The absolute value of number is:"<<-(a);
getch();
}
Q15: -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.h>
#include<conio.h>
void 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;
getch();
}
Q16: - Write a program to determine whether the seller has made profit or incurred loss. Also determine
how much profit he made or loss he incurred. Cost price and selling price of an item is input by the
user.
#include<iostream.h>
#include<conio.h>
void main()
{
int cp,sp,result;
cout<<"Enter cost price of item : ";
cin>>cp;
cout<<"Enter selling price of item : ";
cin>>sp;
result=sp-cp;
if(result>0)
cout<<"Profit : "<<result;
else
if(result<0)
cout<<"Loss : "<<-(result);
else
cout<<"No profit no loss";
getch();
}
Q17:- If the ages of Ram, Sulabh and Ajay are input by the user, write a program to determine the youngest of
the three.
#include<iostream.h>
#include<conio.h>
void main()
{
int ram_age,sulabh_age,ajay_age;
cout<<"Enter Ram age:";
cin>>ram_age;
cout<<"Enter Sulabh age:";
cin>>sulabh_age;
cout<<"Enter Ajay age:";
cin>>ajay_age;
if (ram_age<sulabh_age && ram_age<ajay_age)
cout<<"Ram is youngest";
else if(sulabh_age<ram_age && sulabh_age<ajay_age)
cout<<"Sulabh is youngest";
else
cout<<"Ajay is youngest";
getch();
}
Q18: - Write a program to check whether a triangle is valid or not, when the three angles of the triangle are
entered
by the user. A triangle is valid if the sum of all the three angles is equal to 180 degrees.
#include<iostream.h>
#include<conio.h>
void main()
{
int angle1,angle2,angle3;
cout<<"Enter the three angles of triangle:";
cin>>angle1>>angle2>>angle3;
if (angle1+angle2+angle3==180)
cout<<"Triangle is valid";
else
cout<<"Triangle is not valid";
getch();
}
Q19: - Any year is input by the user. Write a program to determine whether the year is a leap year or not.
#include<iostream.h>
#include<conio.h>
void 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";
getch();
}
Q20: - 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.h>
#include<conio.h>
void 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;
Q22:- Write a program to find the roots of and quadratic equation of type ax2+bx+c where a is not equal to
zero.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,root1,root2;
cout<<"Enter value of a, b and c : ";
cin>>a>>b>>c;
d=b*b-4*a*c;
if(d==0)
{
root1=(-b)/(2*a);
root2=root1;
cout<<"Roots are real & equal";
}
else if(d>0)
{
root1=-(b+sqrt(d))/(2*a);
root2=-(b-sqrt(d))/(2*a);
cout<<"Roots are real & distinct";
}
else
{
root1=(-b)/(2*a);
root2=sqrt(-d)/(2*a);
cout<<"Roots are imaginary";
}
cout<<"\nRoot 1= "<<root1<<"\nRoot 2= "<<root2;
getch();
}
Q23: - The marks obtained by a student in 5 different subjects are input by the user. The student gets a
division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student.
#include<iostream.h>
#include<conio.h>
void main()
{
int sub1,sub2,sub3,sub4,sub5,percentage;
cout<<"Enter marks of five subjects : ";
cin>>sub1>>sub2>>sub3>>sub4>>sub5;
percentage=(sub1+sub2+sub3+sub4+sub5)/5;
if(percentage>=60)
cout<<"Ist division";
else if(percentage>=50)
cout<<"IInd division";
else if(percentage>=40)
cout<<"IIIrd division";
else
cout<<"Fail" ;
getch();
}
Q24:- Any character is entered by the user; write a program to determine whether the character entered is a
capital
letter, a small case letter, a digit or a special symbol. The following table shows the range of ASCII
values for
various characters.
Characters ASCII Values
A–Z 65 – 90
a–z 97 – 122
0–9 48 – 57
special symbols 0 - 47, 58 - 64, 91 - 96, 123 – 127
#include<iostream.h>
#include<conio.h>
void main ()
{
char ch;
cout<<"Enter any character:";
cin>>ch;
if (ch>=65 && ch<=90)
cout<<"Character is a capital letter";
else if (ch>=97 && ch<=122)
cout<<"Character is a small letter";
else if (ch>=48 && ch<=57)
cout<<"Character is a digit";
else if ((ch>0 && ch<=47)||(ch>=58 && ch<=64)||(ch>=91 && ch<=96)||(ch>=123 && ch<=127))
cout<<"Character is a special symbol";
getch();
}