Practical Programs of C++ Vaibhav

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 21

Practical Programs of C++

List of all programs in Sem 4

1. Reverse of a Number
2. Factorial of a Number
3. Switch program for :
a. Sin
b. Cos
c. Factorial
d. Power
e. Square root of a Number.
4. Palindrome of a Number.
5. Write A Program (WAP) using class student which has access specified as
public and the name of function “getmarks()” and “totalmarks()” of 2
subjects. This program will accept marks of two subjects (using
“getmarks()”) and adds them then displays total addition of marks(using
“totalmarks()”).
6.
7. Program to obtain result of a BSc.IT student as per the ATKT pattern if
the student is having KT print number of KT and Subject Name in which
he failed.If more than 2KT’s fail & If no kt pass

Assessed By - Miss Maunika Kamadi


1. Reverse of a Number
Code:

#include<iostream.h>
#include<conio.h> //For clrscr() & getch()//
void main()
{
long num, digit, rev=0;
clrscr();
cout<<"*************************************************";
cout<<"\n*\t\t\t\t\t\t*\n";
cout<<"*\t Program for Reverse of a Number \t*";
cout<<"\n*\t\t\t\t\t\t*\n";
cout<<"*************************************************\n";
cout<<"Enter the Number\n";
cin>>num; // Input the number
while(num !=0) // while the number does not go to zero
{
digit= num % 10; // keep getting the last digit

rev= rev * 10 + digit; /* rev stores the previous digit times 10,
plus the last digit*/

num /= 10; // remove the last digit from the number


}

cout<<"Reverse = "<<rev;
getch();
}

Assessed By - Miss Maunika Kamadi


OUTPUT:

*************************************************
* *
* Program for Reverse of a Number *
* *
*************************************************
Enter the Number
154
Reverse = 451

Assessed By - Miss Maunika Kamadi


2. Factorial of a Number
Code:
#include<iostream.h>
#include <conio.h>

void main()
{
clrscr();
cout<<"*************************************************";
cout<<"\n*\t\t\t\t\t\t*\n";
cout<<"*\t Program for Factorial of a Number \t*";
cout<<"\n*\t\t\t\t\t\t*\n";
cout<<"*************************************************\n";
int i, num;
long double fact=1;
cout<<"\nEnter the Number to find its Factorial \n";
cin>>num;
for(i=num ; i>=1 ; i--)
{
fact = fact*i;
}
cout<<"\nFactorial => "<<fact;
getch();
}

Assessed By - Miss Maunika Kamadi


OUTPUT:

*************************************************
* *
* Program for Factorial of a Number *
* *
*************************************************

Enter the Number to find its Factorial


8

Factorial => 40320

Assessed By - Miss Maunika Kamadi


3. Switch case to perform certain operations on a Number
Code:
#include<iostream.h>
#include<conio.h>
#include<math.h> //For Math functions like Sin,Cos & Pow
void main()
{
int num,sw;
float ans;
clrscr();
cout<<"*************************************************";
cout<<"\n*\t\t\t\t\t\t*\n";
cout<<"*\t This is a SWITCH Case Program \t\t*";
cout<<"\n*\t\t\t\t\t\t*\n";
cout<<"*************************************************\n";
cout<<"Choose an Option From the Following\n\n";
cout<<"1.Factrorial"<<endl;
cout<<"2.Power pow(x,y)"<<endl;
cout<<"3.Square root"<<endl;
cout<<"4.Sin"<<endl;
cout<<"5.Cos"<<endl;
cin>>sw;
switch(sw)
{
case 1:
clrscr();

cout<<"*************************************************";
cout<<"\n*\t\t\t\t\t\t*\n";

Assessed By - Miss Maunika Kamadi


cout<<"*\t Program for Factorial of a Number \t*";
cout<<"\n*\t\t\t\t\t\t*\n";

cout<<"*************************************************\n";
int i;
long double fact=1; /*Data types is declared
"long double" to perform operation on bigger numbers */
cout<<"\nEnter the Number to find its Factorial \n";
cin>>num;
for(i=num ; i>=1 ; i--)
{
fact = fact*i;
}
cout<<"\nFactorial => "<<fact; //Display result
getch();
break;

case 2:
clrscr();
int power;
cout<<"Program to find Power of a number"<<endl;
cout<<"Enter the base value"<<endl;
cin>>num;
cout<<"Enter the power\n";
cin>>power;
ans=pow(num,power);
cout<<ans;
getch();
break;

case 3:
clrscr();

Assessed By - Miss Maunika Kamadi


cout<<"Enter the number to find its Squareroot"<<endl;
cin>>num;
cout<<"Squareroot of num="<<sqrt(num);
getch();
break;

case 4:
clrscr();
cout<<"Enter the number to find its Sin"<<endl;
cin>>num;
cout<<"Sin of number="<<sin(num);
getch();
break;
case 5:
clrscr();
cout<<"Enter the number to find its Cos"<<endl;
cin>>num;
cout<<"Cos of number="<<cos(num);
getch();
break;
default:
clrscr();
cout<<"U ENTERED WRONG OPTION !!!!!!!!!!!";
getch();

}
}

