Professional Documents
Culture Documents
Code:
//1:Write a program to adddition of two numbers .
#include<iostream>
using namespace std;
int main() {
int a,b,tot;
cout<<"Enter two numbers"<<endl;
cin>>a;
cin>>b;
tot = a+b;
cout<<"sum = "<<tot;
}
Output:
Code:
#include<iostream>
using namespace std;
int main(){
int a,b,temp;
cout<<"Enter two numbers to be swaped"<<endl;
cin>>a;
cin>>b;
temp=a;
a=b;
b=temp;
cout<<"Swapped no. a= "<<a<<" b = "<<b;
}
Output:
//3:Write a program to find factorial of a given number.
Code:
#include<iostream>
using namespace std;
int factorial(int n){
int i,res=1;
cout<<"enter number :"<<endl;
cin>>n;
for(i=1;i<=n;i++){
res *= i;
}
cout<<"\nfactorial is "<<res;
}
int main(){
int n=5;
factorial(n);
}
Output:
#include<iostream>
int main(){
int n,m,i,res;
cin>>m;
cin>>n;
for (i=1;i<=n;i++){
res = res*m;
cout<<"product = "<<res;
Output:
//5:Check if number is a prime number or not.
#include<iostream>
#include<math.h>
int main(){
int m,i;
int y=1;
cin>>m;
for(i=2;i<=sqrt(m);i++)
if(m%i==0)
y=0;
break;
if(y==1)
cout<<"prime number\n";
else
{
cout<<"not prime number";
Output:
#include<iostream>
#include<math.h>
int main(){
int num,sum=0;
cin>>num;
for(int i=0;i<=num;i++){
sum += i;
cout<<"Sum = "<<sum;
Output:
#include<iostream>
#include<math.h>
int main(){
int a,temp,r,sum=0;
cout<<"Enter a number"<<endl;
cin>>a;
temp = a;
while(a>0){
r = a%10;
sum=(sum*10)+r;
a/=10;
if(temp==sum){
cout<<"Number is palindrome";
}else{
Output:
#include<iostream>
#include<math.h>
int main(){
int i,n,esum=0,osum=0;
cout<<"Enter a number"<<endl;
cin>>n;
for(i=1;i<=n;i++)
if (i%2==0)
esum+=i;
else
osum+=i;
Output:
include<iostream>
#include<math.h>
int main(){
int a,r,sum=0;
cout<<"Enter a number"<<endl;
cin>>a;
while(a>0){
r = a%10;
sum=(sum*10)+r;
a/=10;
Output
#include<iostream>
#include<math.h>
int main(){
int a,i,j,y=0;
cin>>a;
for(i=2;i<=a;i++){
for(j=2;j<i;j++){
if(i%j==0){
y=1;
break;
else
y=0;
}
}
if(y==0){
cout<<i<<" ";
Output:
#include<iostream>
#include<math.h>
int main() {
int n,r,sum=0,temp;
cout<<"enter number\n";
cin>>n;
temp = n;
while(n>0){
r=n%10;
n/=10;
if(temp==sum){
cout<<"Number is Armstrong\n";
}else{
}
}
Output:
#include<iostream>
#include<math.h>
int main(){
int a,b,c;
cout<<"Enter a=";
cin>>a;
cout<<"Enter b=";
cin>>b;
cout<<"Enter c=";
cin>>c;
if(a>b&&a>c){
}else if(b>c&&b>a){
}else{
Output:
#include<iostream>
int main(){
int a=0,b=0,c=0,reg=99,med=199,large=299,che=150,pan=120;
int thin=200,oni=30,corn=30,mush=30,total=0;
do{
cout<<"1.pasta pizza"<<endl;
cout<<"2.Margherita"<<endl;
cout<<"3.Farmhouse"<<endl;
cin>>a;
switch(a){
case 1: total+=reg;
break;
case 2: total+=med;
break;
case 3: total+=large;
break;
}while(a=0);
do
{
cout<<"1.Cheez Burst"<<endl;
cout<<"2.Pan Pizza"<<endl;
cout<<"3.Thin Crust"<<endl;
cin>>b;
switch(b){
case 1: total+=che;
break;
case 2: total+=pan;
break;
case 3: total+=thin;
break;
}while(b=0);
do{
cout<<"1.Onion"<<endl;
cout<<"2.Corn"<<endl;
cout<<"3.Mushroom"<<endl;
cin>>c;
switch(c){
case 1: total+=oni;
break;
case 2: total+=corn;
break;
case 3: total+=mush;
break;
}while(c=0);
Output:
**
***
****
*****
*/
#include<iostream>
#include<math.h>
int main(){
int i,j,n;
cout<<"enter no rows\n";
cin>>n;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
cout<<"* ";
cout<<"\n";
Output:
Day 5:
#include<iostream>
class Student{
private:int rollno;
double marks1,marks2,marks3,total,percentage;
string name,grade;
//private function
void detGrade(){
total = marks1+marks2+marks3;
percentage = total/3;
if(percentage>80){
grade = "A+";
grade="A";
grade="B";
public:
Student(){
cout<<"-------constructor invoked------------"<<endl;
rollno = 1;
name = "kanad";
marks1 =100.0;
marks2 =100.0;
marks3 =100.0;
}
cout<<"------parameterized constructor--------"<<endl;
rollno = rn;
name=nm;
marks1=mk1;
marks2=mk2;
marks3=mk3;
void printData(){
cout<<"---------inside class-----------"<<endl;
void showStudent(){
cout<<"Inside Show()"<<endl;
detGrade();
// to modofy name
name=nm;
string getName(){
return name;
};
//user:
int main(){
Student s1;
s1.printData();
s1.showStudent();
int rollno;
string name;
double marks1,marks2,marks3;
Student s2(101,"sam",40,50,60);
s2.showStudent();
Student s3(102,"tom",90,90,80);
s3.showStudent();
Output:
8: Create a class Person with data members as name, age, city. Write
getters and setters for all the data members. Also add the display
function. Create Default and Parameterized constructors. Create the
object of this class in main method and invoke all the methods in
that class.
#include<iostream>
class Person{
private:string name,city;
int age;
public:
Person(){
cout<<"-------Default constructor-------"<<endl;
name="Sam";
age = 15;
city = "Pune";
cout<<"---para Constructor"<<endl;
name=nm;
age=ag;
city=ct;
void display(){
cout<<"inside display()"<<endl;
cout<<name<<"\t"<<age<<"\t"<<city<<endl;
string getName(){
return name;
}
name = nm;
string getCity() {
return city;
city = ct;
int getAge() {
return age;
age = ag;
};
//user:
int main(){
Person p1;
p1.display();
cout<<"Name\tage\tcity "<<endl;
string name,city;
int age;
Person p2("Dom",27,"Mumbai");
p2.display();
Person p3("tom",45,"Goa");
p3.display();
Person p4("Jammy",49,"Bengluru");
p4.display();
Output:
9:. Create a class Date with data members as dd, mm, yy. Write
getters and setters for all the data members. Also add the display
function. Create Default and Parameterized constructors. Create the
object of this class in main method and invoke all the methods in
that class.
#include<iostream>
class Date{
public:
Date(){
cout<<"***Default contructor***"<<endl;
date=1;
month=1;
year=1970;
cout<<"--para contructor--"<<endl;
date=dd;
month=mm;
year=yy;
void display(){
cout<<"dd/mm/yy"<<endl;
cout<<date<<"/"<<month<<"/"<<year<<endl;
int getDate() {
return date;
date = dd;
int getMonth() {
return month;
month = mm;
int getYear() {
return date;
year = yy;
};
//user
int main(){
Date dob;
dob.display();
int date,month,year;
Date d2(14,3,1995);
d2.display();
cout<<d2.getDate()<<"th"<<d2.getMonth()<<"/"<<d2.getYear()<<endl;
d2.setDate(13);
d2.display();
Output
10: Create a class Book with data members as bname,id,author,price.
Write getters and setters for all the data members. Also add the
display function. Create Default and Parameterized constructors.
Create the object of this class in main method and invoke all the
methods in that class.
#include<iostream>
class Book{
private:string bname,author;
float price;
long id;
public:
Book(){
cout<<"***Default constructor***"<<endl;
id = 1234564;
author="Sam Perry";
price = 450.50;
}
Book(string bname,long id,string author,float price){
cout<<"**PAra constructor***"<<endl;
this->bname=bname;
this->id = id;
this->author=author;
this->price=price;
void display(){
cout<<"bookname = "<<bname<<endl;
cout<<"barcode id = "<<id<<endl;
cout<<"Price : "<<price<<endl;
string getBname(){
return bname;
this->bname = bname;
string getAuthor(){
return author;
this->author = author;
long getId(){
return id;
this->id=id;
}
float getPrice(){
return price;
this->price=price;
};
//user;;
int main(){
Book b1;
b1.display();
string bname,author;
float price;
long id;
b2.display();
Output
11. Create a class Point with data members as x,y. Create Default and
Parameterized constructors. Write getters and setters for all the data
members. Also add the display function. Create the object of this
class in main method and invoke all the methods in that class.
#include<iostream>
class Point{
public:
Point(){
cout<<"***Default constructor***"<<endl;
pt1=0;
pt2=0;
cout<<"***Para constructor***"<<endl;
this->pt1=pt1;
this->pt2=pt2;
void display(){
cout<<"Point = "<<pt1<<"."<<pt2<<endl;
int getPt1(){
return pt1;
this->pt1=pt1;
int getPt2(){
return pt1;
this->pt2=pt2;
};
//user;;
int main(){
Point p1;
p1.display();
int pt1,pt2;
Point p2(4,5);
p2.display();
o/p
12. Create a class ComplexNumber with data members real,
imaginary. Create Default and Parameterized constructors. Write
getters and setters for all the data members. Also add the display
function. Create the object of this class in main method and invoke
all the methods in that class.
#include<iostream>
class ComplexNumber{
public:
ComplexNumber(){
cout<<"***Default Constructor***"<<endl;
real=0;
imaginary=0;
cout<<"***Para constructor***"<<endl;
this->real=real;
this->imaginary=imaginary;
void display(){
int getReal(){
return real;
}
this->real=real;
int getImaginary(){
return imaginary;
this->imaginary=imaginary;
};
int main(){
ComplexNumber c1;
c1.display();
int real,imaginary;
ComplexNumber c2(88,99);
c2.display();
o/p
class Date{
public:
Date(){
cout<<"***Default Constructor***"<<endl;
day=15;
month=8;
year=1947;
this->day=day;
this->month=month;
this->year=year;
int getDay(){
return day;
this->day=day;
int getMonth(){
return month;
}
void setMonth(int month){
this->month=month;
int getYear(){
return year;
this->year=year;
void display(){
};
int main(){
Date d1;
d1.display();
o/p