You are on page 1of 25

1)Write a C++ program to reverse a given

number.
#include <iostream.h>
using namespace std;

int main()
{
int n, reversedNumber = 0, remainder;
cout << "Enter an integer: ";
cin >> n;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 +
remainder;
n /= 10;
}
cout << "Reversed Number = " << reversedNumber;
return 0;
}

Output

Enter an integer: 12345

Reversed number = 54321


2)Write a C++ program to add numbers using
class
#include <iostream.h>
class maths
{
int x,y;
public:
void input()
{
cout<<"Input two intergers \n";
cin>>x>>y;
}
void add()
{
cout<<"Result:"<<x+y;
}
};
int main()
{
mathematics m;
m.input();
m.add();
return 0;
}
OUTPUT:
Input two integers 3,4
result 7.
3)Write a C++program to check whether a given
number is prime.
#include <iostream.h>
using namespace std;
int main ()
{
int num, i, count = 0;
cout << "Enter the number to be checked : ";
cin >> num;
if (num == 0)
{
cout << "\n" << num << " is not prime";
exit(1);
}
else {
for(i=2; i < num; i++)
if (num % i == 0)
count++;
}
if (count > 1)
cout << "\n" << num << " is not prime.";
else
cout << "\n" << num << " is prime.";
return 0;
}
OUTPUT:
Case 1 :
Enter the number to be checked : 5
5 is prime.
Case 2 :
Enter the number to be checked : 0
0 is not prime.
Case 3 :
Enter the number to be checked : 28
28 is not prime.
4)Write a C++ program to demonstrate the usage of scope
resolution operator.
#include <iostream>

using namespace std;

char c = 'a'; // global variable

int main()

char c = 'b'; //local variable

cout << "Local variable: " << c << "\n";

cout << "Global variable: " << ::c << "\n"; //using scope
resolution operator

return 0;

OUTPUT:
Local variable: b
Global variable: a
5. Write a C++ program to check whether a given year is leap
year or not

#include <iostream>
using namespace std;

int main()
{
int year;

cout << "Enter the year in yyyy format : ";


cin >> year;
if (year % 4 == 0)
cout << year << " is a leap year";
else
cout << year << " is not a leap year";
}

OUTPUT:

Enter the year in yyyy format : 2013


2013 is not a leap year

Enter the year in yyyy format : 2012


2012 is a leap year
6. Write a C++ program to add two numbers using functions.

#include <iostream>
using namespace std;

//function declaration
int addition(int a,int b);

int main()
{
int num1; //to store first number
int num2; //to store second number
int add; //to store addition

//read numbers
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;

//call function
add=addition(num1,num2);

//print addition
cout<<"Addition is: "<<add<<endl;

return 0;
}

//function definition
int addition(int a,int b)
{
return (a+b);
}

OUTPUT:
Enter first number: 100
Enter second number: 200
Addition is: 300
7) Write a C++ Program to accept and display the details of a
student using class

/*C++ program to create class for a student.*/


#include <iostream>
using namespace std;

class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};

//member function definition, outside of the class


void student::getDetails(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks out of 500: ";
cin >> total;
}

//member function definition, outside of the class


void student::putDetails(void)
{
cout << "Student details:\n";
cout << "Name:"<< name;
cout << ",Roll Number:" << rollNo;
cout << ",Total:" << total;
}

int main()
{
student std; //object creation
std.getDetails();
std.putDetails();

return 0;
}

OUTPUT:
Enter name: mike
Enter roll number: 112
Enter total marks out of 500: 456
Student details:
Name :mike
Roll Number:112
Total:456
8.Write a C++ program to accept and display the details of an
employee using a class.
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib>
using namespace std;