Assessed By - Miss Maunika Kamadi


OUTPUT:
*************************************************
* *
* This is a SWITCH Case Program *
* *
*************************************************
Choose an Option From the Following

1.Factrorial
2.Power pow(x,y)
3.Square root
4.Sin
5.Cos

OUTPUT for option 1:

*************************************************
* *
* Program for Factorial of a Number *
* *
*************************************************

Enter the Number to find its Factorial


6

Factorial => 720

Assessed By - Miss Maunika Kamadi


OUTPUT for option 2:

Program to find Power of a number


Enter the base value
2
Enter the power
4
16

OUTPUT for option 3:

Enter the number to find its Squareroot


5
Squareroot of num=2.236068

OUTPUT for option 4:

Enter the number to finds its Sin


5
Sin of number=-0.958924

OUTPUT for option 5:

Enter the number to find its Cos


6
Cos of number=0.96017

OUTPUT for default option:

U ENTERED WRONG OPTION !!!!!!!!!!!

Assessed By - Miss Maunika Kamadi


4. Palindrome of a Number
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
long number,num, digit, rev=0; // initialize three integer variables
clrscr();
cout<<"*************************************************";
cout<<"\n*\t\t\t\t\t\t*\n";
cout<<"*\t Program for Palindrome of a Number \t*";
cout<<"\n*\t\t\t\t\t\t*\n";
cout<<"*************************************************\n";
cout<<"Enter the Number\n";
cin>>num; // Input the number
number=num;

while(num !=0) // while the number does not go to zero


{
digit= num % 10; // keep getting the last digit
rev= rev * 10 + digit;
/* rev stores the previous digit times 10, plus the last digit */

num /= 10; // remove the last digit from the number


}

cout<<"Reverse = "<<rev;
if(number==rev)
{
cout<<"\nNumber is a Palindrome";
}

Assessed By - Miss Maunika Kamadi


else
{
cout<<"\nNumber is not Palindrome";
}
getch();
}

OUTPUT:

*************************************************
* *
* Program for Palindrome of a Number *
* *
*************************************************
Enter the Number
564
Reverse = 465
Number is not Palindrome

*************************************************
* *
* Program for Palindrome of a Number *
* *
*************************************************
Enter the Number
45654
Reverse = 45654
Number is a Palindrome

Assessed By - Miss Maunika Kamadi


5. Studying how to use class and functions in a program
Code:
#include<iostream.h>
#include<conio.h>
class student //student is the name of class
{

int marks1, marks2;


public: // access specifier as public
void getmarks();
/*Declaring a Function & void specifies return type of function getmarks.*/
void totalmarks();
/*Declaring a function & void specifies return type of function totalmarks.*/
};
void student:: getmarks() /*void - return type & student - class name & :: -
scope resolution operator for getmarks*/
{
cout<<"Enter the marks of Two Subjects\n";
cin>>marks1 >> marks2;
}
void student:: totalmarks()
{
cout<<"\nThe marks for two subjects are\n";
cout<<marks1 <<" "<< marks2;
cout<<"\nTotal of Marks =>"<< marks1+marks2;
}
void main()
{
clrscr();
student s; //s is the object of class student
s.getmarks();
s.totalmarks();

Assessed By - Miss Maunika Kamadi


getch();
}

OUTPUT:

Enter the marks of Two Subjects


98
96

The marks for two subjects are


98 96
Total of Marks =>194

Assessed By - Miss Maunika Kamadi


