You are on page 1of 51

1.

Program to find the area and parameter of


1. #include<iostream>
2. using namespace std;
3.
4. int main()
5. {
6. float l,w,area,peri;
7. cout<<"Enter the length of rectangle:";
8. cin>>l;
9. cout<<"Enter the width of rectangle:";
10. cin>>w;
11.
12. area=l*w;
13. peri=2*(l+w);
14.
15. cout<<"Area of rectangle is:"<<area<<endl;
16. cout<<"Perimeter of rectangle
is:"<<peri<<endl;
17.
18. cout<<endl;
return 0;
19. }

Output
2.Program to find area and circumference of area.
#include<iostream>
using namespace std;

int main()
{
float radius,PI=3.14,area,circum;
cout<<"Enter the radius of circle:";
cin>>radius;

area=PI*radius*radius;
circum=2*PI*radius;

cout<<"Area of circle="<<area<<endl;
cout<<"circumference of circle="<<circum<<endl;

cout<<endl;
return 0;
}

Output
3.program to calculate the area of square.
#include<iostream>
using namespace std;

int main()
{
float a,area;
cout<<"Enter the side of square:";
cin>>a;
area=a*a;
cout<<"Area of square="<<area<<endl;
cout<<endl;
return 0;
}

Output
3. Program to calculate the volume of cube.
#include<iostream>
using namespace std;

int main()
{
float a,volume;
cout<<"Enter the side of cube:";
cin>>a;
volume=a*a*a;
cout<<"Area of cube="<<volume<<endl;
cout<<endl;
return 0;
}

Output
5. Program to swap two numbers.

#include<iostream>
using namespace std;

int main()
{
int num1,num2,temp;
cout<<"Before swaping\n";
cout<<"Enter first number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;

temp=num2;
num2=num1;
num1=temp;
cout<<"After swaping number\n";
cout<<"First number is:"<<num1<<endl;
cout<<"second number is:"<<num2<<endl;

return 0;
}

Output
6. Program to calculation of aggregate and
percentage marks.
#include<iostream>
using namespace std;

int main()
{
float s1,s2,s3,s4,s5;
float agg,percentage;

cout<<"Enter the marks of first subject:\n";


cin>>s1;
cout<<"Enter the marks of second subject:\n";
cin>>s2;
cout<<"Enter the marks of third subject:\n";
cin>>s3;
cout<<"Enter the marks of fourth subject:\n";
cin>>s4;
cout<<"Enter the marks of fifth subject:\n";
cin>>s5;

agg=s1+s2+s3+s4+s5;
percentage=(s1+s2+s3+s4+s5)/500*100;

cout<<"Aggregate = "<<agg<<endl;
cout<<"percentage = "<<percentage<<endl;

return 0;
}

Output
7. Program that convert kilometer per hour
to miles per hour.

#include<iostream>
using namespace std;

int main()
{
float kmph,miph;

cout<<"Enter the distance in kilometer:";


cin>>kmph;

miph=kmph*0.6213712;
cout<<"The "<<kmph<<" km/h = "<<miph<<"
Miles/h"<<endl;
return 0;
}

Output
8. Program to enter P,T,R and calculate compound
interest.
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
float p,r,t,cp,ci;

cout<<"Enter the principle:";


cin>>p;
cout<<"Enter the rate of interest:";
cin>>r;
cout<<"Enter the time:";
cin>>t;

ci=p*pow((1+r/100),t)-p;
cp=p*pow((1+r/100),t);

cout<<"The interest after compounded for the amount"<<p<<" for


"<<t<<" years "<<r<<" %is:"<<ci<<endl;
cout<<"The total amount after compounded for the amount"<<p<<"
for "<<t<<" years "<<r<<" %is:"<<cp<<endl;

