PROGRAM NO.
Write a simple program to Print your Introduction.
#include <iostream>
using namespace std;
int main()
{
cout << "My name is Sudesh Sahu" << endl;
cout << "Course: BCA 2nd semester" << endl;
return 0;
}
Output—
My name is Sudesh Sahu
Course: BCA 2nd semester
--------------------------------
Process exited after 0.7766 seconds with return value 0
Press any key to continue . . .
1
PROGRAM NO. 2
Write C++ program to calculate sum of two numbers.
#include<iostream>
using namespace std;
int main()
{
int sum,a,b;
cout<<"Enter value of a:"<<endl;
cin>>a; cout<<"Enter value of b:"<<endl;
cin>>b; cout<<"The sum of a and b is "<<(a+b);
return 0;
}
OUTPUT—
Enter value of a:
10
Enter value of b:
50
The sum of a and b is 60
--------------------------------
Process exited after 9.425 seconds with return value 0
Press any key to continue . . .
2
PROGRAM NO. 3
Write C++ Program to print your name 10 times.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ int i;
for(i=1;i<=10;++i) cout<<"Sudesh Sahu"<<"\n";
return 0;
}
OUTPUT—
Sudesh Sahu
Sudesh Sahu
Sudesh Sahu
Sudesh Sahu
Sudesh Sahu
Sudesh Sahu
Sudesh Sahu
Sudesh Sahu
Sudesh Sahu
Sudesh Sahu
--------------------------------
Process exited after 1.256 seconds with return value 0
Press any key to continue . . .
3
PROGRAM NO. 4
Write a program to swap two numbers in C++.
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
cout<<a<<" "<<b<<endl;
int temp = a;
a = b;
b = temp;
cout<<a<<" "<<b<<endl;
return 0;
}
OUTPUT—
10 20
20 10
--------------------------------
Process exited after 1.063 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 5
4
Write C++ Program to Accept Student Roll No, Marks in 3 Subjects and
Calculate Total, Average and Print it.
#include<iostream>
# include <conio.h>
using namespace std;
int main()
{
int a,b,c,tot, avg;
cout<<"ENTER STUDENT ROLL NO ; "<<endl;
cin>>a;
cout<<"ENTER FIRST SEMESTER MARKS ;"<<endl;
cin>>b;
cout<<"ENTER SECOND SEMESTER MARKS;"<<endl;
cin>>c; tot=b+c; avg=tot/2;
cout<<"\n\n\t\t V.B.S.Purvanchal University Jaunpur \n\n";
cout<<"\t STUDENT ROLL NO :"<<a<<endl;
cout<<"\t FIRST SEMESTER MARKS :"<<b<<endl;
cout<<"\t SECOND SEMESTER MARKS :"<<c<<endl;
cout<<"\t AVERAGE MARKS :"<<avg<<endl;
return 0;
}
OUTPUT—
ENTER STUDENT ROLL NO ;
5
94
ENTER FIRST SEMESTER MARKS ;
61
ENTER SECOND SEMESTER MARKS;
90
V.B.S.Purvanchal University Jaunpur
STUDENT ROLL NO :94
FIRST SEMESTER MARKS :60
SECOND SEMESTER MARKS :90
AVERAGE MARKS :75
--------------------------------
Process exited after 19.03 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 6
6
Write a C++ Program to Print ODD numbers from 1 to 20.
# include <iostream>
using namespace std;
int main( )
{
int i;
for (i=1; i<=20; i+=2) cout<<i<<" ";
return 0;
}
OUTPUT—
1 3 5 7 9 11 13 15 17 19
--------------------------------
Process exited after 2.799 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 7
Write a C++ Program to Print Natural numbers from 1 to10 in Reverse.
7
# include <iostream>
using namespace std;
int main( )
{ int i;
for (i=10; i>=1; i--)
cout<<i<<" ";
return 0;
}
OUTPUT—
10 9 8 7 6 5 4 3 2 1
--------------------------------
Process exited after 1.444 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 8
Write C++ Program to print table of any number.
#include<iostream>
#include<conio.h>
8
using namespace std;
int main()
{
int i,n;
cout<<"Enter number for which you want to generate table:";
cin>>n;
cout<<"\n";
for(i=1;i<=10;++i)
cout<<"\t"<<n<<"*"<<i<<"="<<n*i<<"\n";
return 0;
}
OUTPUT—
Enter number for which you want to generate table:15
15*1=15
15*2=30
15*3=45
15*4=60
9
15*5=75
15*6=90
15*7=105
15*8=120
15*9=135
15*10=150
--------------------------------
Process exited after 6.967 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 9
Write a C++ program to find the largest number among three numbers.
#include <iostream>
using namespace std;
int main()
{
10
float a, b, c;
cin >> a >> b >> c;
if(a >= b && a >= c)
cout << "Largest number: " << a;
if(b >= a && b >= c)
cout << "Largest number: " << b;
if(c >= a && c >= b)
cout << "Largest number: " << c;
return 0;
}
OUTPUT—
Input: 1 2 3
Largest number: 3
PROGRAM NO. 10
Square Root of Number program in C++.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float number, root;
cout << "Enter number whose root is to be found: ";
11
cin >> number;
root = sqrt(number);
cout << "Square root of " << number << " is " << root;
return 0;
}
OUTPUT—
Enter number whose root is to be found: 5
Square root of 5 is 2.23607
--------------------------------
Process exited after 3.316 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 11
Program to Add two numbers using pointers in C++.
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
int *ptr1, *ptr2;
int sum;
cout << "\n Enter first number: ";
cin >> num1;
cout << "\n Enter second number: ";
12
cin >> num2;
ptr1 = &num1;
ptr2 = &num2;
sum = *ptr1 + *ptr2;
cout << "\n Sum is: " << sum;
return 0;
}
OUTPUT—
Enter first number: 5
Enter second number: 10
Sum is: 15
--------------------------------
Process exited after 5.991 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 12
Program to find Cube Root of Number in C++.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
float number, ans;
cout << "Enter any number: ";
cin >> number;
ans = cbrt(number);
13
cout << "\n Cube Root of " << number << " is: " << ans;
}
OUTPUT—
Enter any number: 10
Cube Root of 10 is: 2.15443
--------------------------------
Process exited after 5.359 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 13
Write C++ program to find sum of series 1 + 2 + 3 +......+ n.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,n,sum=0;
cout<<"1+2+3+......+n";
cout<<"\nEnter the value of n:";
cin>>n; for(i=1;i<=n;++i)
sum+=i;
14
cout<<"\nSum="<<sum;
return 0;
}
OUTPUT—
1+2+3+......+n
Enter the value of n:4
Sum=10
--------------------------------
Process exited after 5.24 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 14
Write C++ program to Protect your Roll no by Using Access Modifier.
#include <bits/stdc++.h>
using namespace std;
class Parent
{
protected:
int id_protected;
};
class Child : public Parent
{
public:
void setId(int id)
{
15
id_protected = id;
}
void displayId()
{
cout << "id_protected is: " << id_protected << endl;
}
};
int main() {
Child obj1;
obj1.setId(94);
obj1.displayId();
return 0;
}
OUTPUT—
id_protected is: 94
--------------------------------
Process exited after 1.541 seconds with return value 0
Press any key to continue . . .
16
PROGRAM NO. 15
Write C++ Program to Display your Intrduction by Using Public Access
Modifier.
#include<iostream>
using namespace std;
class demo
{
public:
void show()
{
cout<<"Sudesh Sahu"<<endl;
cout<<"PU22/100094"<<endl;
cout<<"BCA 2nd Sem";
}
};
17
int main()
{
demo obj;
obj.show();
}
OUTPUT—
Sudesh Sahu
PU22/100094
BCA 2nd Sem
--------------------------------
Process exited after 1.359 seconds with return value 0
Press any key to continue . . .
18