Cpp Foundation Programs
Cpp Foundation Programs
#include <iostream>
using namespace std;
int main()
{
int first, second, sum;
return 0;
}
2. Write a C++ Program to Find Size of Int Float Double and Char data types.
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "\nSize of int: " << sizeof(int) << " bytes" << endl;
cout << "\nSize of float: " << sizeof(float) << " bytes" << endl;
cout << "\nSize of double: " << sizeof(double) << " bytes" << endl;
return 0;
}
3. Write a C++ Program to Find Sum and Average of three numbers.
#include<iostream>
using namespace std;
int main()
{
float a,b,c,sum,avg;
cout<<"Enter 1st number : ";
cin>>a;
cout<<"\nEnter 2nd number : ";
cin>>b;
cout<<"\nEnter 3rd number : ";
cin>>c;
sum=a+b+c;
avg=sum/3;
return 0;
}
#include<iostream>
#include<math.h> //for pow() function
using namespace std;
int main()
{
int x,n,result;
result=pow(x,n);
return 0;
}
5. Write a C++ Program to find Square Root of a number using sqrt() function.
#include<iostream>
#include<math.h>
int main()
{
float sq,n;
sq=sqrt(n);
return 0;
}
6. Write a C++ Program to Check Character is Uppercase, Lowercase, Digit or Special Character.
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter any character to check : ";
cin>>ch;
if(ch>=65&&ch<=90)
{
cout<<"\n The Entered Character "<<ch<<" is an UPPERCASE character.\n";
}
else if(ch>=48&&ch<=57)
{
cout<<"\n The Entered Character "<<ch<<" is a DIGIT.\n";
}
else if(ch>=97&&ch<=122)
{
cout<<"\n The Entered Character "<<ch<<" is a LOWERCASE character.\n";
}
else
{
cout<<"\n The Entered Character "<<ch<<" is an SPECIAL character.\n";
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i,n;
if(n==1)
{
cout<<"\nSmallest prime number is : 2";
}
for(i=2;i<n;i++)
{
if(n%i==0)
{
cout<<"\nThe Entered Number "<<n<<" is NOT a prime number.\n";
break;
}
}
if(n==i)
{
cout<<"\nThe Entered Number "<<n<<" is a prime number.\n";
}
return 0;
}
8. Write a C++ Program to Find the Number of Digits in a number.
#include<iostream>
using namespace std;
int main()
{
int n,no,a=0;
no=n;
while(no>0)
{
no=no/10;
a++;
}
cout<<"\nNumber of Digits in a number :"<<n<<" is : "<<a<<"\n";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i,no, first=0, second=1, next;
first=0;
second=1;
#include<iostream>
using namespace std;
int main()
{
int mark[5], i;
float sum=0;
cout<<"\nEnter marks obtained in Physics, Chemistry, Maths, CS, English : \n";
for(i=0; i<5; i++)
{
cout<<"\nEnter mark "<<i+1<<" : ";
cin>>mark[i];
sum=sum+mark[i];
}
float avg=sum/5;
float perc;
perc=(sum/500)*100;
cout<<"\nAverage Marks of 5 Subjects = "<<avg<<" \n";
cout<<"\nPercentage in 5 Subjects = "<<perc<<"% \n";
return 0;
}
11. Write a C++ Program to find Largest Element in an Array.
#include<iostream>
using namespace std;
int main()
{
int i,a[50],size;
cout<<"Enter array size( Max:50 ) : ";
cin>>size;
cout<<"\nEnter array elements : \n";
for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}
int largest=a[0];
for (i=0;i<size;i++)
{
if(a[i]>largest)
{
largest=a[i];
}
}
cout<<"\n\nLargest Element in an Array : "<<largest<<endl;
return 0;
}
12. Write a C++ Program to Reverse an Array using functions.
#include <iostream>
using namespace std;
int main()
{
int i,a[50],size;
cout<<"Enter array size( Max:50 ) : ";
cin>>size;
cout<<"\nEnter array elements : \n";
for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}
// Calling Reverse Array Values Function
Reverse_Array(a,size);
cout << "\n\nReversed Array Values are : " << endl;
for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}
cout<<"\n";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i,j,a[50],size;
cout<<"Enter array size( Max:50 ) : ";
cin>>size;
cout<<"\nEnter array elements : \n";
for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}
cout<<"\n";
return 0;
}
14. Write a C++ Program to Delete an element in an array at desired position.
#include<iostream>
using namespace std;
int main()
{
int i,a[50],no,pos,size;
cout<<"Enter array size( Max:50 ) : ";
cin>>size;
cout<<"\nEnter array elements : \n";
for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}
if(pos>size)
{
cout<<"\nThis is out of range.\n";
}
else
{
--pos;
for(i=pos;i<=size-1;i++)
{
a[i]=a[i+1];
}
cout<<"\nNew Array is : \n\n";
for(i=0;i<size-1;i++)
{
cout<<" "<<a[i]<<" ";
}
}
cout<<"\n";
return 0;
}
15. Write a C++ Program to Insert an element in an array at specific position.
#include<iostream>
using namespace std;
int main()
{
int i,a[50],no,pos,size;
cout<<"Enter array size( Max:50 ) : ";
cin>>size;
cout<<"\nEnter array elements : \n";
for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}
if(pos>size)
{
cout<<"\nThis is out of range.\n";
}
else
{
cout<<"\nEnter number to be inserted : ";
cin>>no;
--pos;
for(i=size;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=no;
for(i=0;i<size+1;i++)
{
cout<<" "<<a[i]<<" ";
}
}
cout<<"\n";
return 0;
#include<iostream>
using namespace std;
int main()
{
int i,a[50],no,pos,size;
cout<<"Enter array size( Max:50 ) : ";
cin>>size;
cout<<"\nEnter array elements : \n";
for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}
if(pos>size)
{
cout<<"\nThis is out of range.\n";
}
else
{
cout<<"\nEnter number to be inserted : ";
cin>>no;
--pos;
for(i=size;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=no;
for(i=0;i<size+1;i++)
{
cout<<" "<<a[i]<<" ";
}
}
cout<<"\n";
return 0;
17. Write a C++ program to Find Sum of n Natural Numbers using Recursion.
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "\nThe Sum of natural numbers upto "<<n<<" = " << add(n)<<"\n";
return 0;
}
int add(int n)
{
if(n != 0)
return n + add(n - 1);
return 0;
}
18. Write a C++ Program to find the area of rectangle using Structures.
#include <iostream>
using namespace std;
struct Rectangle{
int width, height;
};
int main(){
struct Rectangle rec;
rec.width = 6;
rec.height = 4;
cout << "Area of Rectangle is: " << (rec.width * rec.height) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i, j, n;
cout << "Enter number of rows: ";
cin >> n;
for(i = 1; i <= n; i++) // for rows
{
for(j = 1; j <= i; j++) // for columns
{
cout << "* ";
}
//Ending line after each row
cout << "\n";
}
return 0;
}
20. Write a C++ program to print this pattern
#include<iostream>
using namespace std;
int main()
{
int n, i , j;
cout << "Enter number of rows: ";
cin >> n;
// upper part
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
cout << "*";
}
// ending line after each row
cout<<"\n";
}
//lower part
for(i = n; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
cout << "*" ;
}
// ending line after each row
cout<<"\n";
}
return 0;
}