6.
#include<iostream.h>
#include<conio.h>
class student
{
char name[20];
int rollno;
int marks[5];
int total;
float per;
public:
void getdata();
void getmarks();
void showdata();
void showmarks();
};
void student :: getdata()
{
cout<<"\nEnter Name:";
cin>>name;
cout<<"Enter Rollno:";
cin>>rollno;
}
void student :: getmarks()
{
cout<<"\nEnter Marks:\n";
for(int i=0;i<5;i++)
{
cin>>marks[i];
}
}

Assessed By - Miss Maunika Kamadi


void student :: showdata()
{
cout<<"\nName:"<<name<<"\t"<<"Roll No:"<<rollno;
}
void student :: showmarks()
{
total=0;
cout<<"\nMarks:\n";
for(int i=0;i<5;i++)
{
cout<<marks[i]<<"\n";
total=total+marks[i];
}
per = total/5.00;
cout<<"Total="<<total;
cout<<"\nPercentage:"<<per;

}
void main()
{
clrscr();
student s;
s.getdata();
s.getmarks();
s.showdata();
s.showmarks();
getch();
}

Assessed By - Miss Maunika Kamadi


7. Program to obtain result of a BSc.IT student as per the ATKT pattern if the
student is having KT print number of KT and Subject Name in which he failed.
If more than 2KT’s fail & If no kt pass
Code:
#include<iostream.h>
#include<conio.h>
class student //This class contains basic info like Name and Roll no.
{
int rollno; // As it is private cannot be used outside this class
public:
char name[30]; //Name declared in array
void getdata();
void showdata();
};
void student :: getdata() //This function is used to Obtain Name and Roll no
{
cout<<"\nEnter Name:";
cin>>name;
cout<<"Enter Roll no:";
cin>>rollno;
}
void student :: showdata() //This function Displays Name and Roll no
{
cout<<"\nName :"<<name;
cout<<"\tRoll no:"<<rollno;
}

class marks: public student //This class is derived from Student class

Assessed By - Miss Maunika Kamadi


{

public:
int marks[5]; //Marks
void getmarks();
};
void marks :: getmarks()
{
cout<<"\nEnter Marks below for CPP, DBMS, SE, E-Commerce & OS
Respectively:\n";
for(int i=0;i<5;i++)
{
cin>>marks[i];
}
}

class bonus_marks: public marks


{

public:
void bonusgrace();
void total();
void showmarks();
};
void bonus_marks :: bonusgrace()
{
for (int i=0; i<5; i++)
{
if (marks[i] <40 && marks[i] >=35)
marks[i] =40;
}
}

Assessed By - Miss Maunika Kamadi


void bonus_marks :: total()
{
int total=0;
for(int i=0 ; i<5 ;i++)
{
total=total + marks[i];
}
cout<<"\nTotal="<<total<<"\n";
}
void bonus_marks :: showmarks()
{
showdata();
cout<<"\nMarks of "<<name<<" are as follows:\n";
for(int i=0;i<5;i++)
{
cout<<marks[i]<<"\n";
}
}
class atkt : public bonus_marks
{
public:
void kt();

};
void atkt:: kt()
{
int count=0;
for (int i=0; i<5; i++)
{
if(marks[i]<35)
{

Assessed By - Miss Maunika Kamadi


++count;
}
}
if (count>0)
{
cout<<"\nYou Failed in "<<count<<" Subjects .\n";
}

if (marks[0]<35)
cout<<"\nYou failed in CPP";
if (marks[1]<35)
cout<<"\nYou failed in DBMS";
if (marks[2]<35)
cout<<"\nYou failed in SE";
if (marks[3]<35)
cout<<"\nYou failed in E-commerce";
if (marks[4]<35)
cout<<"\nYou failed in OS";

if (count==0)
{
char s=2;
for(int i=0;i<35;i++)
cout<<s;
cout<<"\n\tHurrah!! You Passed\n";
for(i=0;i<35;i++)
cout<<s;
}
else if(count>2)
{
cout<<"\n***********************************\n";

Assessed By - Miss Maunika Kamadi


cout<<"YOU FAILED TO QUALIFY FOR ATKT ";
cout<<"\n***********************************\n";
}
else if (count<=2)
{
cout<<"\n**************************************\n";
cout<<"You Can try for ATKT as ur having "<<count<<" Kt." ;
cout<<"\n**************************************\n";

}
}
void main()
{
clrscr();
atkt r;
r.getdata();
r.getmarks();
r.bonusgrace();
r.showmarks();
r.total();
r.kt();
getch();
}

Assessed By - Miss Maunika Kamadi

You might also like