class staff
{
int staff_id;
char name[20];
float salary;
public:
staff()
{

}
void accept()
{
cout<<"\n Enter Staff Id : ";
cin>>staff_id;
cout<<"\n Enter Staff Name : ";
cin>>name;
cout<<"\n Enter Salary : ";
cin>>salary;
}
void display();
friend void sort(char nm[], int n, staff *s);
void operator =(staff s1);
};
void staff::operator=(staff s1)
{
staff_id = s1.staff_id;
strcpy(name,s1.name);
salary = s1.salary;
}
void staff::display()
{
cout<<"\n Staff Id : "<<staff_id;
cout<<"\n Name : "<<name;
cout<<"\n Salary : "<<salary<<"\n";
}
void sort(char nm[], int n, staff *s) //Function for sorting the data by
employee name
{
staff temp;
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
int r = strcmp(s[i].name,s[j].name);
if(r>0)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
}
int main()
{
int ch;
staff *s;
int n;
cout<<"\n Enter No.of Records You Want : ";
cin>>n;
s = new staff[n];
do
{
cout<<"\n 1. Accept Data ";
cout<<"\n 2. Display Data ";
cout<<"\n 3. Sort Data by Name ";
cout<<"\n 4. Exit: ";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
for(int i=0; i<n; i++)
{
cout<<"\n\n Enter Data for Employee "
<<i+1<<"\n";
s[i].accept();
}
break;
case 2:
for(int i=0; i<n; i++)
{
s[i].display();
}
break;
case 3:
sort("a", n, s); //Passing parameter to sort() function
cout<<"\n Data is Sorted!!!\n";
break;
case 4:
exit(0);
default:
cout<<"\n Invalid Choice . . .";
}
}
while(ch!=4);
return 0;
}
9.Write a C++ program to count the number of words and
characters in a given text.
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int main()
{
char str[155];
int i=0,chr=0,st=1;
cout<<"enter the string:";
gets(str);
for(i=0;str[i]!='\0';i++)
{
chr=chr+1;
}
cout<<"total characters:"<<chr;
for(i=0;i<=chr-1;i++)
{
if(str[i]==' ')
{
st=st+1;
}
}
cout<<"\n total words:"<<st;
}
Output:
Enter the string: The School
Total characters:10
The total words:2
10. Write a C++ program to compare two strings using string
function

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str1[100], str2[100];
cout<<"Enter first string : ";
gets(str1);
cout<<"Enter second string : ";
gets(str2);
if(strcmp(str1, str2)==0)
{
cout<<"Both the strings are equal";
}
else
{
cout<<"Both the strings are not
equal";
}
getch();
}
OUTPUT:
11. Write a program to find GCD of two numbers.
#include <iostream>
using namespace std;

int main()
{
int n1, n2;

cout << "Enter two numbers: ";


cin >> n1 >> n2;

while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}

cout << "GCD = " << n1;


return 0;
}

OUTPUT:

Enter two numbers: 78

52

GCD = 26
12. Write a C++ program to find area of Square and rectangle
using function overloading
#include<iostream.h>
#include<conio.h>
int area(int)
int area(int,int)
int main()
{
clrscr();
int s,l,b;
cout<<"Enter side of a Square:";
cin>>s;
cout<<"Enter length and breadth of Rectangle:";
cout>>l>>b;
cout<<"\n Area of square is:"<<area(s);
cout<<"\n Area of rectangle is:"<<area(l,b);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
OUTPUT:
Enter side of a Square:4
Enter length and breadth of Rectangle:5 7
Area of Square is:16
Area of Rectangle is:35
13. Write a c++ program to add two numbers using pointers

1. #include<iostream.h>
2. #include<conio.h>
3. void main()
4. {
5. int f,s,*p,*q,sum;
6. cout<<"Enter two integers to add\n";
7. cin>>f>>s;
8. p= &f;
9. q=&s;
10. sum= *p + *q;
11. cout<<"Sum of 2 numbers="<< sum;
12. getch();
13. }

OUTPUT:
Enter two integers to add 3 4
Sum of 2 numbers =7
14 Write a C++ program to find factorial of a given number

#include <iostream.h>
using namespace std;
int main()
{
unsigned int n;
unsigned long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
return 0;
}

Output

Enter a positive integer: 12

Factorial of 12 = 479001600
15. Write a C++ program to search for an element using binary
search
#include <iostream.h>
using namespace std;

int main()
{
int count, i, arr[30], num, first, last, middle;
cout<<"how many elements would you like to enter?:";
cin>>count;

for (i=0; i<count; i++)


{
cout<<"Enter number "<<(i+1)<<": ";
cin>>arr[i];
}
cout<<"Enter the number that you want to search:";
cin>>num;
first = 0;
last = count-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < num)
{
first = middle + 1;

}
else if(arr[middle] == num)
{
cout<<num<<" found in the array at the
location "<<middle+1<<"\n";
break;
}
else {
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<num<<" not found in the array";
}
return 0;
}
Output:

how many elements would you like to enter?:5


Enter number 1: 12
Enter number 2: 45
Enter number 3: 8
Enter number 4: 9
Enter number 5: 100
Enter the number that you want to search:8
8 found in the array at the location 3

You might also like