return 0;
}
Output
9. Program to which accepts amount as integer and display
total number of notes of Rs 500,100,50,20,10,5,1.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int amount,R500,R100,R50,R20,R10,R5,R1;
cout<<"Enter amount:";
cin>>amount;
R500=amount/500;
amount=amount%500;
R100=amount/100;
amount=amount%100;
R50=amount/50;
amount=amount%50;
R20=amount/20;
amount=amount%20;
R10=amount/10;
amount=amount%10;
R5=amount/5;
amount=amount%5;
R500=amount/1;

cout<<"Rs.500:"<<R500<<"\nRs.100:"<<R100<<"\nRs.50:"<<R5
0<<"\nRs.20:"<<R20<<"\nRs.10:"<<R10<<"\nRs.5:"<<R5<<"\nRs.1:
"<<R1;
return 0;
}
Output
10. Program to get the volume of a sphere.
#include<iostream>
using namespace std;
int main()
{
int radius;
float volume,PI=3.1416;
cout<<"Enter the value of radius of sphere:";
cin>>radius;

volume=(4*PI*radius*radius*radius)/3;
cout<<"The volume of sphere is:"<<volume<<endl;

return 0;
}
Output
11. program to print positive number entered by
user.
#include<iostream>
using namespace std;
int main()
{
int number;
cout<<"Enter an integer:";
cin>>number;

if(number>0)
{
cout<<"Entered positive number is:"<<number<<endl;
}

return 0;
}

Output
11. Program to check whether an integer is positive
or negative.
#include<iostream>
using namespace std;

int main()
{
int number;

cout<<"Enter an integer:";
cin>>number;

if(number>=0)
{
cout<<"Entered number is positive:"<<number<<endl;
}
else
{
cout<<"Entered number is negative:"<<number<<endl;

}
return 0;
}

Output
13. Program to check whether an integer is positive
negative or zero.
#include<iostream>
using namespace std;

int main()
{
int number;
cout<<"Enter an integer:";
cin>>number;

if(number>0)
{
cout<<"Entered number is positive:"<<number<<endl;
}
else if(number<0)
{
cout<<"Entered number is negative:"<<number<<endl;
}
else
{
cout<<"Entered number is zero"<<endl;
}
return 0;
}

Output
14. Program to check whether number is odd or
even.
#include<iostream>
using namespace std;

int main()
{
int number;
cout<<"Enter an integer:";
cin>>number;

if(number%2==0)
{
cout<<"Entered number is
even:"<<number<<endl;
}
else
{
cout<<"Entered number is odd:"<<number<<endl;
}
return 0;
}

Output
15. Program to read the age of a candidate and
determine whether it is eligible for casting his/her vote.
#include<iostream>
using namespace std;

int main()
{
int age;
cout<<"Enter age:";
cin>>age;
if(age>=13 && age<=19)
{
cout<<"Person is teenager"<<endl;
}
else{
cout<<"Person is not teenager"<<endl;
}
if(age>=18)
{
cout<<"Person is eligible for voting"<<endl;
}
else
{
cout<<"Person is not eligible for voting"<<endl;
}
return 0;
}

Output
18. Program to find greatest of three
numbers.
#include<iostream>
using namespace std;
int main()
{
int num1,num2,num3;
cout<<"Enter three integer number:";
cin>>num1>>num2>>num3;

if(num1>num2 && num1>num3)


cout<<num1<<"a is Largest number "<<endl;

else if(num2>num1 && num2>num3)


cout<<num2<<" is Largest"<<endl;

else if(num3>num1 && num3>num2)


cout<<num3<<" c is Largest number "<<endl;

else
cout<<"Two large numbers or all numbers are
same"<<endl;
return 0 ;
}

