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 = 5, b = 10;
int sum = a + b;
cout << "The sum is " << sum;
return0;
}
Output
> The sum is 15
2. Program that takes string input using cin
#include<iostream>usingnamespacestd;intmain() {
string fName;
cout << "Enter your name: ";
cin >> fName;
cout << "Your name is " << fName;
return0;
}
Output
> Enter your name: Nikhil> Your name is Nikhil
3. Sum using user input
#include<iostream>usingnamespacestd;intmain() {
int a,b,sum=0;
cout << "Enter a: ";
cin >> a;
cout << "Enter b: ";
cin >> b;
sum = a + b;
cout << "The sum is " << sum;
return0;
}
Output
> Enter a: 8> Enter b: 12> The sum is 20
4. Program to find the area
#include<iostream>usingnamespacestd;intmain() {
int area,l,b;
cout << "Enter length: ";
cin >> l;
cout << "Enter breath: ";
cin >> b;
area = l*b;
cout << "The area is " << area << " sq.m";
return0;
}
Output
> Enter length: 7> Enter breath: 3> The area is 21 sq.m
5. Simple interest using double
#include<iostream>usingnamespacestd;intmain() {
double simpleInterest, principal, rate, time;
cout << "Enter the Principal: ";
cin >> principal;
cout << "Enter the Rate: ";
cin >> rate;
cout << "Enter the Time: ";
cin >> time;
simpleInterest = (principal * time * rate) / 100;
cout << "The simple interest is " << simpleInterest;
return0;
}
Output
> Enter the Principal: 20000> Enter the Rate: 12> Enter the Time: 2.5> The simple interest is
6. Area of circle
#include<iostream>usingnamespacestd;intmain() {
float areaOfCircle, radius;
double pi = 3.14;
cout << "Enter the radius of the circle: ";
cin >> radius;
areaOfCircle = pi * radius * radius;
cout << "The area of the circle is " << areaOfCircle << " cm2";
return0;
}
Output
> Enter the radius of the Circle: 14> The area of the circle is 615.75cm2
#include<iostream>usingnamespacestd;classaddNum {
public:int a, b;
int sum = 0;
voidaskNum() {
cout << "Enter any two number: ";
cin >> a;
cin >> b;
sum = a + b;
}
voidcalcSum() {
cout << "The sum is " << sum;
}
};
intmain() {
addNum ap;
ap.askNum();
ap.calcSum();
return0;
}
Output
> Enter any two number: 10 20 > The sum is 30
11. Area of Rectriangle
#include<iostream>usingnamespacestd;classArea
{
int l, b;
public:voidinput()
{
cout << "Enter length :";
cin >> l;
cout << "Enter breadth :";
cin >> b;
}
voidoutput()
{
cout << "The area is = " << l*b;
}
};
intmain()
{
Area a1;
a1.input();
a1.output();
return0;
}
Output
> Enter length: 10> Enter breath: 4> The area is 40
#include<stdio.h>intmain()
{
for (inti=2; i <= 10; i+=2)
{
printf("%d\t", i);
}
return0;
}
Output
> 2 4 6 8 10
16. Checks if the input number is positive or not
#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
17. 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
18. 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
19. 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+
20. 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!
21. 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
22. 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.
33. Input two number and find the sum, difference, product and quotient using switch
#include<iostream>usingnamespacestd;classmaths {
public:int sum = 0, div, mul, sub;
int a, b;
char choice;
voidgetInput() {
cout << "Enter one number: ";
cin >> a;
cout << "Enter two number: ";
cin >> b;
}
voidgetChoice() {
cout << "Enter + for addition!" << endl;
cout << "Enter - for Substraction!" << endl;
cout << "Enter * for Multiplication!" << endl;
cout << "Enter / for Division!" << endl;
cin >> choice;
}
voidoperation() {
switch(choice) {
case'+': {
sum = a + b;
cout << "Sum is " << sum;
break;
};
case'-': {
sub = a - b;
cout << "Substraction is " << sub;
break;
};
case'*': {
mul = a * b;
cout << "Multiplication is " << mul;
break;
};
case'/': {
div = a / b;
cout << "Division is " << div;
break;
};
default: {
cout << "Invalid Input!";
break;
}
}
}
};
intmain() {
maths mt;
mt.getInput();
mt.getChoice();
mt.operation();
return0;
}
Output
> Enter one number: 10> Enter two number: 2 > Enter + for addition!> Enter - for Substraction!> Enter * for Multiplication!*> Enter / for Division!> 1> Sum is 12