1.
Write a Program to Find the ASCII Value of a Character:
#include <iostream>
using namespace std;
int main()
char ch;
ch = 'A';
cout << "The ASCII value of " << ch << " is " << int(ch)
<< endl;
return 0;
Output:
The ASCII value of A is 65
2.C++ Program to check whether a number is positive or negative
#include <iostream>
using namespace std;
int main()
int number;
number = -100;
if (number >= 0)
cout << number << " is a positive number." << endl;
else
cout << number << " is a negative number." << endl;
return 0;
}
Output:
-100 is a negative number.
3.Write a Program to Find the Factorial of a Number Using Loops
#include <bits/stdc++.h>
using namespace std;
// function to find factorial
int factorial(int n)
int fact = 1;
while (n > 1) {
fact *= n;
n--;
return fact;
// driver code
int main()
int num = 5;
cout << factorial(num);
return 0;
Output:- 120
4.Write a Program to Check the Prime Number:
#include <iostream>
using namespace std;
bool isPrime(int n)
{
// base condition
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
int main()
isPrime(21) ? cout << " true\n" : cout << " false\n";
isPrime(17) ? cout << " true\n" : cout << " false\n";
return 0;
Output:
false
true
5.C++ program to check if a number is Palindrome or not :
#include <iostream>
using namespace std;
// Function to check Palindrome
bool checkPalindrome(int n)
int ans = 0;
int temp = n;
while (temp != 0)
{
ans = (ans * 10) + (temp % 10);
temp = temp / 10;
return (ans == n);
int main()
int n = 12321;
if (checkPalindrome(n) == 1)
cout << "Yes\n";
else
cout << "No\n";
return 0;
Output:
Yes
6.Write a Program to Check Whether a Number is an Armstrong Number or
Not
#include <iostream>
using namespace std;
int main()
int n = 153;
int temp = n;
int ans = 0;
// function to calculate
// the sum of individual digits
while (n > 0)
int rem = n % 10;
ans = (ans) + (rem * rem * rem);
n = n / 10;
// condition to check
if (temp == ans)
cout << ("Yes, it is Armstrong Number");
else
cout << ("No, it is not an Armstrong Number");
return 0;
Output;
Yes, it is Armstrong Number
7.Write a Program to Find the Nth Term of the Fibonacci Series :
#include <iostream>
using namespace std;
int fib(int n)
int first = 0, second = 1, ans;
if (n == 0)
return first;
for (int i = 2; i <= n; i++)
ans = first + second;
first = second;
second = ans;
return ans;
int main()
int n = 13;
cout << fib(n);
return 0;
Output:
233
8.Write a Program to Find the Smallest and Largest Element in an Array:
#include <iostream>
using namespace std;
void findMinMax(int arr[], int n)
int mini = arr[0];
int maxi = arr[0];
for (int i = 0; i < n; i++)
if (arr[i] < mini)
mini = arr[i];
}
else if (arr[i] > maxi)
maxi = arr[i];
cout << "Min: " << mini << endl;
cout << "Max: " << maxi << endl;
int main()
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
findMinMax(arr, N);
return 0;
Output:
Min: 1
Max: 5
9.Write a Program to Print a Simple Pyramid Pattern
#include <iostream>
using namespace std;
int main()
int rows = 5;
for (int i = 1; i <= rows; i++)
for (int j = rows; j >= i; j--)
cout << " ";
}
for (int k = 1; k <= (2 * i - 1); k++)
cout << "*";
cout << endl;
return 0;
Output:
*
*
***
***
***
10.c++ program to print day of week:
#include <iostream>
int main() {
int dayNumber;
// Prompt the user to enter a number for the day of the week
std::cout << "Enter a number (1-7) to get the corresponding day of the week: ";
std::cin >> dayNumber;
// Switch-case to determine and print the day of the week
switch (dayNumber)
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
case 4:
std::cout << "Thursday" << std::endl;
break;
case 5:
std::cout << "Friday" << std::endl;
break;
case 6:
std::cout << "Saturday" << std::endl;
break;
case 7:
std::cout << "Sunday" << std::endl;
break;
default:
std::cout << "Invalid input! Please enter a number between 1 and 7." << std::endl;
return 0;
Output:
Enter a number (1-7) to get the corresponding day of the week: 2
Tuesday
11. Write a program to find the reverse of a number:
#include<iostream>
using namespace std;
int main()
int a = 1223;
int res = 0;
while(a!=0)
int dig = a%10;
res = res*10 + dig;
a =a/10;
cout<<res;
return 0;
Output:
3221
12. A Code to swap the values of two variables without the usage of a third
variable.
#include<bits/stdc++.h>
using namespace std;
int main ()
int a = 15;
int b = 30;
a = a + b;
b = a - b;
a = a - b;
cout << "A : " << a << "\n";
cout << "B : " << b << "\n";
return 0;
Output:
A : 30
B : 15
13.C++ program to create calculator using
#include <iostream>
using namespace std;
// Driver code
int main()
char op;
float num1, num2;
// It allows user to enter operator
// i.e. +, -, *, /
cin >> op;
// It allow user to enter the operands
cin >> num1 >> num2;
// Switch statement begins
switch (op) {
// If user enter +
case '+':
cout << num1 + num2;
break;
// If user enter -
case '-':
cout << num1 - num2;
break;
// If user enter *
case '*':
cout << num1 * num2;
break;
// If user enter /
case '/':
cout << num1 / num2;
break;
// If the operator is other than +, -, * or /,
// error message will display
default:
cout << "Error! operator is not correct";
// switch statement ends
return 0;
Output:Input
+
2
2
4
14.C++ Program to Swap Two Numbers using a Temporary Variable:
// C++ program to swap two
// numbers using 3rd variable
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
int a = 2, b = 3;
cout << "Before swapping a = " << a << " , b = " << b
<< endl;
// temporary variable
int temp;
// appying swapping algorithm
temp = a;
a = b;
b = temp;
cout<< "After swapping a = " << a << " , b = " << b
<< endl;
return 0;
Output
Before swapping a = 2 , b = 3
After swapping a = 3 , b = 2
14. c++ program for table:
#include <iostream>
using namespace std;
int main()
int n, range;
cout << "Enter an integer: ";
cin >> n;
cout << "Enter range: ";
cin >> range;
for (int i = 1; i <= range; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
return 0;
}
Output:
Enter an integer: 8
Enter range: 12
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
8 * 11 = 88
8 * 12 = 96
15. Write a C++ Program to Find the sum of all the natural numbers from 1 to
n.
#include <iostream>
using namespace std;
int main()
int n, sum = 0;
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
// or sum = n*(n+1)/2;
cout << sum;
return 0;
Input: 5
Output: 15