0% found this document useful (0 votes)
29 views6 pages

C++ Practicle Ques

The document contains 15 C++ programs covering topics such as: 1) Reversing an integer number 2) Using relational operators 3) Function overloading for area calculation 4) Creating and reading from a file 5) Inheritance concept 6) Call by value and address swapping 7) Factorial calculation using for loop 8) Finding min and max elements in an array 9) Reversing an array 10) Checking for palindrome numbers 11) Checking if a number is prime 12) Grading system using switch case 13) Calculating sum and average of array elements

Uploaded by

Anand Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

C++ Practicle Ques

The document contains 15 C++ programs covering topics such as: 1) Reversing an integer number 2) Using relational operators 3) Function overloading for area calculation 4) Creating and reading from a file 5) Inheritance concept 6) Call by value and address swapping 7) Factorial calculation using for loop 8) Finding min and max elements in an array 9) Reversing an array 10) Checking for palindrome numbers 11) Checking if a number is prime 12) Grading system using switch case 13) Calculating sum and average of array elements

Uploaded by

Anand Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

reverse a no program
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int reverse=0;
while(n>0){
int lastdigit=n%10;
reverse=reverse*10+lastdigit;
n=n/10;
}
cout<<reverse;
return 0;
}

2. relational operators
#include<iostream>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<"a==b is"<<(a==b)<<endl;
cout<<"a!=b is"<<(a!=b)<<endl;
cout<<"a>b is"<<(a>b)<<endl;
cout<<"a<b is"<<(a<b)<<endl;
cout<<"a<=b is"<<(a<=b)<<endl;
cout<<"a>=b is"<<(a>=b)<<endl;
return 0;
}

3.area of rectangle nd cicle by func overloading

#include<iostream>
using namespace std;
int area(int l,int b){
return (l*b);
}
int area (int r){
return (3.14*r*r);
}
int main(){
cout<<"area of circle "<<area(3)<<endl;
cout<<"area of rectangle "<<area(10,20);
return 0;
}
4.create file insert date nd display it

#include<iostream>
#include<fstream>
using namespace std;
int main(){
ofstream out("file.cpp");
string name;
cout<<"enter name";
cin>>name;
out<<name;
out.close();
ifstream in("file.cpp");
string c;
in>>c;
cout<<"content is"<<c;
in.close();
}
5. program of inheritance

#include<iostream>
using namespace std;
class parent {
public:
void display(){
cout<<"parent"<<endl;
}
};
class child:public parent{
public:
void disp(){
cout<<"child";
}
};
int main(){
child obj;

obj.display();
obj.disp();
return 0;
}

6.swapping call by value with third variable

include<iostream>
using namespace std;
void swap(int x,int y){
int temp;
temp=x;
x=y;
y=temp;
cout<<"after swapping inside the function a="<<x<<" ,b="<<y<<endl;
}
int main(){
int a,b;
a=10;
b=20;
cout<<" before swapping a="<<a<<",and b="<<b<<endl;
swap(a,b);
cout<<" after swapping outside the function a="<<a<<",and b="<<b;
return 0;

7.swapping call by address with third variable


#include<iostream>
using namespace std;
void swap(int *x,int *y){
int temp;
temp=*x;
*x=*y;
*y=temp;
}
int main(){
int a,b;
a=10;
b=20;
cout<<" before swapping a="<<a<<",and b="<<b;
swap(&a,&b);
cout<<" after swapping a="<<a<<",and b="<<b;
return 0;

}
8.without thirld variable swapping nd call by value

#include<iostream>
using namespace std;
void swap(int x,int y){
int temp;
x=x+y;
y=x-y;
x=x-y;
cout<<" after swapping inside the function a="<<x<<",and b="<<y<<endl;
}
int main(){
int a,b;
a=10;
b=20;
cout<<" before swapping a="<<a<<",and b="<<b<<endl;
swap(a,b);
cout<<" after swapping outside the function a="<<a<<",and b="<<b;
return 0;

}
9.factorial using for loop
#include<iostream>
using namespace std;
int main (){
int fact=1;
int n;
cout<<"enter no";
cin>>n;
for(int i=1;i<=n;i++){
fact=fact*i;

}
cout<<"factorial is "<<fact;
return 0;
}
10. min nd max elements of an array

#include<iostream>
using namespace std;
int main(){
int a[5];
int max,min;
cout<<"enter elements ina array";
for(int i=0;i<5;i++){
cin>>a[i];
}
max=a[0];
min=a[0];

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

if(a[i]>max)
max=a[i];
if (a[i]<min)
min=a[i];

}
cout<<"maximum element"<<max<<endl;
cout<<"minimum element"<<min;

return 0;
}
11. reversing of array

#include <iostream>
using namespace std;

void revarray(int arr[],int size){


int j,i,temp;
for(i=0,j=size-1;i<j;i++,j--)

temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;

}
int main(){
int arr[]={1,2,3,4,5};
int size=sizeof (arr)/sizeof(arr[0]);
cout<<"original array";
for(int i=0;i < size;i++){
cout<<arr[i]<<" ";

}
cout<<endl;
revarray(arr,size);
cout<<" reversed array";
for(int i=0;i < size;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;

}
12.palindrome program

#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int lastdigit;
int original_n=n;
int reverse=0;
while(n>0){
lastdigit=n%10;
reverse=reverse*10+lastdigit;
n=n/10;

}
cout<<"reverse of"<<n<<" is "<<reverse<<endl;
if(original_n==reverse){
cout<<"palindrome number";
}
else{
cout<<"not a palindrome number";
}
return 0;
}
13.prime or not

#include<iostream>
using namespace std;
int main(){
int n;
int count=0;
cout<<"enter n";
cin>>n;
for(int i=1;i<=n;i++){
if(n%i==0){
count++;
}
}
if(count==2){
cout<<"prime no";
}
else {
cout<<"not a prime no";
}
return 0;
}
14.grade of a student using switch statement

#include<iostream>
using namespace std;
int main(){
int marks;
cout<<"enter marks";
cin>>marks;
switch(marks/10){
case 10:
cout<<"a+";*
break;
case 9:
cout<<"A";
break;
case 8:
cout<<"B";
break;
case 7:
cout<<"c";
break;
case 6:
cout<<"d";
break;
default:
cout<<"fail";
return 0;
}
15. sum nd averge of array elements

#include<iostream>
using namespace std;
int main(){
int a[5];
int sum=0,avg;
for(int i=0;i<5;i++){
cin>>a[i];
sum=sum+a[i];

}
cout<<"sum of array elements is "<<sum<<endl;
cout<<"average of array elements is"<<(sum/5);
return 0;
}

You might also like