Practical Programs of C++ Vaibhav
Practical Programs of C++ Vaibhav
Practical Programs of C++ Vaibhav
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
#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*/
cout<<"Reverse = "<<rev;
getch();
}
*************************************************
* *
* Program for Reverse of a Number *
* *
*************************************************
Enter the Number
154
Reverse = 451
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();
}
*************************************************
* *
* Program for Factorial of a Number *
* *
*************************************************
cout<<"*************************************************";
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();
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();
}
}
1.Factrorial
2.Power pow(x,y)
3.Square root
4.Sin
5.Cos
*************************************************
* *
* Program for Factorial of a Number *
* *
*************************************************
cout<<"Reverse = "<<rev;
if(number==rev)
{
cout<<"\nNumber is a Palindrome";
}
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
OUTPUT:
}
void main()
{
clrscr();
student s;
s.getdata();
s.getmarks();
s.showdata();
s.showmarks();
getch();
}
class marks: public student //This class is derived from Student class
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];
}
}
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;
}
}
};
void atkt:: kt()
{
int count=0;
for (int i=0; i<5; i++)
{
if(marks[i]<35)
{
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";
}
}
void main()
{
clrscr();
atkt r;
r.getdata();
r.getmarks();
r.bonusgrace();
r.showmarks();
r.total();
r.kt();
getch();
}