Solved Programs list :
1. Write a program that give the sum of the digits of a three digit number.
#include <iostream>
using namespace std;
int main() {
int num, sum = 0;
cout << "Enter a three-digit number: ";
cin >> num;
if (num >= 100 && num <= 999) {
sum += num % 10;
num /= 10;
sum += num % 10;
num /= 10;
sum += num;
cout << "The sum of the digits is " << sum <<endl;
} else {
cout << "Invalid input. Please enter a three-digit number." << endl;
}
return 0;
}
2. Write a program for swapping where a=5 and b=10.
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
3. Write a program that takes a number form user and check it is even or odd.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
4. Write a program that take character from user and check it is vowel or consonant.
#include <iostream>
using namespace std;
int main() {
char c;
bool isLowercaseVowel, isUppercaseVowel;
cout << "Enter an alphabet: ";
cin >> c;
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (!isalpha(c))
printf("Error! Non-alphabetic character.");
else if (isLowercaseVowel || isUppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";
return 0;
}
5. Write a program to check leap year.
#include <iostream>
using namespace std;
int main()
{
int a,b,c,n;
cout<<"enter the year to be checked =";
cin>>a;
b=a%4;
if(b==0)
{
cout<<"the year is leap year";
}
else
{
cout<<"the year is not leap year";
}
return 0;
}
6. Write a program that takes input from user and this program will prompt you that number you enters is a
number or character.
#include <iostream>
#include <ctype.h>
using namespace std;
int main() {
char input;
cout << "Enter a value: ";
cin >> input;
if (isdigit(input)) {
std::cout << "You entered a number." << std::endl;
} else if (isalpha(input)) {
cout << "You entered a character." <<endl;
} else {
cout << "You entered an invalid input." <<endl;
}
return 0;
}
7. Write a program that takes three number and find largest number using nested if statement.
#include <iostream>
using namespace std;
int main() {
double n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
if(n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;
else if(n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;
return 0;
}
8. Write a program that will take your age as input and your program will tell whether you are eligible for
voting or not.
#include <iostream>
using namespace std;
int main()
{ int a,b,c;
cout<<"Enter your age =";
cin>>a;
if(a>=18)
{
cout<<"You are eligible to vote";
}
else
{
cout<<"You are not eligible to vote.";
}
return 0;
}
9. Write a program that will check character input is uppercase or lowercase.
#include <iostream>
using namespace std;
int main() {
char c;
cout << "Enter a character: ";
cin >> c;
if (c >= 'A' && c <= 'Z') {
cout << "Uppercase";
} else if (c >= 'a' && c <= 'z') {
cout << "Lowercase";
} else {
cout << "Not an alphabet";
}
return 0;
}
10. Write a program that will take 3 coefficients of quadratic equation and will tell roots are imaginary or not.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c;
cout << "Enter the coefficients of the quadratic equation: " << endl;
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
cout << "c: ";
cin >> c;
float discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
cout << "The roots are imaginary" << endl;
} else {
cout << "The roots are real" << endl;
}
return 0;
}
11. Write a program to print out on the screen. Following pattern
*
**
***
****
#include <iostream>
using namespace std;
int main() {
int a=4;
for(int i = 1; i <= a; ++i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << "\n";
}
return 0;
}
12. Write a program to print following:
****
***
**
*
#include <iostream>
using namespace std;
int main() {
int a=4;
for(int i = a; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << "\n";
}
return 0;
}
13. Write a C++ program to print following on screen.
*
* * *
* * * * *
* * *
*
#include <iostream>
using namespace std;
int main() {
cout << "\t\t*\t\t" << endl;
cout << "\t*\t*\t*\t\n";
cout << "*\t*\t*\t*\t*\n";
cout << "\t*\t*\t*\t\n";
cout << "\t\t*\t\t\n";
return 0;
}
14. Write a program that will printout the elements of array and the sum of these elements . the array will contain 5 elements.
#include <iostream>
using namespace std;
int main() {
int arr[5];
int sum = 0;
for(int i=0; i<5; i++) {
cout << "Enter element " << i+1 << ": ";
cin >> arr[i];
cout << "Elements of array: ";
for(int i=0; i<5; i++) {
cout << arr[i] << " ";
sum += arr[i];
cout << endl << "Sum of array: " << sum << endl;
return 0;}
15. Write a program that will take 50 values from user in integer array and your program will find largest
number of all.
#include <iostream>
using namespace std;
int main()
{
int values[50];
int largest = 0;
cout << "Enter 50 integer values:" << endl;
for (int i = 0; i < 50; i++) {
cin >> values[i];
if (values[i] > largest) {
largest = values[i];
}
}
cout << "The largest value is: " << largest << endl;
return 0;
}
16. Write a program that takes 10 values form user in a integer array and when user enters a value to search
in an array your program will show location of that number in array if it found in array.
#include <iostream>
using namespace std;
int main()
{
int values[10];
cout << "Enter 10 integer values:" <<endl;
for (int i = 0; i < 10; i++) {
cin >> values[i];
}
cout << "The integers are: ";
for (int i = 0; i < 10; ++i) {
cout << values[i] << " ";
}
int searchValue;
cout << "Enter a value to search for:" << endl;
cin >> searchValue;
bool found = false;
int array;
for (int i = 0; i < 10; i++) {
if (values[i] == searchValue) {
found = true;
array = i;
}
}
if (found) {
cout << "The value " << searchValue << " was found at array " << array << "." << endl;
} else {
cout << "The value " << searchValue << " was not found in the array." << endl;
}
return 0;}
17. Write a program that will take 10 number from user in an array . your program will store only even
numbers in array.
#include <iostream>
using namespace std;
int main() {
int nums[10], even[10], count = 0;
for(int i=0; i<10; i++) {
cout << "Enter number " << i+1 << ": ";
cin >> nums[i];
if(nums[i] % 2 == 0) {
even[count] = nums[i];
count++;
}
}
cout << "Even numbers entered: ";
for(int i=0; i<count; i++) {
cout << even[i] << " ";
}
cout << endl;
return 0;}
18. Write a program that take 4 values from user in array and program will show table of the entered
number.
#include <iostream>
using namespace std;
int main() {
int num[4];
for(int i = 0; i < 4; i++) {
cout << "Enter a number: ";
cin >> num[i];
}
for(int i = 0; i < 4; i++) {
cout << "Multiplication table for " << num[i] << ":" << endl;
for(int j = 1; j <= 10; j++) {
cout << num[i] << " x " << j << " = " << num[i] * j << endl;
}
cout << endl;
}
return 0;
}
19. Write a program that will take values form user and calculate the average of these values but if the user
enters 0 than program should average.
#include <iostream>
using namespace std;
int main() {
double sum = 0.0;
int count = 0;
double num;
cout << "Enter a list of numbers, separated by spaces: " << endl;
while (true) {
cin >> num;
if (num == 0) {
if (count == 0) {
cout << "No numbers entered." << endl;
} else {
double average = sum / count;
cout << "The average is: " << average << endl;
}
break;
}
sum += num;
count++; }
return 0;
}
20. Write a program that will display the entered number in reverse order the number can be of any
length.
#include <iostream>
#include <string>
using namespace std;
int main() {
string number;
cout << "Enter a number: ";
cin >> number;
cout << "Number in reverse order: ";
for (int i = [Link]() - 1; i >= 0; i--) {
cout << number[i];
}
cout << endl;
return 0;}
21. Write a program that will calculate standard deviation of 10 numbers.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n = 10;
double arr[n], sum = 0, mean, stdDev = 0;
cout << "Enter 10 numbers: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
mean = sum / n;
for (int i = 0; i < n; i++) {
stdDev += pow(arr[i] - mean, 2);
}
stdDev = sqrt(stdDev / n);
cout << "Mean = " << mean << endl;
cout << "Standard Deviation = " << stdDev << endl;
return 0;
}
22. Write a program that takes 10 values in 2 arrays and sum of both arrays and show the result in 3rd array.
#include <iostream>
using namespace std;
int main() {
int arr1[10], arr2[10], arr3[10];
cout << "Enter 10 values for first array: ";
for (int i = 0; i < 10; i++) {
cin >> arr1[i];
}
cout << "Enter 10 values for second array: ";
for (int i = 0; i < 10; i++) {
cin >> arr2[i];
}
for (int i = 0; i < 10; i++) {
arr3[i] = arr1[i] + arr2[i];
}
cout << "The sum of the arrays is: ";
for (int i = 0; i < 10; i++) {
cout << arr3[i] << " ";
}
return 0;
}
23. Write a program that takes value from user and program will find Factors of that number using do while
loop.
#include <iostream>
using namespace std;
int main() {
int number, factor = 1;
cout << "Enter a positive integer: ";
cin >> number;
cout << "Factors of " << number << " are: " << endl;
do {
if (number % factor == 0) {
cout << factor << " ";
}
factor++;
} while (factor <= number);
return 0;
}
24. Write a program that will sort the array in descending order by using bubble sorting. The number should
get from user.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " numbers:" << endl;
for(int i=0; i<n; i++) {
cin >> arr[i];
}
for(int i=0; i<n-1; i++) {
for(int j=0; j<n-i-1; j++) {
if(arr[j] < arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
cout << "The array in descending order is:" << endl;
for(int i=0; i<n; i++) {
cout << arr[i] << " ";
}
return 0;
}
25. Write a program that takes values in 2D array and your program will find maximum number.
#include <iostream>
using namespace std;
int main() {
const int rows = 3;
const int cols = 3;
int arr[rows][cols];
int max_num = arr[0][0];
cout << "Enter the values in the 2D array: " << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] > max_num) {
max_num = arr[i][j];
}
}
}
cout << "The maximum number in the array is: " << max_num << endl;
return 0;
}
26. Write a program to find transpose of a matrix. Matrix should be entered by the user.
#include <iostream>
using namespace std;
const int MAX_ROWS = 100;
const int MAX_COLS = 100;
int main() {
int matrix[MAX_ROWS][MAX_COLS];
int transposed[MAX_COLS][MAX_ROWS];
int rows, cols;
cout << "Enter the number of rows in the matrix: ";
cin >> rows;
cout << "Enter the number of columns in the matrix: ";
cin >> cols;
cout << "Enter the elements of the matrix:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
transposed[i][j] = matrix[j][i];
}
}
cout << "Transpose of the matrix:" << endl;
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
cout << transposed[i][j] << " ";
} cout << endl; } return 0;}
27. Write a program to input data into two different arrays and your program first check whether they
can be multiplied. If yes your program will multiple them and display the result, otherwise your
program will show that multiplication not possible
#include <iostream>
using namespace std;
int main() {
int row1, col1, row2, col2;
cout << "Enter the number of rows of the first array: ";
cin >> row1;
cout << "Enter the number of columns of the first array: ";
cin >> col1;
int array1[row1][col1];
cout << "Enter the elements of the first array:" << endl;
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
cin >> array1[i][j];
}
}
cout << "Enter the number of rows of the second array: ";
cin >> row2;
cout << "Enter the number of columns of the second array: ";
cin >> col2;
int array2[row2][col2];
cout << "Enter the elements of the second array:" << endl;
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
cin >> array2[i][j];
}
}
if (col1 != row2) {
cout << "Multiplication not possible";
}
else {
int result[row1][col2];
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
result[i][j] = 0;
for (int k = 0; k < col1; k++) {
result[i][j] += array1[i][k] * array2[k][j];
}
}
}
cout << "Resultant array:" << endl;
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
}
return 0;
}
28. Write a program for addition of 2 matrices. Matrices should be entered by the user.
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> columns;
int matrix1[rows][columns], matrix2[rows][columns], sum[rows][columns];
cout << "\nEnter the elements of matrix1: \n";
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cin >> matrix1[i][j];
}
}
cout << "\nEnter the elements of matrix2: \n";
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cin >> matrix2[i][j];
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
cout << "\nSum of the matrices is:\n";
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cout << sum[i][j] << " ";
}
cout << endl;
}
return 0;
}
29. Write a Program to find determinant of 2x2 Matrix entered by the user.
#include <iostream>
using namespace std;
int main() {
int matrix[2][2], determinant;
cout << "Enter the elements of the 2x2 matrix:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cin >> matrix[i][j];
}
}
determinant = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
cout << "The determinant of the matrix is: " << determinant << endl;
return 0;
}
30. Write a program which perform the task of “Strcpy” without using built in function.
#include <iostream>
using namespace std;
void Strcpy(char* dest, const char* src) {
while (*src != '\0') {
*dest = *src;
dest++;
src++;
}
*dest = '\0';
}
int main() {
char src[] = "Hello, world!";
char dest[20];
Strcpy(dest, src);
cout << "src: " << src << endl;
cout << "dest: " << dest << endl;
return 0;
}
31. Write a program which perform the task of “Strlen” without using built in function.
#include <iostream>
using namespace std;
int Strlen(const char* str) {
int length = 0;
while (*str != '\0') {
length++;
str++;
}
return length;
}
int main() {
char str[] = "Hello, world!";
int length = Strlen(str);
cout << "str: " << str << endl;
cout << "length: " << length << endl;
return 0;
}
32. Write a program which perform the task of “Strcmp” without using built in function.
#include <iostream>
using namespace std;
int strcmp(string str1, string str2) {
int len1 = [Link]();
int len2 = [Link]();
int min_len = min(len1, len2);
for (int i = 0; i < min_len; i++) {
if (str1[i] < str2[i]) {
return -1;
} else if (str1[i] > str2[i]) {
return 1;
}
}
33. Write a program which perform the task of “Strcat” without using built in function.
#include <iostream>
using namespace std;
void strcat(char* dest, const char* src) {
int dest_len = 0;
while (dest[dest_len] != '\0') {
dest_len++;
}
int i = 0;
while (src[i] != '\0') {
dest[dest_len + i] = src[i];
i++;
}
dest[dest_len + i] = '\0';
}
int main() {
const int MAX_LENGTH = 100;
char dest[MAX_LENGTH], src[MAX_LENGTH];
cout << "Enter destination string: ";
cin >> dest;
cout << "Enter source string: ";
cin >> src;
strcat(dest, src);
cout << "Concatenated string: " << dest << endl;
return 0;
}
34. Write a program to check frequency of letter in a string. The string should be entered by the user and the
character whose frequency is to check is also entered by the user.
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string str;
char ch;
int freq = 0;
cout << "Enter a string: ";
getline(std::cin, str);
cout << "Enter a character to check frequency: ";
cin >> ch;
for (char c : str) {
if (c == ch) {
freq++;
}
}
cout << "The frequency of " << ch << " in the string \"" << str << "\" is " << freq << std::endl;
return 0;
}
35. Write a program to reverse a String the string should be set using getline() function.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
getline(std::cin, str);
int start = 0;
int end = [Link]() - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
cout << "The reversed string is: " << str << endl;
return 0;
}
36. Write a program using function to print table of a number.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= 10; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}
return 0;
}
37. Write a program in which we pass 2 values and sum is display in main body.
#include <iostream>
using namespace std;
int main() {
int a, b, sum;
cout << "Enter two values: ";
cin >> a >> b;
sum = a + b;
cout << "The sum is: " << sum << endl;
return 0;
}
38. Write a function that finds the area of the rectangle on providing length and width. Get Length &
width from user in main () Call the function area () Calculate the length and return the area Display
the result in main ().
#include <iostream>
using namespace std;
int area(int length, int width) {
return length * width;
}
int main() {
int length, width, a;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
a = area(length, width);
cout << "The area of the rectangle is: " << a << endl;
return 0;
}
39. write a program for checking the entered number is prime or not.
#include <iostream>
using namespace std;
int main() {
int i, n;
bool is_prime = true;
cout << "Enter a positive integer: ";
cin >> n;
if (n == 0 || n == 1) {
is_prime = false;
}
for (i = 2; i <= n/2; ++i) {
if (n % i == 0) {
is_prime = false;
break;
}
}
if (is_prime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number";
return 0;
}
40. Write a program in C++ that takes a starting number and an ending number and you program will
display the primes numbers between those entered numbers using do while loop.
#include <iostream>
using namespace std;
bool is_prime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
int start, end, i;
cout << "Enter the starting number: ";
cin >> start;
cout << "Enter the ending number: ";
cin >> end;
i = start;
do {
if (is_prime(i)) {
cout << i << " ";
}
i++;
} while (i <= end);
cout << endl;
return 0;
}
41. Write a program for multiplication of matrices.
#include <iostream>
using namespace std;
const int MAX_ROWS = 10;
const int MAX_COLS = 10;
void multiply_matrices(int a[][MAX_COLS], int b[][MAX_COLS], int c[][MAX_COLS], int m, int n, int p) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
c[i][j] = 0;
for (int k = 0; k < n; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
void display_matrix(int matrix[][MAX_COLS], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int a[MAX_ROWS][MAX_COLS], b[MAX_ROWS][MAX_COLS], c[MAX_ROWS][MAX_COLS];
int m, n, p;
cout << "Enter the number of rows and columns of matrix A: ";
cin >> m >> n;
cout << "Enter the elements of matrix A:\n";
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
cout << "Enter the number of rows and columns of matrix B: ";
cin >> n >> p;
cout << "Enter the elements of matrix B:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
cin >> b[i][j];
}
}
multiply_matrices(a, b, c, m, n, p);
cout << "The product of the matrices is:\n";
display_matrix(c, m, p);
return 0;
}
42. Write a program that count no of words, spaces in a sentence which is entered by user in string variable or
character array.
#include <iostream>
#include <string>
using namespace std;
int main() {
string sentence;
int num_words = 0, num_spaces = 0;
cout << "Enter a sentence: ";
getline(cin, sentence);
for (int i = 0; i < [Link](); i++) {
if (sentence[i] == ' ') {
num_spaces++;
}
}
num_words = num_spaces + 1;
cout << "Number of words: " << num_words << endl;
cout << "Number of spaces: " << num_spaces << endl;
return 0;
}
43. Write a program to check the number entered by the user is Armstrong number or not.
#include <iostream>
using namespace std;
int main() {
int num, originalNum, remainder, result = 0;
cout << "Enter a three-digit integer: ";
cin >> num;
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";
return 0;
}
44. Wirte a program to display following menu:
1= Print table
2= Print Factorial
3= Quit
Program will ask to enter your choice if user entered number other then menu invalid should be displayed
if the number matched the corresponding function is called and perform the task. This program should be
written using functions.
#include <iostream>
using namespace std;
void printTable() {
int num;
cout << "Enter a number to print its multiplication table: ";
cin >> num;
for (int i = 1; i <= 10; i++) {
cout << num << " x " << i << " = " << num*i << endl;
}
}
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n-1);
}
}
void printFactorial() {
int num;
cout << "Enter a number to print its factorial: ";
cin >> num;
int fact = factorial(num);
cout << num << "! = " << fact << endl;
}
int main() {
int choice;
do {
cout << "MENU:" << endl;
cout << "1. Print table" << endl;
cout << "2. Print factorial" << endl;
cout << "3. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
printTable();
break;
case 2:
printFactorial();
break;
case 3:
cout << "Exiting program..." << endl;
break;
default:
cout << "Invalid choice. Please enter 1, 2, or 3." << endl;
}
} while (choice != 3);
return 0;}
45. Write a program in C++ that take a single character from the user, and tells it's a Small Letter or it's
a CAPITAL letter using nested if statement only.
#include<iostream>
#include <stdio.h>
using namespace std;
int main()
{
char ch;
cout<<"Enter any type of character:";
cin>>ch;
if (ch>='a' && ch<='z')
{cout<<"Entered character is lower case";}
else if(ch>='A' && ch<='Z')
{cout<<"Entered character is upper case"; }
else
{
cout<<"Entered value is not a character";
}
return 0;
46. Write a Program that display numbers from 1-20 using for loop. 1,2,3,4,5,6,7.8.9………………..20
#include <iostream>
using namespace std;
int main() {
for(int i=1; i<=20; i++) {
cout << i << " ";
}
return 0;
}
47. Write a program in C++ that calculate the factorial of user defined number, using for loop.
#include <iostream>
using namespace std;
int main() {
int n;
long factorial = 1.0;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
return 0;
}
48. Write a program in C++ to find the factorial of number using while loop. The number must be user
defined.
#include <iostream>
using namespace std;
int main() {
int num, fact=1;
cout << "Enter a number: ";
cin >> num;
int i = 1;
while(i <= num) {
fact *= i;
i++;
}
cout << "The factorial of " << num << " is " << fact;
return 0;
}
49. Write a program that will display of the ASCII code of the character entered by the user if user enter “Q”
the program should terminate else it will display the ACSII as user enter the any character.
#include <iostream>
using namespace std;
int main() {
char c;
while (true) {
cout << "Enter a character ('Q' to quit): ";
cin >> c;
if (c == 'Q') {
break;
}
cout << "The ASCII code of '" << c << "' is " << (int) c << endl;
}
return 0;
}
50. Write a program in C++ that take a single character from the user, and tells it's a Small Letter or it's
a CAPITAL letter using nested if statement only.
#include<iostream>
#include <stdio.h>
using namespace std;
int main()
{
char ch;
cout<<"Enter any type of character:";
cin>>ch;
if (ch>='a' && ch<='z')
{cout<<"Entered character is lower case";}
else if(ch>='A' && ch<='Z')
{cout<<"Entered character is upper case"; }
else
{
cout<<"Entered value is not a character";
}
return 0;
}
51. Write a program that will find the size of array you can initialize the array in the program with
different numbers.
#include <iostream>
using namespace std;
int main() {
int arr[] = { 1, 2, 3, 4, 5 };
int size = sizeof(arr) / sizeof(arr[0]);
cout << "The size of the array is: " << size << endl;
return 0;
}
52. Write a program that will find the GCD of the two numbers that are entered by the user.
#include <iostream>
using namespace std;
int main() {
int n1, n2, hcf;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
if ( n2 > n1) {
int temp = n2;
n2 = n1;
n1 = temp;
}
for (int i = 1; i <= n2; ++i) {
if (n1 % i == 0 && n2 % i ==0) {
hcf = i;
}
}
cout << "GCD = " << hcf;
return 0;
}