Output
19. Program to calculate student grade.
#include<iostream>
using namespace std;
int main()
{
int marks;
cout<<"Enter Your Marks: ";
cin>>marks;
if (marks >= 90){
cout<<"Your Grade is A+";
}
else if (marks >= 80){
cout<<"Your Grade is A";
}
else if (marks >= 70){
cout<<"Your Grade is B+";
}
else if (marks >= 60){
cout<<"Your Grade is B";
}
else if (marks >= 50){
cout<<"Your Grade is C";
}
else if (marks >= 40){
cout<<"Your Grade is D";
}
else if (marks >= 30){
cout<<"Your Grade is E";
}
else if (marks <= 30){
cout<<"Your Grade is F";
}
else{
cout<<"Enter Valid Marks";
}
return 0;
}

Output
20. Program to print numbers which are
divisible by 3 and 5.
#include <iostream>
using namespace std;
int main()
{
int i;
cout<<" First 100 numbers which are divisible by 3 and 5 ";
for (i=1; i<=100; i++)
{
if (i%3==0 && i%5==0)
{
cout<<"\n"<<i;
}
}
return 0;
}

Output
21. Program to check positive negative or zero
using switch case
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number ";
cin>>num;
switch (num > 0)
{
case 0:
cout<<"num is negative";
break;
case 1:
cout<<"num is positive";
break;
Default :
cout<<"num is zero";
}
return 0;
}

Output
22. Program to find maximum using switch case.
#include<iostream>
using namespace std;
int main()
{
int num1,num2;
cout<<"Enter first Number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;
switch(num1>num2)
{
case 0:
if(num2>num1)
{
cout<<"second number is greater ";
}
else{
cout<<"Equal";
}
break;
case 1:
cout<< "\n first number is greater ";
break;
}
return 0;
}

Output
23.Program to Create a Calculator using switch
statements.
#include <iostream>
using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
cout << "Error! The operator is not correct";
break;
}
return 0;
}

Output
24. Program to check even or odd number using
switch case.
#include <iostream>
using namespace std;
int main(){
int num;
cout<<"Enter a number to check even or odd: ";
cin>>num;
switch(num % 2)
{
case 0: cout<<"Number is even";
break;
case 1: cout<<"Number is odd";
break;
}
return 0;
}

Output
25. program to print number of days in a month using switch
case.
#include <iostream>
using namespace std;
int main(){
int monthnumber;
cout<<"Enter month number(1-12): ";
cin>>monthnumber;
switch(monthnumber){
case 1: cout<<"31 days";
break;
case 2: cout<<"28 or 29 days";
break;
case 3: cout<<"31 days";
break;
case 4: cout<<"30 days";
break;
case 5: cout<<"31 days";
break;
case 6: cout<<"30 days";
break;
case 7: cout<<"31 days";
break;
case 8: cout<<"31 days";
break;
case 9: cout<<"30 days";
break;
case 10: cout<<"31 days";
break;
case 11: cout<<"30 days";
break;
case 12: cout<<"31 days";
break;
default: cout<<"Invalid input";
}
return 0;
}
Output
23.Program to find first 10 natural numbers.
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"The first 10 natural numbers are:\n";
for(n=1;n<=10;n++)
{
cout<<n<<endl;
}
return 0;
}

Output
27.Program to find the sum of first 10 natural
numbers.
#include<iostream>
using namespace std;
int main()
{
int n,sum=0;
cout<<"The first 10 natural numbers are:\n";
for(n=1;n<=10;n++)
{
cout<<n<<endl;
sum=sum+n;
}
cout<<"The sum of first 10 natural
numbers:"<<sum<<endl;
return 0;
}

Output
28.Program to find the factorial of a number.
#include<iostream>
using namespace std;
int main()
{
int n,factorial=1;
cout<<"Enter a number to find the factorial:";
cin>>n;
for(int i=1;i<=n;i++)
{
factorial=factorial*i;
}
cout<<"factorial :"<<factorial<<endl;
return 0;
}

Output
29.Program to generate multiplication table.
#include<iostream>
using namespace std;

int main()
{
int n;
cout<<"Enter an positive integer:";
cin>>n;
for(int i=1;i<=10;i++)
{
cout<<n<<"*"<<i<<"="<<n*i<<endl;
}
return 0;
}

