0% found this document useful (0 votes)
32 views12 pages

SOLVED IF ELSE QUESTION by Adnan Rai

This document contains 15 questions that involve writing C++ programs using if/else conditional statements. Each question provides a programming problem to solve related to determining conditions, comparing values, or calculating outputs based on input values. The programs demonstrate the use of if/else statements to check conditions and return different outputs depending on the input values and conditions evaluated.

Uploaded by

Adnan Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views12 pages

SOLVED IF ELSE QUESTION by Adnan Rai

This document contains 15 questions that involve writing C++ programs using if/else conditional statements. Each question provides a programming problem to solve related to determining conditions, comparing values, or calculating outputs based on input values. The programs demonstrate the use of if/else statements to check conditions and return different outputs depending on the input values and conditions evaluated.

Uploaded by

Adnan Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

SOLVED IF ELSE QUESTION by Adnan

Rai
Warning: Try run every program at once. Some programs may have logic errors.
#include <iostream>

using namespace std;

Q2. Find the absolute value of a number entered by the user

...

int main(){

int a;

cout<<"Enter any number";

cin>>a;

if(a>0){

cout<<"The absolute value is: "<<a<<endl;}

else{

cout<<"The absolute value is: "<<-(a);

return 0;

...

Q2: Write a program to check whether a number is divisible by 5 and 11 or not

int main(){

int a;

cout<<"Enter anny num: ";

cin>>a;

if(a%5==0 && a%11==0){

cout<<a<<" is divisible by 5 and 11";


}

else{

cout<<a<<" is not divisible by 5 and 11";

...

Q4. 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.

...

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<<"10% of Discount: "<<discount<<endl;

cout<<"Total Expense is Rs. "<<totalexp;


return 0;

...

Q5. 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

...

int 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";

return 0;

}
Q6. If the ages of Ali, Kashif and Aslam are input by the user, write a program to determine the
youngest of the three

...

int main()

int Ali_age,Kashif_age,Aslam_age;

cout<<"Enter Ali age:";

cin>>Ali_age;

cout<<"Enter Kashif age:";

cin>>Kashif_age;

cout<<"Enter Aslam age:";

cin>>Aslam_age;

if (Ali_age<Kashif_age && Ali_age<Aslam_age)

cout<<"Ali is youngest";

else if(Kashif_age<Ali_age && Kashif_age<Aslam_age)

cout<<"Kashif is youngest";

else

cout<<"Aslam is youngest";

return 0;

...

Q7. 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.
int 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";

return 0;

...

Q8. Any year is input by the user. Write a program to determine whether the year is a leap year or
not.

...

int main(){

int y;

cout<<"Enter any year";

cin>>y;

if(y%4==0 || y%400==0){

cout<<"it is leap year";

else{

cout<<"it is not leap year";

}
return 0;

...

Q9. 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.

...

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;

return 0;

...

Q10. 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%

...

int main()

float basic, gross, da, hra;

cout<<"Enter basic salary of an employee: ";

scanf("%f", &basic);

if(basic <= 10000)

da = basic * 0.8;
hra = basic * 0.2;

else if(basic <= 20000)

da = basic * 0.9;

hra = basic * 0.25;

else

da = basic * 0.95;

hra = basic * 0.3;

gross = basic + hra + da;

cout<<"GROSS SALARY OF EMPLOYEE = %.2f", gross";

return 0;

...

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

...

int main()

char c;

int isLowercaseVowel, isUppercaseVowel;

cout << "Enter an alphabet: ";

cin >> c;
// evaluates to 1 (true) if c is a lowercase vowel

isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

// evaluates to 1 (true) if c is an uppercase vowel

isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

// evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true

if (isLowercaseVowel || isUppercaseVowel)

cout << c << " is a vowel.";

else

cout << c << " is a consonant.";

return 0;

...

Q12. 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.

...

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;

...

Q13. 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


...

int 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" ;

return 0;

...

Q15. 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


...

int 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";

return 0;

You might also like