0% found this document useful (0 votes)
24 views

Conditional Programs

The document contains solutions to 19 questions involving conditional programs and logic in C++. Each question contains code to solve a programming problem involving input from the user and conditional checks on the input to determine output. The questions cover topics like finding maximum of numbers, checking positive/negative values, determining odd/even, leap years, and profit/loss calculations.

Uploaded by

Abrash Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Conditional Programs

The document contains solutions to 19 questions involving conditional programs and logic in C++. Each question contains code to solve a programming problem involving input from the user and conditional checks on the input to determine output. The questions cover topics like finding maximum of numbers, checking positive/negative values, determining odd/even, leap years, and profit/loss calculations.

Uploaded by

Abrash Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CONDITIONAL PROGRAMS SOLUTION

MADE BY ALI AKBER

BSCS IST SELF

QNO1: Write a program that asks the user to input 2 numbers now determine
which number is greater and print the result.

#include<iostream>

#include<math.h>

using namespace std;

int main()

int n1,n2;

cout<<"enter number 01"<<endl;

cin>>n1;

cout<<"enter number 02"<<endl;

cin>>n2;

int Max=n1;

if(Max<n2)

Max=n2;

cout<<"Max is"<<Max<<endl;

return 0 ;

}
QNO2: Write a program that asks the user to input 3 numbers now determine
which number is greater and print the result.

#include<iostream>

#include<math.h>

using namespace std;

int main()

int n1,n2,n3;

cout<<"enter number 01"<<endl;

cin>>n1;

cout<<"enter number 02"<<endl;

cin>>n2;

cout<<"enter number 03"<<endl;

cin>>n3;

int Max=n1;

if(Max<n2)

Max=n2;

if (Max<n3)

Max=n3;

cout<<"Max is"<<Max<<endl;

return 0 ;
}

QNO3: Write a program that accepts a value from the user, program find the
absolute value (Positive) of a number entered by the user.

#include<iostream>

#include<math.h>

using namespace std;

int main()

int a;

cout<<"enter a value"<<endl;

cin>>a;

if (a>0)

cout<<"the absolute value of number is"<<a<<endl;

else

cout<<"the absolute value of number is"<<-(a);

return 0 ;

QNO4:Write a program that asks the user to input time if user enters negative
time the program should output/print an error and exit.

#include<iostream>
#include<math.h>

using namespace std;

int main()

int time;

cout<<"enter time"<<endl;

cin>>time;

if (time>0)

cout<<"time is"<<time;

else

cout<<"error and exit";

return 0 ;

QNO5: Write a program that inputs 2 numbers from the user and display
message if 2nd number is square of 1 no.

#include<iostream>

#include<math.h>

using namespace std;

int main()

int n1,n2;

cout<<"enter number 01"<<endl;


cin>>n1;

cout<<"enter number 02"<<endl;

cin>>n2;

if (n1*n1==n2)

cout<<"2nd number is square of number 01"<<endl;

return 0 ;

QNO6: Write a program to calculate the total expenses. Quantity and price
per item are input by the user and a discount of 10% is offered if the expense
is more than 5000.

#include<iostream>

#include<math.h>

using namespace std;

int main()

int a,b,t,td,tp;

float d;

d=0.10;

cout<<"enter the quantity of books"<<endl;

cin>>a;

cout<<"enter price per item"<<endl;

cin>>b;

t=a*b;

td=t*d;
tp=t-td;

if (t>5000)

cout<<"total expenses are"<<t<<endl;

cout<<"total expenses after 10%discount"<<td<<endl;

cout<<"total price is"<<tp<<endl;

return 0 ;

QNO7: Write a program that inputs the name and 3 subject's marks from the
user, and calculate the marks average if the average is above 80 then the
program display message you are outstanding.

#include<iostream>

#include<math.h>

using namespace std;

int main()

int sub, totalmarks,marks;

float average;

cout<<"enter subject marks"<<endl;

cin>>marks;

average=marks%100;

if (marks>80)

cout<<"You are outstanding"<<endl;


return 0 ;

QNO8: Write a program that inputs a year from the user and displays the
entered year is a Leap year or not.

#include<iostream>

#include<math.h>

using namespace std;

int main()

int year;

cout<<"enter a year"<<endl;

cin>>year;

if (year%4==0)

cout<<year<<" is leap year"<<endl;

else

cout<<year<<"is not a leap year"<<endl;

return 0 ;

QNO9: Any integer is input by the user. Write a program to find out whether
it is an odd number or even number.
#include<iostream>

#include<math.h>

using namespace std;

int main()

int numb,odd,even;

cout<<"enter a number"<<endl;

cin>>numb;

if (numb%2 == 0)

cout<<numb<<"numb is even"<<endl;

else

cout<<numb<<"numb is odd"<<endl;

return 0 ;

QNO10: Write a C++ program to check whether a number is divisible by 5 and


11 or not.

#include<iostream>

#include<math.h>

using namespace std;

int main()

int num;

cout<<"enter a number"<<endl;
cin>>num;