Output
30.Program to calculate sum of first n even number.
#include<iostream>
using namespace std;

int main()
{
int n,sum=10,j;
cout<<"print sum of even number til:";
cin>>n;

for(j=1;j<=n;j++)
{
if((j%2)==0)
{
sum+=j;
}
}
cout<<"Sum of even number from 1 to "<<n<<"
is:"<<sum<<endl;
return 0;
}

Output
31.program to add two numbers using function.
#include<iostream>
using namespace std;

int add(int a,int b);


int main()
{
int num1;
int num2;
int sum;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;

sum=add(num1,num2);

cout<<"sum is: "<<sum<<endl;


return 0;
}
int add(int a,int b)
{
return (a+b);
}

Output
32.program to find the square root of a number
using function.
#include<iostream>
#include<math.h>
using namespace std;

int main() {

float num, squareroot;


cout << "Enter a number:";
cin >> num;

squareroot = sqrt(num);
cout << "Square root of " << num << " is :"
<<squareroot;
return 0;
}

Output
33.Program to find max between two number using
function.
#include<iostream>
using namespace std;

int maxOfTwo(int, int);


int main()
{
int numOne, numTwo, larg;
cout<<"Enter the Two Numbers: ";
cin>>numOne>>numTwo;
larg = maxOfTwo(numOne, numTwo);
cout<<"\nLargest = "<<larg;
cout<<endl;
return 0;
}
int maxOfTwo(int nOne, int nTwo)
{
if(nOne>nTwo)
return nOne;
else
return nTwo;
}

Output
34.Program to check even or odd using function.
#include <iostream>
using namespace std;

int isEven(int num)


{
return !(num & 1);
}

int main(){

int num;
cout<<"Enter any number: ";
cin>>num;
if(isEven(num))
{
cout<<"The entered number is even.";
}
else
{
cout<<"The entered number is odd.";
}
return 0;
}

Output
35.Program to find factorial through
function.
#include <iostream>
#include <conio.h>
using namespace std;
int fact(int);
int main()
{
int num,factorial;
cout<<"Enter the number to find factorial\n";
cin>>num;
factorial=fact(num);
cout<<"Factorial of "<<num<<"is: "<<factorial;
getch();
return 0;
}
int fact(int num)
{
int i,f=1;
for(i=1; i<=num; i++)
f=f*i;
return(f);
}

Output
36.Program to reverse a number using function.
#include<iostream>
using namespace std;
int rev(int);
int main()
{
int num, r;
cout<<"Enter the Number: ";
cin>>num;
r = rev(num);
cout<<"\nReverse = "<<r;
cout<<endl;
return 0;
}
int rev(int n)
{
int rem, res=0;
while(n!=0)
{
rem = n%10;
res = (res*10) + rem;
n = n/10;
}
return res;
}

Output
37.Program to input number is Armstrong or not.
#include <iostream>
using namespace std;
bool checkArmstrongNumber(int num);
int main(){
int num;
bool flag;
cout<<"Enter a positive integer: ";
cin>>num;

flag = checkArmstrongNumber(num);
if(flag == true)
cout<<num<<" is an Armstrong number.";
else
cout<<num<<" is not an Armstrong number.";
return 0;
}
bool checkArmstrongNumber(int num) {
int temp, sum=0, digit;
bool isArm;
temp = num;
while(temp != 0) {
digit = temp % 10;
sum = sum +(digit * digit * digit);
temp = temp/10;
}
if (sum==num)
isArm = true;
else
isArm = false;
return isArm;
}
Output
38.Program to display the first n term of Fibonacci
series using function.
#include<iostream>
using namespace std;
void printFibonacci(int n){
static int n1=0, n2=1, n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n-1);
}
}
int main(){
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2);
return 0;
}

Output
39.Program to add number through function overloading.
#include <iostream>
using namespace std;

int add(int, int);


float add(float, float);

