C++ Programs
C++ Programs
1
Program 1.
Swap Numbers (Using Temporary Variable)
#include <iostream>
using namespace std;
int main ()
{
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
return 0;
}
Output
Before swapping.
a = 5, b = 10
After swapping.
a = 10, b = 5
2
Program 2.
Check Vowel or a Consonant Manually
#include <iostream>
using namespace std;
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;
// evaluates to 1 (true) if c is a
lowercase vowel
isLowercaseVowel = (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 (true) if c is an
uppercase vowel
isUppercaseVowel = (c == 'A' || c == 'E' ||
c == 'I' || c == 'O' || c == 'U');
return 0;
}
Output
Enter an alphabet: u
u is a vowel.
3
Program 3.
Find Largest Number Using if Statement
#include <iostream>
using namespace std;
int main() {
float n1, n2, n3;
Output
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3
4
Program 4.
Fibonacci Series up to n number of terms
#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;
Output
5
Program 5.
C++ Program to Reverse an Integer
#include <iostream>
using namespace std;
int main() {
int n, reversedNumber = 0, remainder;
while(n != 0) {
remainder = n%10;
reversedNumber = reversedNumber*10 +
remainder;
n /= 10;
}
return 0;
}
Output
6
Program 6.
Program to print half pyramid using *
*
* *
* * *
* * * *
* * * * *
#include <iostream>
using namespace std;
int main ()
{
int rows;
7
Program 7.
Prime Numbers Between two Intervals
#include <iostream>
using namespace std;
int checkPrimeNumber(int);
int main() {
int n1, n2;
bool flag;
if(flag)
cout << i << " ";
}
return 0;
}
8
int checkPrimeNumber(int n) {
bool isPrime = true;
return isPrime;
}
Output
Enter two positive integers: 12
55
Prime numbers between 12 and 55 are: 13 17 19
23 29 31 37 41 43 47 53
9
Program 8.
Multiply two matrices without using functions
#include <iostream>
using namespace std;
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1,
r2, c2, i, j, k;
10
cout << "Enter element a" << i + 1 << j +
1 << " : ";
cin >> a[i][j];
}
return 0;
}
11
Output
Enter rows and column for first matrix: 3
2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of
second.
Output Matrix:
24 29
6 25
12
Program 9.
int main()
{
string s1, s2;
s2 = s1;
return 0;
}
Output
Enter string s1: C++ Strings
s1 = C++ Strings
s2 = C++ Strings
13
Program 10.
Store and Display Information Using Structure
#include <iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
student s;
cout << "Enter information," << endl;
cout << "Enter name: ";
cin >> s.name;
cout << "Enter roll number: ";
cin >> s.roll;
cout << "Enter marks: ";
cin >> s.marks;
Output
Enter information,
Enter name: Bill
Enter roll number: 4
Enter marks: 55.6
Displaying Information,
Name: Bill
Roll: 4
14
Marks: 55.6
Program 11.
Length of String Object
#include <iostream>
using namespace std;
int main()
{
string str = "C++ Programming";
return 0;
}
Output
String Length = 15
15
Program 12.
Simple Calculator using switch statement
# include <iostream>
using namespace std;
int main()
{
char op;
float num1, num2;
switch(op)
{
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default:
// If the operator is other than +, -, * or /,
error message is shown
cout << "Error! operator is not correct";
break;
}
return 0;
}
Output
Enter operator either + or - or * or divide: -
Enter two operands:
16
3.4
8.4
Program 13.
Display all Factors of a Number
#include <iostream>
using namespace std;
int main()
{
int n, i;
cout << "Factors of " << n << " are: " << endl;
for(i = 1; i <= n; ++i)
{
if(n % i == 0)
cout << i << endl;
}
return 0;
}
Output
Enter a positive integer: 60
Factors of 60 are: 1 2 3 4 5 6 12 15 20 30 60
17
Program 14.
int main()
{
int n;
return 0;
}
int add(int n)
{
if(n != 0)
return n + add(n - 1);
return 0;
}
Output
Enter an positive integer: 10
Sum = 55
18
Program 15.
WAP displaying any info using Class and Object.
#include <iostream>
#include <string>
using namespace std;
int main() {
MyClass myObj; // Create an object of MyClass
// Print values
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}
19
Program 16.
#include <iostream>
#include <string>
using namespace std;
class Car {
public:
string brand;
string model;
int year;
};
int main() {
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
Car carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
cout << carObj1.brand << " " << carObj1.model << "
" << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << "
" << carObj2.year << "\n";
return 0;
}
Output is:
BMW X5 1999
Ford Mustang 1969
20
Program 17.
Constructor Parameters
#include <iostream>
using namespace std;
int main() {
// Create Car objects and call the constructor with
different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << "
" << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << "
" << carObj2.year << "\n";
return 0;
}
BMW X5 1999
Ford Mustang 1969
21
Program 18.
constructors can also be defined outside the class
#include <iostream>
using namespace std;
int main() {
// Create Car objects and call the constructor with
different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << "
" << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << "
" << carObj2.year << "\n";
return 0;
}
22
Output is:
BMW X5 1999
Ford Mustang 1969
Program 19.
Polymorphism Example
#include <iostream>
#include <string>
using namespace std;
// Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n" ;
}
};
// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee \n" ;
}
};
// Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n" ;
}
};
int main() {
Animal myAnimal;
Pig myPig;
Dog myDog;
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
return 0;
}
Output is :
23
The animal makes a sound
The pig says: wee wee
The dog says: bow wow
Program 20.
int main () {
// Create a text file
ofstream MyWriteFile("filename.txt");
24
// Close the file
MyReadFile.close();
}
Output is :
25