Program 1: Q.WAP To Find The Sum of Two Numbers Sol

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Page |1

Program 1
Q.WAP to find the sum of two numbers

Sol.

#include<iostream>
using namespace std;
int main()
{
int n1,n2;
cout<<"eNter two no\n";
cin>>n1>>n2;
cout<<" sum = "<<n1+n2;
}

OUTPUT:

00190202018 AAKASH CHAHAL


Page |2

Program 2
Q.WAP to swap two numbers.

Sol.

#include<iostream>
using namespace std;
int main()
{
int n1,n2,temp;
cout<<"enter two no.' to swap:\n";
cin>>n1>>n2;
cout<<"bEfore sWapping n1="<<n1<<" & n2="<<n2;
temp=n1;
n1=n2;
n2=temp;
cout<<"\naFter sWapping n1="<<n1<<" & n2="<<n2;
}

OUTPUT:

00190202018 AAKASH CHAHAL


Page |3

Program 3
Q.WAP to find if a number is even or odd.

Sol.

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"eNter the no. : ";
cin>>n;
if(n%2==0)
cout<<n<<" is even";
else
cout<<n<<" is odd";
}

OUTPUT:

00190202018 AAKASH CHAHAL


Page |4

Program 4
Q.WAP to find factorial of a number.

Sol.

#include<iostream>
using namespace std;
int main()
{
int n,fact;
cout<<"eNter the number to find factorial : ";
cin>>n;
fact=n;
if(n==0)
fact=1;
else
{
for(int i=n-1;i>0;i--)
fact=fact*i;
}
cout<<"fActorial of "<<n<<" = "<<fact;
}

OUTPUT:

00190202018 AAKASH CHAHAL


Page |5

Program 5
Q.WAP to find if number is prime or not.

Sol.

#include<iostream>
using namespace std;
int main()
{
int n,flag=0;
cout<<"eNter the number : ";
cin>>n;
for(int i=1;i<=n;i++)
{
if(n%i==0)
flag++;
}
if(flag==2)
cout<<n<<" is a prime number";
else
cout<<n<<" is not a prime number";
}

OUTPUT:

00190202018 AAKASH CHAHAL


Page |6

00190202018 AAKASH CHAHAL


Page |7

Program 6
Q.WAP to find whether the entered year is a leap year or not.

Sol.
#include<iostream>
using namespace std;
int main()
{
int yr;
cout<<"eNter the year: ";
cin>>yr;
if(yr%4==0)
cout<<yr<<" is a leap year";
else
cout<<yr<<" is not a leap year";
}

OUTPUT:

00190202018 AAKASH CHAHAL


Page |8

Program 7
Q.WAP to create a class to enter and display the details of student.
Sol.
#include<iostream>
#include<cstdio>
using namespace std;
class student
{
int roll,marks;
char name[20],fname[20];
public:
int enter()
{
cout<<"\t\t*/eNter dEtails oF sTudent*/";
cout<<"\nEnter Roll Number :";
cin>>roll;
cin.get();
cout<<"Enter Name :";
gets(name);
cout<<"Enter Father' Name :";
gets(fname);
cout<<"Enter Marks :";
cin>>marks;
}
int display()
{
cout<<"\nRoll NO :"<<roll;
cout<<"\nName :"<<name;
cout<<"\nFather' Name :"<<fname;
cout<<"\nMarks :"<<marks;
}
};
int main()
{
student stu;
stu.enter();
cout<<"\t\tdEtails oF sTudent";
stu.display();
}

00190202018 AAKASH CHAHAL


Page |9

OUTPUT:

00190202018 AAKASH CHAHAL


P a g e | 10

Program 8
Q.WAP to create class to calculate and display are of any rectangle.

Sol.
#include<iostream>
using namespace std;
class Rectangle
{
float l,b,ar;
public:
int calc()
{
cout<<"eNter tHe lEngth & bReadth rEspectively oF rEctangle\n";
cin>>l>>b;
ar=l*b;
cout<<"Area of rectangle = "<<ar;
}
};
int main()
{
Rectangle rect;
rect.calc();
}

OUTPUT:

00190202018 AAKASH CHAHAL

You might also like