You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<iostream>usingnamespacestd;intmain() {
int a;
cout << "Enter a number: ";
cin >> a;
if(a > 1) {
cout << "number is positive";
}
return0;
}
Output
> Enter a number: 10> Number is positive
2. check if input numbers is positive or negative
#include<iostream>usingnamespacestd;intmain() {
int a;
cout << "Enter a number: ";
cin >> a;
if(a > 1 && a != 0) {
cout << "number is positive";
} else {
cout << "number is negative";
}
return0;
}
Output
> Enter a number: -10> Number is negative
3. checks if the input number is positive, negative or zero
#include<iostream>usingnamespacestd;intmain() {
int a;
cout << "Enter a number: ";
cin >> a;
if(a == 0) {
cout << "number is zero";
} elseif( a < 1) {
cout << "number is negative";
} else {
cout << "number is positive";
}
return0;
}
Output
> Enter a number: 0> Number is zero
4. enter the marks of five subjects and find the total marks, percentage and grades using classes
#include<iostream>usingnamespacestd;classmarks {
public:double english, nepali, account, maths, computer;
double totalMarks, percentage;
voidgetMarks() {
cout << "Enter the marks of English: ";
cin >> english;
cout << "Enter the marks of Nepali: ";
cin >> nepali;
cout << "Enter the marks of Account: ";
cin >> account;
cout << "Enter the marks of Maths: ";
cin >> maths;
cout << "Enter the marks of Computer: ";
cin >> computer;
}
voidcalcTotal() {
totalMarks = english + maths + nepali + computer + account;
cout << "Total Marks of all subject = " << totalMarks << endl;
}
voidcalcPercentage() {
percentage = (totalMarks / 500) * 100;
}
voidshowGrades() {
if (percentage >= 90) {
cout << "Grades = A+";
} elseif (percentage < 90 && percentage >= 80) {
cout << "Grades = A";
} elseif (percentage < 80 && percentage >= 70) {
cout << "Grades = B+";
} elseif (percentage < 70 && percentage >= 60) {
cout << "Grades = B";
} elseif (percentage < 60 && percentage >= 50) {
cout << "Grades = C+";
} elseif (percentage < 50 && percentage >=40 ) {
cout << "Grades = C";
} else {
cout << "FAIL";
}
}
};
intmain() {
marks mk;
mk.getMarks();
mk.calcTotal();
mk.calcPercentage();
mk.showGrades();
return0;
}
Output
> Enter the marks of English: 92> Enter the marks of Nepali: 92> Enter the marks of Account: 92> Enter the marks of Maths: 92> Enter the marks of Computer: 92> Total marks of all subject = 460> Grade = A+
Assignment
1. Enter values of length and breadth of a rectangle from user and check if it is square or not.
#include<iostream>usingnamespacestd;classrect {
public:int length, breath;
voidgetData() {
cout << "Enter the length of the rectriangle: ";
cin >> length;
cout << "Enter the breath of the rectriangle: ";
cin >> breath;
}
voidcheckShape() {
if(length == breath) {
cout << "It is a square!";
} else {
cout << "It is a rectriangle!";
}
}
};
intmain() {
rect rt;
rt.getData();
rt.checkShape();
return0;
}
Output
> Enter the length of the rectriangle: 10 > Enter the breath of the rectriangle: 10> It is a square!
2. A shop will give discount of 10% if the cost of purchased quantity is more than 1000. Ask user for quantity. Suppose, one unit will cost 100. Judge and print total cost for user.
> Enter the quantity of product you want to buy: 9> Total Cost = Rs. 900
3. A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years. Ask user for their salary and year of service and print the net bonus amount.