if (num%5==0 and num%11==0)

cout<<num<<"number is divisible by 5 and 11"<<endl;

else

cout<<num<<"number is not divisible by 5 and 11"<<endl;

return 0 ;

QNO11: Write a program that inputs the name age of three persons, If the
ages of Ali, Kashif and Aslam are input by the user than determine and
display the youngest of the them.

#include<iostream>

#include<math.h>

using namespace std;

int main()

int Ali, Aslam,Kashif;

cout<<"enter age of Ali"<<endl;

cin>>Ali;

cout <<"enter age of Kashif"<<endl;

cin>>Kashif;

cout<<"enter age of Aslam"<<endl;

cin>>Aslam;

if(Ali>0 and Aslam>0 and Kashif>0)

{
if (Ali<Aslam and Ali<Kashif)

cout<<"Ali is the youngest"<<endl;

if(Ali>Aslam and Aslam<Kashif)

cout<<"Aslam is the youngest"<<endl;

if(Kashif<Ali and Kashif<Aslam)

cout<<"Kashif is the youngest"<<endl;

}else is

cout<<"Error age invalid"<<endl;

return 0 ;

QNO12: Program to check whether a given number is Armstrong number or


not.

For example, 371 is an Armstrong number, since 3 x 3 +7x3+1x3=371.

#include<iostream>

#include<stdlib.h>

using namespace std;

int main()

int no;

cout<<"Enter No ";

cin>>no;
int d1, d2, d3;

d1 = no;

d3 = d1%10; ///1

d1 = d1/10; /// 37

d2 = d1%10; ///7

d1 = d1/10; /// 3

if(d1*d1*d1 + d2*d2*d2 + d3*d3*d3 == no)

cout<<no<<" is Armstrong number"<<endl;

else

cout<<no<<" is not Armstrong number"<<endl;

return 0;

QNO13:

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>

using namespace std;

int main()

{
float angle1,angle2, angle3;

cout<<"enter angle 01 of triangle"<<endl;

cin>>angle1;

cout<<"enter angle 02 of triangle"<<endl;

cin>>angle2;

cout<<"enter angle 03 of triangle"<<endl;

cin>>angle3;

if (angle1+angle2+angle3 ==180)

cout<<"given triangle is valid"<<endl;

else

cout<<"given triangle is not valid"<<endl;

return 0;

QNO14:

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>

using namespace std;

int main()
{

float costprice, sellprice;

cout<<"enter cost price of the item"<<endl;

cin>>costprice;

cout<<"enter sell price of the item"<<endl;

cin>>sellprice;

float profit,loss;

profit=sellprice-costprice;

loss=costprice-sellprice;

if (costprice>sellprice)

cout<<"you are in the loss of:"<<loss<<"Rs/"<<endl;

else

cout<<"you are in the profit of:"<<profit<<"Rs/"<<endl;

return 0;

QNO15: Write a program that inputs salary and grade. It adds 70% bonus if
the grade is greater than 17.it adds 25% bonus if the grade is 17 or less and
then display the total salary.

#include<iostream>

using namespace std;


int main()

float salary,grade,bonus, totalsalary;

cout<<"enter salary of the person"<<endl;

cin>>salary;

cout<<"enter grade of the person"<<endl;

cin>>grade;

totalsalary=salary+bonus;

if (grade>17)

bonus=salary*(70/100.0f);

if (grade<17)

bonus=salary*(25/100.0f);

totalsalary=salary+bonus;

cout<<"total salary of the person is"<<totalsalary<<endl;

return 0;

QNO16: Write a C program to input basic salary of an employee and calculate


its Gross salary according to following:

⚫ Basic Salary <= 10000 HRA 20%, DA = 80%


⚫ Basic Salary <= 20000: HRA 25%, DA = 90%

⚫ Basic Salary> 20000: HRA 30%, DA = 95%

#include<iostream>

using namespace std;

int main()

int basicsalry,grosssalry;

cout<<"input salary of the employee"<<endl;

cin>>basicsalry;

char hra,da;

if (basicsalry<=10000)

{hra=basicsalry*0.2;

da=basicsalry*0.8;}

if (basicsalry<=20000)

{hra=basicsalry*0.25;

da=basicsalry*0.90;}

else

hra=basicsalry*0.30;

basicsalry*0.95;

grosssalry=basicsalry+hra+da;

cout<<"gross salary is"<<grosssalry<<endl;

return 0;

}
QNO17: Write a C program to input any alphabet and check whether it is
vowel or consonant.

#include<iostream>

using namespace std;

int main()

char ch,v,cons;

cout<<"enter any alphabet"<<endl;

cin>>ch;

if (ch=='a'||ch=='e'|| ch=='i'||ch=='o'||ch=='u' ||ch=='A' ||ch=='E' ||ch=='I'||ch=='O'|| ch=='U')

cout<<"given alphabet is vowel"<<endl;

else

cout<<"given alphabet is consonant"<<endl;

return 0;

QNO18: 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,bill;

cout<<" enter calls"<<endl;

cin>>calls;

if (calls>=0 && calls<=100)

bill= 200;

if (calls>=100 && calls<=150)

bill=200+(calls-100)*0.60;

if (calls>=151 && calls<=200)

bill=200+0.60*50+(calls-150)*0.50;

else

bill=200+0.60*50+0.50*50+(calls-120)*0.40;

cout<<"total telephone bill is"<<bill<<endl;

return 0;

}
QNO19: Write a program that determines a student's grade. The program
will read three types of scores (quiz, mid-term, and final scores) and
determine the grade based on the following rules:

