Basic CPP Programs For Beginners

Basic CPP Programs For Beginners

INTRODUCTION OF C++

What is C++?

C++ is an open-source language that allows you to build high-performance software.

Bjarne Stroup developed C++ as an extension of the C Language.

C++ allows programmers to have a high degree of control over memory and system resources.

The language has been updated four major times since 2011, 2014, 2017 and 2020, to C++11 C++14 C++17 C++20.
CPP Programs And Their Output

Why Use C++

C++ is the most popular programming language in the world.

C++ is used in operating systems, GUIs and embedded systems.

C++ is a language that allows for reuse of code and a clearer structure in programs. This reduces development costs.

C++ can be used for developing applications that are adaptable to different platforms.

C++ is easy and fun to learn!

C++ is similar to C ,C# and Java. This makes it easier for programmers who are familiar with C to switch over to C++.

Difference between C and C++

C++ is a language that was developed to extend C. Both languages share a similar syntax.

C++ supports classes and objects while C does.
Get Started

This tutorial will introduce you to the fundamentals of C++.

No prior programming knowledge is required.

1.EVEN OR ODD

#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int n;
cout<<"Enter a number to check: ";
cin>>n;
if(n%2==0)
{
cout<<"It is an even number";
else 
{
cout<<"It is an odd number";
}
getch();
}

OUTPUT

EVEN OR ODD

2.LARGEST OF THREE NUMBERS

#include<iostream.h>
#include<conio.h>
void main()
 {
clrscr();
int a, b, c;
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
cout<<"Enter third number: ";
cin>>c;
if(a>b && a>c)
{
cout<<a<<" is the largest number";
}
else if(b>c)
{
cout<<b<<" is the largest number";
} else 
{
cout<<c<<" is the largest number";
getch();
} }

OUTPUT

3.MENU CALCULATOR

#include<iostream.h>
#include<conio.h>
void main()
 {
clrscr();
int a, b, c, d;
cout<<"MENU\n------\n"; cout<<"1.ADDITION\n2.SUBTRACTION\n3.MULTIPLICATION\n4.DIVISION\n5.MODULUS
DIVISION\n";
cout<<"Enter an option from menu: ";
cin>>a;
if(a==1) 
{
cout<<"Enter two numbers:";
cin>>b>>c;
d=b+c;
cout<<"Sum is = "<<d;
}
else if(a==2)
{
cout<<"Enter two numbers:";
cin>>b>>c;
d=b-c;
cout<<"Subtraction is = "<<d;
}
else if(a==3)
{
cout<<"Enter two numbers:";
cin>>b>>c;
d=b*c;
cout<<"Multiplication is = "<<d;
}
else if(a==4)
{
cout<<"Enter two numbers:";
cin>>b>>c;
d=b/c;
cout<<"Division is = "<<d;
}
else if(a==5)
{
cout<<“Enter two numbers:”;
cin>>b>>c;
d=b%c;
cout<<“modulus division is=“<<d;
else
{
cout<<"Enter two numbers:";
cin>>b>>c;
d=b%c;
cout<<"Modulus division is = "<<d;
}
else{
cout<<"Invalid operation.";
getch();
}

OUTPUT

4.SUM OF FIRST N NATURAL NUMBERS

#include<iostream.h>
#include<conio.h>
void main() 
{
clrscr();
int n, i, sum = 0;
cout << "Enter a number: ";
cin >> n;
for (i = 1; i <= n; i++)
{
sum = sum + i;
}
cout<<"Sum of " <<n<<" natural numbers is = "<< sum; 
getch();
}

OUTPUT

SUM OF FIRST N NATURAL NUMBERS

5. FACTORIAL

#include<iostream.h>
#include<conio.h>
void main() 
{
clrscr();
int n, i, f = 1;
cout << "Enter a number: ";
cin >> n;
for (i = 1; i <= n; i++) {
f = f * i;
}
cout<<"Factorial of "<<n <<" is = "<< f;
getch(); 
}

OUTPUT

FACTORIAL

6.SUM OF DIGITS OF A NUMBER

#include<iostream.h>
#include<conio.h>
void main() 
{
clrscr();
int n,t,r,sum=0;
cout << "Enter a number: ";
cin >> n;
t=n;
while(n>0) 
{
r=n%10;
sum=sum+r;
n=n/10;
}
cout<<"Sum of the digits of "<<t<<" is : "<<sum;
getch();
}

OUTPUT

SUM OF DIGITS OF A NUMBER

7. ARMSTRONG

#include<iostream.h>
#include<conio.h>
void main() 
{
clrscr();
int n,r,t,sum=0;
cout<< "Enter a number: ";
cin>> n;
t=n;
while(n>0) {
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(sum==t) {
cout<<"It is an armstrong number";
} else 
{
cout<<"It is not an armstrong number";
}
getch(); 
}

OUTPUT

Amstrong

8. REVERSE OF A NUMBER

#include<iostream.h>
#include<conio.h>
void main()
 {
clrscr();
long int n,r,rev=0;
cout<< "Enter a number: ";
cin>> n;
while(n>0) 
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
cout<<"Reverse of the number is : "<<rev;
getch();
}

OUTPUT

reverse of a number

9. PALINDROME

#include<iostream.h>
#include<conio.h>
void main() 
{
clrscr();
long int n,r,t,rev=0;
cout<< "Enter a number: ";
cin>> n;
t=n;
while(n>0) {
r=n%10;
rev=rev*10+r;
n=n/10;
}
if(rev==t)
{
cout<<"It is a Palindrome number";
} else 
{
cout<<"It is not a palindrome number";
}
getch();

OUTPUT

palliandrome

10. SPY NUMBER

#include<iostream.h>
#include<conio.h>
void main() 
{
clrscr();
int n,r,sum=0, product=1;
cout<< "Enter a number: ";
cin>> n;
while(n>0)
 {
r=n%10;
sum=sum+r;
product=product*r;
n=n/10;
}
if(sum==product)
{
cout<<"It is a spy number";
}
 else 
{
cout<<"It is not a spy number";
}
getch();
}

OUTPUT

spy number

11.PRIME NUMBER

#include<iostream.h>
#include<conio.h>
void main() 
{
clrscr();
int i,n,r;
cout<< "Enter a number: ";
cin>> n;
for(i=2;i<n;i++)
{
if(n%i==0) {
cout<<"It is not a prime number";
break; }
}
 if(i==n) 
{
cout<<"It is a prime number";
getch();
} }
} }

OUTPUT

prime number

12. SORT N NUMBERS

#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int i,j,n,c,a[20];
cout<<"Enter the number of elements you want to store: "; cin>>n;
cout<<"Enter array elements: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Array elements before sorting:\n "; for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
c=a[i];
a[i]=a[j];
a[j]=c;
} }
}
cout<<"\nArray elements after sorting:\n "; for(i=0;i<n;i++)
{
} }
cout<<a[i]<<"\t";
getch();


}