int main(){
int a, b, x;
float c, d, y;
cout << "Enter two integers\n";
cin >> a >> b;
x = add(a, b);
cout << "Sum of integers: " << x << endl;
cout << "Enter two floating point numbers\n";
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
return 0;
}
int add(int x, int y){
int sum;
sum = x + y;
return sum;
}
float add(float x, float y)
{
float sum;
sum = x + y;
return sum;
}
Output
40.Program to find area of square, rectangle, circle
and triangle using function overloading.
#include<iostream>
using namespace std;

int area(int);
int area(int,int);
float area(float);
float area(float,float);

int main()
{
int s,l,b;
float r,bs,ht;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of
rectangle:";
cin>>l>>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
cout<<"Area of square is"<<area(s);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(bs,ht);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float bs,float ht)
{
return((bs*ht)/2);
}

Output
41.Program to function overriding.
#include <iostream>
using namespace std;

class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};

class Derived : public Base {


public:
void print() {
cout << "Derived Function" << endl;
}
};

int main() {
Derived derived1;
derived1.print();
return 0;
}

Output
42.Program of single inheritance.
#include <iostream>
using namespace std;

class Animal {
public:
void eat() {
cout << "I can eat." << endl;
}
void sleep() {
cout << "I can sleep." << endl;
}
};
class Dog : public Animal {

public:
void bark() {
cout << "I can bark, Woof woof" << endl;
}
};
int main() {
Dog dog1;
dog1.eat();
dog1.sleep();
dog1.bark();
return 0;
}

Output
43.Program of multiple inheritance.
#include<iostream>
using namespace std;

class A{
public:
A(){
cout<<"Constructor of class A"<<endl;
}
};
class B{
public:
B(){
cout<<"Constructor of class B"<<endl;
}
};
class C:public A, public B{
public:
C(){
cout<<"Constructor of class C"<<endl;
}
};
int main(){
C obj;
return 0;
}

Output
44.Program to show the mechanism of
the abstract class.
#include <iostream>
using namespace std;

class Shape {
public:
virtual int Area() = 0;
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};

class Rectangle: public Shape {


public:
int Area() {
return (width * height);
}
};

class Triangle: public Shape {


public:
int Area() {
return (width * height)/2;
}
};
int main() {
Rectangle R;
Triangle T;

R.setWidth(4);
R.setHeight(9);

T.setWidth(22);
T.setHeight(8);

cout << "The area of the rectangle is: " <<


R.Area() << endl;
cout << "The area of the triangle is: " << T.Area()
<< endl;
}

Output
45.Program to show the mechanism of the
pure virtual function.
#include<iostream>
using namespace std;

class Base
{
int x;
public:
virtual void fun()=0;
int getX(){
return x;
}
};
class Derived
{
int y;
public:
void fun(){
cout<<"fun() called";
}
};
int main()
{
Derived d;
d.fun();
return 0;
}

Output
46.Program to find length of string.
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
char name[15]="MADHYAPRADESH";
int len;
len=strlen(name);
cout<<"length:"<<len<<endl;
}

Output
47.Program to compare two string.
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
char name1[15]="MADHYAPRADESH";
char name2[15]="UTTARPRADESH";

if(strcmp(name1,name2)==0)
{
cout<<"Both Strings are equal"<<endl;
}
else
cout<<"strings are not equal"<<endl;
return 0;
}

Output
48.Program to copy string.

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
char str1[20]="python";
char str2[20];

strcpy(str2,str1);
cout<<str2;
}

Output
49.Program to concatenate string.

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
char str1[20]="python";
char str2[20]="java";

strcat(str1,str2);
cout<<str1;
}

Output
50.Program to reverse string.

#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50], temp;
int i, j;
cout << "Enter a string : ";
gets(str);
j = strlen(str) - 1;
for (i = 0; i < j; i++,j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << "\nReverse string : " << str;
return 0;
}

Output

You might also like