⚫ if the average score =90% =>grade=A

⚫ if the average score >= 70% and <90% => grade=B

⚫ if the average score>=50% and <70% =>grade=C

⚫ if the average score<50% =>grade=F

#include<iostream>

using namespace std;

int main()

float grade,quiz,mids,finals, avg;

cout<<" enter marks in quiz"<<endl;

cin>>quiz;

cout<<" enter marks in mids"<<endl;

cin>>mids;

cout<<" enter marks in finals"<<endl;

cin>>finals;

avg=(quiz+mids+finals)/3.0f;

if (avg>=90)

cout<<"You got an A grade"<<endl;

if(avg>=70 && avg<90)

cout<<"You got an B grade"<<endl;

if (avg>=50 && avg<70)


cout<<"You got an C grade"<<endl;

if (avg<50)

cout<<"You got an F grade"<<endl;

cout<<"average score of student is"<<avg<<endl;

return 0;

QNO20: 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

#include<iostream>

using namespace std;

int main()

int eng,maths, ict, phy, chem;

float percentage;

cout<<" enter marks in English"<<endl;

cin>>eng;

cout<<" enter marks in maths"<<endl;


cin>>maths;

cout<<" enter marks in ict"<<endl;

cin>>ict;

cout<<" enter marks in physics"<<endl;

cin>>phy;

cout<<" enter marks in chemistry"<<endl;

cin>>chem;

percentage = (eng+maths+ict+phy+chem)/500.0f*100;

cout<<"percentage is"<<percentage<<endl;

if (percentage>=60)

cout<<"You got First division"<<endl;

if (percentage>=50 && percentage<=59)

cout<<"You got 2nd division"<<endl;

if (percentage>=40 && percentage<49)

cout<<"You got 3rd division"<<endl;

if (percentage<40)

cout<<"You failed the exam"<<endl;

return 0;

QNO21: Write a program that calculates the electricity bill. The rate of
electricity per unit as follows.

• If the units consumed are <=100, then the cost is Rs. 5.5/- per unit. • If the
units consumed are <100 and <=250, then the cost is Rs. 7.5/- per unit.

⚫ If the units consumed exceed <250, then the cost is Rs. 10.5/- per unit.
A line rent Rs. 50 is also added to the bill and surcharge of 7% extra if the bill
exceeded Rs.1500/-.calculate the total bill.

#include<iostream>

using namespace std;

int main()

float bill,totbill,units,linerent=50;

cout<<"enter electricity units"<<endl;

cin>>units;

if (units<=100)

bill=units*5.5;

if (units>100 && units<250)

{bill=units*7.5;

if (units>250)

{bill=units*10.5;

if (bill>1500)

{totbill=bill+linerent+(7/100.0f);

cout<<"total electricity bill is"<<totbill;

}
else

totbill=bill;

cout<<" total bill is"<<bill<<endl;

return 0;

QNO22: Write a program that displays the following menu for a parking area.

• Enter B for Bike •

• Enter C for car

Enter T for truck

The program inputs the type of vehicle and number of days to park the
vehicle. It finally displays the total charges for the parking according to the
following.

⚫ Bike Rs. 300/- per day

⚫ Car Rs. 600/- per day

Truck Rs. 900/- per day

#include<iostream>

using namespace std;

int main()

char ch;

int days,rent;

cout<<"Enter the type of vehicle";

cin>>ch;

cout<<"Enter Days you want to park the car";

cin>>days;
if(ch=='B')

rent=days*300;

cout<<"your rent for vehicle is"<<rent<<endl;

else if(ch=='C')

rent=days*600;

cout<<"your rent for vehicle is"<<rent<<endl;

else if(ch=='T')

rent=days*900;

cout<<"your rent for vehicle is"<<rent<<endl;

return 0;

QNO23: 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.


#include<iostream>

using namespace std;

int main()

char ch;

cout<<"enter any character"<<endl;

cin>>ch;

if (ch>=65 && ch<=90)

cout<<"character is capital letter"<<endl;

else if (ch>=97 && ch<=122)

cout<<"character is small letter"<<endl;

else if (ch>=48 && ch<=57)

cout<<"character is a digit"<<endl;

else if ((ch>0 && ch<=47)||(ch>=58 && ch<=64) ||

(ch>=91 && ch<=96) || (ch>=91 && ch<=96) || (ch>=123 && ch<=127))

cout<<"character is special symbols"<<endl;

return 0;

}
THE END.

You might also like