You are on page 1of 31

Day 4:

1:Write a program to addition of two numbers .

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:

2:Write a program to swap two numbers.

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:

//4:Write a program to find m to the power n.

#include<iostream>

using namespace std;

int main(){

int n,m,i,res;

cout<<"enter main number"<<endl;

cin>>m;

cout<<"enter the exponent"<<endl;

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>

using namespace std;

int main(){

int m,i;

int y=1;

cout<<"Enter number: ";

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:

//6:Sum of series : 1+2+3+….+n

#include<iostream>

#include<math.h>

using namespace std;

int main(){

int num,sum=0;

cout<<"Enter a number upto which you want sum"<<endl;

cin>>num;

for(int i=0;i<=num;i++){

sum += i;

cout<<"Sum = "<<sum;

Output:

//7:Check whether the number is palindrome or not?

#include<iostream>
#include<math.h>

using namespace std;

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{

cout<<"number is not palindrome";

Output:

//8:Write a program to find sum of all even and odd numbe

#include<iostream>

#include<math.h>

using namespace std;

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;

cout<<"sum of even numbers is "<<esum<<"\n";

cout<<"sum of odd numbers is "<<osum;

Output:

//9: Write a program to enter a number and print its reve

include<iostream>

#include<math.h>

using namespace std;

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;

cout<<"reverse number is "<<sum;

Output

//10:Write a program to print all Prime numbers between 1 to n

#include<iostream>

#include<math.h>

using namespace std;

int main(){

int a,i,j,y=0;

cout<<"enter a number upto which prime numbers to be shown"<<endl;

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:

//11:Write a program to check entered number is Armstrong number or not.

#include<iostream>

#include<math.h>

using namespace std;

int main() {

int n,r,sum=0,temp;

cout<<"enter number\n";

cin>>n;

temp = n;

while(n>0){

r=n%10;

sum = sum +(r*r*r);

n/=10;

if(temp==sum){

cout<<"Number is Armstrong\n";

}else{

cout<<"Number is not armstrong\n";

}
}

Output:

//12:Write a program to find greatest of three numbers using nested if-else.

#include<iostream>

#include<math.h>

using namespace std;

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){

cout<<"greatest number = "<<a;

}else if(b>c&&b>a){

cout<<"greatest number = "<<b;

}else{

cout<<"greatest number = "<<c;

Output:
#include<iostream>

using namespace std;

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<<"-----Welcome to Dapper's Pizza-----"<<endl;

cout<<"Please select size"<<endl;

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;

default : cout<<"Not in menu. \n";

cout<<"\ntotal by far: "<<total<<endl;

}while(a=0);

do
{

cout<<"Please select below option for pizza"<<endl;

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;

default : cout<<"Not in menu. \n";

cout<<"\nTotal after extra addings: "<<total;

}while(b=0);

do{

cout<<"\nPlease select below topings for pizza"<<endl;

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;

default : cout<<"Not in menu. \n";

cout<<"\nYour Garnd total : "<<total;

}while(c=0);

Output:

/* 15:Write a program to print following pattern.

**

***
****

*****

*/

#include<iostream>

#include<math.h>

using namespace std;

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:

7:Write a program to create student class with data members rollno,


marks1,mark2,mark3.
Accept data (acceptInfo()) and display  using display member
function.
Also display total, percentage and grade.
Code:

#include<iostream>

using namespace std;

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+";

}else if (percentage>60 || percentage<=79){

grade="A";

}else if (percentage>40 || percentage<=59){

grade="B";

public:

Student(){

cout<<"-------constructor invoked------------"<<endl;

rollno = 1;

name = "kanad";

marks1 =100.0;

marks2 =100.0;

marks3 =100.0;
}

Student(int rn,string nm,double mk1,double mk2,double mk3){

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();

cout<<" "<<rollno<<" "<<name<<" "<<marks1<<"


"<<marks2<<" "<<marks3<<" "<<total<<" "<<percentage<<" "<<grade<<endl;

// to modofy name

void setName(string nm){

name=nm;

string getName(){

return name;

};

//user:
int main(){

Student s1;

s1.printData();

s1.showStudent();

cout<<"Enter RollNo name marks1 marks2 marks3 total percentage grade"<<endl;

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>

using namespace std;

class Person{

private:string name,city;

int age;

public:

Person(){

cout<<"-------Default constructor-------"<<endl;

name="Sam";

age = 15;

city = "Pune";

Person(string nm,int ag,string ct){

cout<<"---para Constructor"<<endl;

name=nm;

age=ag;

city=ct;

void display(){

cout<<"inside display()"<<endl;

cout<<name<<"\t"<<age<<"\t"<<city<<endl;

//getters and setters

string getName(){

return name;
}

void setName(string nm) {

name = nm;

string getCity() {

return city;

void setCity(string ct) {

city = ct;

int getAge() {

return age;

void setAge(int ag) {

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>

using namespace std;

class Date{

private: int date,month,year;

public:

Date(){
cout<<"***Default contructor***"<<endl;

date=1;

month=1;

year=1970;

Date(int dd,int mm,int yy){

cout<<"--para contructor--"<<endl;

date=dd;

month=mm;

year=yy;

void display(){

cout<<"dd/mm/yy"<<endl;

cout<<date<<"/"<<month<<"/"<<year<<endl;

//getters and setters

int getDate() {

return date;

void setDate(int dd) {

date = dd;

int getMonth() {

return month;

void setMonth(int mm) {

month = mm;

int getYear() {
return date;

void setYear(int yy) {

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>

using namespace std;

class Book{

private:string bname,author;

float price;

long id;

public:

Book(){

cout<<"***Default constructor***"<<endl;

bname="wait how do I write this email !!";

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<<"Author name : "<<author<<endl;

cout<<"Price : "<<price<<endl;

string getBname(){

return bname;

void setBname(string bname) {

this->bname = bname;

string getAuthor(){

return author;

void setAuthor(string author) {

this->author = author;

long getId(){

return id;

void setId(long id){

this->id=id;

}
float getPrice(){

return price;

void setPrice(float price){

this->price=price;

};

//user;;

int main(){

Book b1;

b1.display();

string bname,author;

float price;

long id;

Book b2("Shriman yogi",1475475,"Ranjeet desai",889.99);

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>

using namespace std;

class Point{

private: int pt1,pt2;

public:

Point(){

cout<<"***Default constructor***"<<endl;

pt1=0;

pt2=0;

Point(int pt1,int pt2){

cout<<"***Para constructor***"<<endl;

this->pt1=pt1;

this->pt2=pt2;

void display(){
cout<<"Point = "<<pt1<<"."<<pt2<<endl;

int getPt1(){

return pt1;

void setPt1(int pt1){

this->pt1=pt1;

int getPt2(){

return pt1;

void setPt2(int pt2){

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>

using namespace std;

class ComplexNumber{

private: int real,imaginary;

public:

ComplexNumber(){

cout<<"***Default Constructor***"<<endl;

real=0;

imaginary=0;

ComplexNumber(int real,int imaginary){

cout<<"***Para constructor***"<<endl;

this->real=real;

this->imaginary=imaginary;

void display(){

cout<<"Complex Number = "<<real<<" + "<<imaginary<<"i"<<endl;

int getReal(){

return real;
}

void setReal(int real){

this->real=real;

int getImaginary(){

return imaginary;

void setImaginary(int imaginary){

this->imaginary=imaginary;

};

int main(){

ComplexNumber c1;

c1.display();

int real,imaginary;

ComplexNumber c2(88,99);

c2.display();

o/p

13:Create Date class with members day,month ,year.Write no


argument and parameterised constructor .Create two object s and
initialize them using no argument and parameterised constructor
respectively.Print date using display function.
#include<iostream>

using namespace std;

class Date{

private: int day,month,year;

public:

Date(){

cout<<"***Default Constructor***"<<endl;

day=15;

month=8;

year=1947;

Date(int day,int month,int year){

cout<<"***Para Constructor ***"<<endl;

this->day=day;

this->month=month;

this->year=year;

int getDay(){

return day;

void setDay(int day){

this->day=day;

int getMonth(){

return month;

}
void setMonth(int month){

this->month=month;

int getYear(){

return year;

void setYear(int year){

this->year=year;

void display(){

cout<<"Today's date : "<<day<<"/"<<month<<"/"<<year<<endl;

};

int main(){

Date d1;

d1.display();

o/p

You might also like