1. PROGRAM TO SWAP TWO NUMBERS.
#include <iostream>
using namespace std;
int main()
int num1, num2, temp;
cout<<"Enter 1st Number: ";
cin>>num1;
cout<<"Enter 2nd Number: ";
cin>>num2;
//displaying numbers before swapping
cout<<"Before Swapping: First Number: "<<num1<<" Second Number: "<<num2;
//swapping
temp=num1;
num1=num2;
num2=temp;
//displaying numbers after swapping
cout<<"\nAfter Swapping: First Number: "<<num1<<" Second Number: "<<num2;
return 0;
}
OUTPUT
Enter 1 st Number : 101
Enter 2 nd Number : 99
Before Swapping : first number : 101 second number : 99
After Swapping : first number : 99 second number : 101
2. PROGRAM TO FIND THE SUM OF N NATURAL NUMBERS USING
RECURSION.
#include<iostream>
using namespace std;
/* This is function declaration, When you define function
* after the main then you need to declare it like this.
* If you define the function before main then no need to
* declare function.
*/
int sum(int n);
int main(){
int n;
cout<<"Enter the value of n(should be positive integer): ";
cin>>n;
/* Here we are checking whether the entered value of n is
* natural number or not. If user enters the zero or negative
* value then display error message else prints the sum of n
* natural numbers.
*/
if(n<=0){
cout<<"The entered value of n is invalid";
else{
cout<<"Sum of n natural numbers is: "<<sum(n);
return 0;
int sum(int n)
if(n!= 0)
return n + sum(n-1);
return 0;
OUTPUT
Enter the value of n (should be positive integer) : 5
Sum of n natural numbers is : 15
3. PROGRAM TO FIND LARGEST ELEMENT IN AN ARRAY.
#include <iostream>
using namespace std;
int main(){
//n is the number of elements in the array
int n, largest;
int num[50];
cout<<"Enter number of elements you want to enter: ";
cin>>n;
/* Loop runs from o to n, in such a way that first
* element entered by user is stored in num[0], second
* in num[1] and so on.
*/
for(int i = 0; i < n; i++) {
cout<<"Enter Element "<<(i+1)<< ": ";
cin>>num[i];
// Storing first array element in "largest" variable
largest = num[0];
for(int i = 1;i < n; i++) {
/* We are comparing largest variable with every element
* of array. If there is an element which is greater than
* largest variable value then we are copying that variable
* to largest, this way we have the largest element copied
* to the variable named "largest" at the end of the loop
*/
if(largest < num[i])
largest = num[i];
cout<<"Largest element in array is: "<<largest;
return 0;
OUTPUT
Enter number of elements you want to enter: 5
Enter Element 1: 19
Enter Element 2: 21
Enter Element 3: 3
Enter Element 4: 89
Enter Element 5: 13
Largest element in array is: 89
4. PROGRAM TO FIND THE MISSING NUMBER.
#include <iostream>
using namespace std;
int findMissingNo (int arr[], int len){
int temp;
temp = ((len+1)*(len+2))/2;
for (int i = 0; i<len; i++)
temp -= arr[i];
return temp;
int main() {
int n;
cout<<"Enter the size of array: ";
cin>>n; int arr[n-1];
cout<<"Enter array elements: ";
for(int i=0; i<n; i++){
cin>>arr[i];
int missingNo = findMissingNo(arr,5);
cout<<"Missing Number is: "<<missingNo;
return 0;
}
OUTPUT
Enter the size of array: 5
Enter array elements: 1
Missing Number is: 4
5. PROGRAM TO CONVERT UPPERCASE TO LOWERCASE.
#include <iostream>
using namespace std;
int main()
char ch;
cout<<"Enter a character in uppercase: ";
cin>>ch;
//converting the uppercase char to lowercase by adding 32
//to the ASCII value of the input character
ch=ch+32;
cout<<"Entered character in lowercase: "<<ch;
return 0;
OUTPUT
Enter a character in uppercase : D
Entered character in lowercase : d
6. PROGRAM TO FIND NUMBER OF DIGITS AND WHITE SPACES IN A STRING.
#include <iostream>
using namespace std;
int main(){
string str;
int digitCounter = 0, spaceCounter = 0;
cout << "Enter any string: ";
getline(cin, str);
//'\0 represent end of string
for(int i = 0; str[i]!='\0'; i++) {
if(str[i]>='0' && str[i]<='9') {
digitCounter++;
}
else if(str[i]==' ') {
spaceCounter++;
cout << "Digits in String: " << digitCounter << endl;
cout << "White Spaces in String: " << spaceCounter << endl;
return 0;
OUTPUT
Enter any string: My Roll No is: 1099234
Digits in String: 7
White Spaces in String: 4
7. PROGRAM TO FIND THE NUMBER OF VOWELS AND CONSONANT IN A
STRING.
#include <iostream>
using namespace std;
int main(){
char str[100];
int vowelCounter = 0, consonantCounter = 0;
cout << "Enter any string: ";
cin.getline(str, 150);
//'\0 represent end of string
for(int i = 0; str[i]!='\0'; i++) {
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
vowelCounter++;
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
consonantCounter++;
cout << "Vowels in String: " << vowelCounter << endl;
cout << "Consonants in String: " << consonantCounter << endl;
return 0;
}
OUTPUT
Enter any string: Hello Guys, Welcome to Beginnersbook.com
Vowels in String: 13
Consonants in String: 21
8. PROGRAM TO CHECK ARMSTRONG NUMBER USING USER DEFINED
FUNCTION.
#include <iostream>
using namespace std;
bool checkArmstrongNumber(int num);
int main(){
int num;
bool flag;
cout<<"Enter a positive integer: ";
cin>>num;
//Calling function
flag = checkArmstrongNumber(num);
if(flag == true)
cout<<num<<" is an Armstrong number.";
else
cout<<num<<" is not an Armstrong number.";
return 0;
/* User defined function that checks whether the passed
* integer number is Armstrong or not
*/
bool checkArmstrongNumber(int num) {
int temp, sum=0, digit;
bool isArm;
temp = num;
while(temp != 0) {
digit = temp % 10;
sum = sum +(digit * digit * digit);
temp = temp/10;
if (sum==num)
isArm = true;
else
isArm = false;
return isArm;
}
OUTPUT
Enter a positive integer: 371
371 is an Armstrong number.
9. PROGRAM TO CHECK PRIME NUMBER USING A FUNCTION.
#include <iostream>
using namespace std;
bool isPrime(int num);
int main(){
int num;
bool flag;
cout<<"Enter any number(should be positive integer): ";
cin>>num;
flag = isPrime(num);
if (flag==true)
cout<<num<<" is a prime number";
else
cout<<num<<" is not a prime number";
return 0;
bool isPrime(int num){
bool flag=true;
for(int i = 2; i <= num / 2; i++) {
if(num % i == 0) {
flag = false;
break;
return flag;
OUTPUT
Enter any number (should be positive integer): 9
9 is not a prime number
10.PROGRAM TO CHECK LEAP YEAR USING FUNCTION.
#include <iostream>
using namespace std;
bool leapYear(int y);
int main(){
int y;
cout<<"Enter year: ";
cin>>y;
//Calling function
bool flag = leapYear(y);
if(flag == true)
cout<<y<<" is a leap Year";
else
cout<<y<<" is not a leap Year";
return 0;
bool leapYear(int y){
bool isLeapYear = false;
if (y % 4 == 0) {
if (y % 100 == 0) {
if (y % 400 == 0) {
isLeapYear = true;
else isLeapYear = true;
return isLeapYear;
OUTPUT
Enter year: 2016
2016 is a leap Year
11.PROGRAM TO ADD TWO MATRICES.
#include<iostream>
using namespace std;
int main()
int row, col, m1[10][10], m2[10][10], sum[10][10];
cout<<"Enter the number of rows(should be >1 and <10): ";
cin>>row;
cout<<"Enter the number of column(should be >1 and <10): ";
cin>>col;
cout << "Enter the elements of first 1st matrix: ";
for (int i = 0;i<row;i++ ) {
for (int j = 0;j < col;j++ ) {
cin>>m1[i][j];
cout << "Enter the elements of first 1st matrix: ";
for (int i = 0;i<row;i++ ) {
for (int j = 0;j<col;j++ ) {
cin>>m2[i][j];
}
cout<<"Output: ";
for (int i = 0;i<row;i++ ) {
for (int j = 0;j<col;j++ ) {
sum[i][j]=m1[i][j]+m2[i][j];
cout<<sum[i][j]<<" ";
return 0;
OUTPUT
Enter the number of rows(should be >1 and <10): 2
Enter the number of column(should be >1 and <10): 3
Enter the elements of first 1st matrix: 1 2 3 4 5 6
Enter the elements of first 1st matrix: 5 5 5 5 5 5
Output: 6 7 8 9 10 11
12.PROGRAM TO FIND TRANSPOSE OF MATRIX.
#include<iostream>
using namespace std;
int main(){
int matrix[10][10], transMatrix[10][10], row, col;
//Getting the rows from user and storing in row
cout<<"Enter the number of rows: ";
cin>>row;
//Getting the columns from user and storing in col
cout<<"Enter the number of columns: ";
cin>>col;
/* Asking the user to input the elements of matrix
* and storing them in the matrix array
*/
cout<<"Enter elements of matrix: "<<endl;
for(int i =0;i<row;i++) {
for(int j=0;j<col;j++) {
cin>>matrix[i][j];
// Finding the transpose matrix.
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
transMatrix[j][i] = matrix[i][j];
//Displaying the transpose matrix
cout<<"Transpose of Matrix: "<<endl;
for(int i=0;i<col;i++) {
for(int j=0;j<row;j++) {
cout<<transMatrix[i][j]<<" ";
/* This is just to format the output
* so you can see the matrix format
* in the output transpose matrix.
*/
if(j==row-1)
cout<<endl;
return 0;
OUTPUT
Enter the number of rows: 4
Enter the number of columns: 3
Enter elements of matrix:
1 2 3
4 5 6
7 8 9
10 11 12
Transpose of Matrix:
1 4 7 10
2 5 8 11
3 6 9 12
13.PROGRAM FOR BINARY SEARCH.
#include <iostream>
using namespace std;
int binarySearch(int[], int, int, int);
int main()
int num[10] = {10, 22, 37, 55, 92, 118};
int search_num, loc=-1;
cout<<"Enter the number that you want to search: ";
cin>>search_num;
loc = binarySearch(num, 0, 6, search_num);
if(loc != -1)
cout<<search_num<<" found in the array at the location: "<<loc;
else
{
cout<<"Element not found";
return 0;
int binarySearch(int a[], int first, int last, int search_num)
int middle;
if(last >= first)
middle = (first + last)/2;
//Checking if the element is present at middle loc
if(a[middle] == search_num)
return middle+1;
//Checking if the search element is present in greater half
else if(a[middle] < search_num)
return binarySearch(a,middle+1,last,search_num);
//Checking if the search element is present in lower half
else
return binarySearch(a,first,middle-1,search_num); }
}
return -1;
OUTPUT
Enter the number that you want to search: 55
55 found in the array at the location: 4
14.PROGRAM TO PRINT A DIAMOND PATTERN.
#include<iostream>
using namespace std;
int main()
int i, j, rowNum, space, num=1;
cout<<"Enter the Number of Rows: ";
cin>>rowNum;
space = rowNum-1;
for(i=1; i<=rowNum; i++)
for(j=1; j<=space; j++)
cout<<" ";
space--;
for(j=1; j<=(2*i-1); j++)
cout<<num;
num++;
cout<<endl;
num = 1;
space = 1;
for(i=1; i<=(rowNum-1); i++)
for(j=1; j<=space; j++)
cout<<" ";
space++;
for(j=1; j<=(2*(rowNum-i)-1); j++)
cout<<num;
num++;
cout<<endl;
num = 1;
}
cout<<endl;
return 0;
OUTPUT
Enter The Number Of Rows : 5
123
12345
1234567
123456789
1234567
12345
12345
123
15. PROGRAM FOR INSERTION SORT.
#include<iostream>
using namespace std;
int main()
{
int arr[50], tot, i, j, k, elem, index;
cout<<"Enter the Size for Array: ";
cin>>tot;
cout<<"Enter "<<tot<<" Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
for(i=1; i<tot; i++)
elem = arr[i];
if(elem<arr[i-1])
for(j=0; j<=i; j++)
if(elem<arr[j])
index = j;
for(k=i; k>j; k--)
arr[k] = arr[k-1];
break;
else
continue;
arr[index] = elem;
cout<<"\nThe New Array (Sorted Array):\n";
for(i=0; i<tot; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
OUTPUT
Enter the Size for Array: 10
Enter 10 Array Elements: 10
The New Array (Sorted Array) :
1 2 3 4 5 6 7 8 9 10