OUTPUT

SORT N NUMBERS

13. MATRIX ADDITION

#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int i,j,n,r,c,a[10][10],b[10][10]; cout<<"Enter number of rows:";
cin>>r;
cout<<"Enter number of columns: ";
cin>>c;
cout<<"Enter elements of first matrix:\n"; for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{

}
} }
cin>>a[i][j];
} }
cout<<"Enter elements of second matrix:\n"; for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>b[i][j];
} }
cout<<"Sum of the two matrix :\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j]+b[i][j]<<"\t";
cout<<"\n";
getch();
}

OUTPUT

MATRIX ADDITION

14. STRING PALINDROME

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main() {
clrscr();
char s1[20], s2[20];
cout<<"Enter a string: ";
cin>>s1;
strcpy(s2,s1);
strrev(s1);
if(strcmp(s1,s2)==0)
{
cout<<"The given string is palindrome";
} else {
cout<<"The given string is not palindrome";
 getch();
}

OUTPUT

STRING PALINDROME

15. FUNCTION OVERLOADING

#include<iostream.h>
#include<conio.h>
void area(int l, int b);
void area(double r);
void area(int s);
void area(double base, double h);
} }

void main() {
clrscr();
int l,b,s;
double r,base,h;
cout<<"Enter the length of the rectangle”;
cin>>l;
cout<<"Enter the  breadth of the rectangle”;
cin>>b;
area(l,b);
cout<<"\n\nEnter  radius  of the Circle”;
cin>>r;
area(r);
cout<<"\n\nEnter the side of the square”;
cin>>s;
area(s);
cout<<"\n\nEnter base of the triangle”;
cin>>base;
cout<<"Enter the height of the triangle”;
cin>>h;
area(base,h);
getch();
}
void area(int l, int b)
{
int a;
a=l*b;
cout<<"Area of the rectangle: "<<a;
}
void area(double r)
{
double a;
a=3.14*(r*r);
cout<<"Area of the circle: "<<a;
}
void area(int s)
{
int a;
a=s*s;
cout<<"Area of the square: "<<a;
}
void area(double base, double h)
{
}
int a;
a=(base*h)/2;
cout<<"Area of the triangle: "<<a;

OUTPUT

FUNCTION OVERLOADING

Download the pdf of CPP programs

Previous Post Next Post

نموذج الاتصال