0% found this document useful (0 votes)
13 views

Cpp Foundation Programs

The document contains a series of C++ programming exercises, each with a specific task such as adding two numbers, finding data type sizes, calculating averages, and generating Fibonacci series. Each exercise includes a complete C++ code implementation and instructions for user input. The tasks cover various programming concepts including loops, conditionals, functions, arrays, and structures.

Uploaded by

dinhminhhoa187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Cpp Foundation Programs

The document contains a series of C++ programming exercises, each with a specific task such as adding two numbers, finding data type sizes, calculating averages, and generating Fibonacci series. Each exercise includes a complete C++ code implementation and instructions for user input. The tasks cover various programming concepts including loops, conditionals, functions, arrays, and structures.

Uploaded by

dinhminhhoa187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

C++ Programs Practice

1. Write a C++ Program to C++ Program to Add Two Numbers.

#include <iostream>
using namespace std;

int main()
{
int first, second, sum;

cout << "Enter 1st integer : ";


cin >> first;
cout << "\nEnter 2nd integer : ";
cin >> second;

sum = first + second;

cout<<"\nSum of Two Numbers "<<first<<" + "<<second<<" = "<<sum<<"\n";

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;

cout<<"\nThe SUM of 3 Numbers "<<a<<" + "<<b<<" + "<<c<<" = "<<sum<<"\n";


cout<<"\nThe AVERAGE of 3 Numbers "<<a<<", "<<b<<", "<<c<<" = "<<avg<<"\n";

return 0;
}

4. Write a C++ Program to raise any number X to power N.

#include<iostream>
#include<math.h> //for pow() function
using namespace std;

int main()
{

int x,n,result;

cout<<"Enter value of X : ";


cin>>x;
cout<<"\nEnter value of N : ";
cin>>n;

result=pow(x,n);

cout<<"\nThe Power of Number "<<x<<" ^ "<<n<<" = "<<result<<"\n";

return 0;
}
5. Write a C++ Program to find Square Root of a number using sqrt() function.

#include<iostream>
#include<math.h>

using namespace std;

int main()
{
float sq,n;

cout<<"Enter any positive number : ";


cin>>n;

sq=sqrt(n);

cout<<"\nSquare root of Entered Number "<<n<<" is : "<<sq<<"\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;
}

7. Write a C++ Program to Check given number is Prime number or not.

#include<iostream>
using namespace std;

int main()
{
int i,n;

cout<<"Enter any positive number : ";


cin>>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;

cout<<"Enter any positive integer : ";


cin>>n;

no=n;

while(no>0)
{
no=no/10;
a++;
}
cout<<"\nNumber of Digits in a number :"<<n<<" is : "<<a<<"\n";

return 0;
}

9. Write a C++ Program to Generate Fibonacci Series for N numbers.

#include<iostream>
using namespace std;

int main()
{
int i,no, first=0, second=1, next;

first=0;
second=1;

cout<<"How many terms u want to Display : ";


cin>>no;

cout<<"\nThe Fibonacci series for "<<no<<" terms are : \n";


for(i=0; i<no; i++)
{
cout<<" "<<first<<" ";
next = first + second;
first = second;
second = next;
}
return 0;
}
10. Write a C++ Program to calculate Average of 5 subjects and find percentage.

#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<<"\nEnter arr"<<i<<" Element : ";
cin>>a[i];
}

cout<<"\nStored Data in Array : \n\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;

void Reverse_Array(int array[],int size);

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<<"\nEnter arr["<<i<<"] Element :";
cin>>a[i];
}

cout<<"\nStored Data in Array : \n\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;
}

void Reverse_Array(int array[],int size)


{
int temp;
size--;
for (int i=0;size>=i;size--,i++)
{
temp=array[i];
array[i]=array[size];
array[size]=temp;
}
}
13. Write a C++ Program to Find Duplicate Elements in an Array.

#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<<"\nEnter arr["<<i<<"] Element : ";
cin>>a[i];
}

cout<<"\nStored Data in Array : \n\n";

for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}

cout<<"\n\nDuplicate Values in Given Array are : \n\n";


for(i=0; i<size; i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]==a[j])
{
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<<"\nEnter arr"<<i<<" Element : ";
cin>>a[i];
}

cout<<"\nStored Data in Array : \n\n";

for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}

cout<<"\n\nEnter position to Delete number : ";


cin>>pos;

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<<"\nEnter arr["<<i<<"] Element : ";
cin>>a[i];
}

cout<<"\nStored Data in Array : \n\n";

for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}

cout<<"\n\nEnter position to insert number : ";


cin>>pos;

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;

cout<<"\nNew Array is : \n\n";

for(i=0;i<size+1;i++)
{
cout<<" "<<a[i]<<" ";
}
}
cout<<"\n";

return 0;

16. Write a C++ Program to Sort Array Elements in Ascending order.

#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<<"\nEnter arr["<<i<<"] Element : ";
cin>>a[i];
}

cout<<"\nStored Data in Array : \n\n";

for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}

cout<<"\n\nEnter position to insert number : ";


cin>>pos;

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;

cout<<"\nNew Array is : \n\n";

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 add(int n);

int main()
{
int n;

cout << "\nEnter any positive integer : ";


cin >> 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;
}

19. Write a C++ Program to Print Pattern

#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;
}

You might also like