What is C++?
C++ is a cross-platform language that can be used to create high-performance applications.
C++ was developed by Bjarne Stroustrup, as an extension to the C language.
Why Use C++
C++ is one of the world’s most popular programming languages.
C++ can be found in today’s operating systems, Graphical User Interfaces, and embedded systems.
C++ is an object-oriented programming(OOP) language which gives a clear structure to programs and allows code to be reused, lowering development costs.
C++ is portable and can be used to develop applications that can be adapted to multiple platforms.
C++ is fun and easy to learn!
As C++ is close to C, C# and Java, it makes it easy for programmers to switch to C++ or vice versa.
Difference between C and C++
C++ was developed as an extension of C, and both languages have almost the same syntax.
The main difference between C and C++ is that C++ support classes and objects, while C does not.
Another thing that always appear in a C++ program is
int main()
void main()
This is called a function. Any code inside its curly brackets { }
will be executed.
cout
(pronounced “see-out”) is an object used together with the insertion operator (<<
) to output/print text.
In our example, it will output “Hello World!”.
Note: Every C++ statement ends with a semicolon ;
.
return 0
ends the main function.
Do not forget to add the closing curly bracket }
to actually end the main function.
Program 1 : C++ Program to calculate the total marks and percentage of five subjects.
#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2,n3,n4,n5,sum,per;
clrscr();
cout<<"Program to calculate total and percentage of student";
cout<<"\n Enter five subject marks :";
cin>>n1>>n2>>n3>>n4>>n5;
sum=n1+n2+n3+n4+n5;
cout<<"The total marks obtained by student are "<<sum;
per=sum/5;
cout<<"\nThe percentage is "<<per;
getch();
}
Program 2 : C++ Program to calculate the total marks and percentage of five subjects and also check whether student is pass or fail. Pass condition is percent>=33.
#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2,n3,n4,n5,sum,per;
clrscr();
cout<<"Program to calculate total and percentage of student";
cout<<"\n Enter five subject marks :";
cin>>n1>>n2>>n3>>n4>>n5;
sum=n1+n2+n3+n4+n5;
cout<<"The total marks obtained by student are "<<sum;
per=sum/5;
cout<<"\nThe percentage is "<<per;
if(per>=33)
cout<<"\nPass";
else
cout<<"\nFail";
getch();
}
Program 3 : C++ Program to illustrate the use of classes and objects.
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[20];
float m1,m2,m3;
public:
void input()
{
cout<<"Enter roll number:";
cin>>rno;
cout<<"Enter name:";
cin>>name;
cout<<"Enter marks of three subjects:";
cin>>m1>>m2>>m3;
}
void output()
{
cout<<"The total marks "<<(m1+m2+m3);
}
};
void main()
{
student s1,s2;
s1.input();
s1.output();
s2.input();
s2.output();
getch();
}
Program 4 : C++ Program to display the table of any given number using class and object.
#include <iostream.h>
#include <conio.h>
class TABLE {
private:
int number;
public:
void getNumber()
{
cout << "Enter Number: ";
cin >> number;
}
void table()
{
for (int i = 1; i<= 10; i++)
{
cout << number << "X" << i << " = " << number * i << endl;
}
}
};
void main()
{
TABLE T;
clrscr();
T.getNumber();
T.table();
getch();
}
Program 5 : C++ Program to Check Whether a Character is a Vowel or Consonant. (Vowels are A,E,I,O and U) and other characters are consonant.
#include <iostream.h>
#include <conio.h>
void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' || x == 'o'
|| x == 'u' || x == 'A' || x == 'E' || x == 'I'
|| x == 'O' || x == 'U')
cout << "Vowel" << endl;
else
cout << "Consonant" << endl;
}
void main()
{
clrscr();
vowelOrConsonant('c');
vowelOrConsonant('E');
getch();
}
Program 6 : C++ Program to Implement a Simple Calculator using switch case.
#include <iostream.h>
#include <conio.h>
void main()
{
char op;
float num1, num2;
clrscr();
cout<<"Enter operator +, - , *, / : ";
cin >> op;
cout<<"Enter two numbers : ";
cin >> 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:
cout << "Error! operator is not correct";
}
getch();
}
Program 7 : C++ program to calculate factorial with a class: for eg. The factorial of 5 is 5x4x3x2x1 = 120. And factorial of number 3 is 3x2x1= 6.
#include <iostream.h>
#include <conio.h>
class Factorial
{
private:
int num;
unsigned long long factorial = 1;
public:
void calculateFactorial();
{
cout << "Enter a number:" << endl;
cin >> num;
if (num == 0 || num == 1)
{
factorial = 1;
}
else
{
while (num > 1)
{
factorial = factorial * num;
num--;
}
}
}
void show();
{
cout << "Factorial: " << factorial << endl;
}
};
void main()
{
clrscr();
Factorial factorial;
factorial.calculateFactorial();
factorial.show();
getch();
}
Program 8 : C++ Program to calculate the number of vowels in the given string. (String is a group of characters like name, address, etc.)
#include <iostream.h>
#include <conio.h>
void main()
{
char str[100];
int count = 0;
clrscr();
cout << "Enter a string to test :" << endl;
cin.get(str, 100);
for (int i = 0; str[i] != '\0'; i++)
{
switch (str[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
count++;
}
}
cout << "Total number of vowels found : " << count << endl;
}