C - Problem Solving-Sheet (Premium Version) - Code With Redoy
C - Problem Solving-Sheet (Premium Version) - Code With Redoy
Web version
return 0;
}
2. Write a program to enter two numbers and perform all arithmetic operations.
#include<iostream>
using namespace std;
int main ()
{
int x,y,sum,sub,mul;
float div;
printf("Enter Two Numbers = ");
scanf("%d %d",&x,&y);
sum = x+y;
sub = x-y;
mul = x*y;
div = x/y;
return 0;
}
3. Write a program to enter the length, and breadth of a rectangle and find its perimeter
and area.
#include<iostream>
using namespace std;
int main()
{
float length, width, perimeter, area;
cout<<"Enter length of rectangle: ";
cin>>length;
cout<<"Enter width of rectangle: ";
cin>>width;
5. Write a program to calculate the perimeter and area of a circle with a given radius.
#include <iostream>
using namespace std;
int main()
{
int radius;
float area, perimeter;
radius = 6;
perimeter = (2*3.1416*radius);
area = (3.1416*radius*radius);
return 0;
}
6. Write a program to find the third angle of a triangle if two angles are given.
#include <iostream>
using namespace std;
int main()
{
int ang1, ang2, ang3;
cout<<"Input two angles of triangle separated by comma : ";
cin>>ang1>>ang2;
int main ()
{
float f, c;
cout<<"Enter Celsius temperature: ";
cin>>f;
f = ((9 * c) / 5) + 32;
cout<<"Fahrenheit: "<< f;
return 0;
}
int main()
{
float frh, cel;
cout << "Input the temperature in Fahrenheit : ";
cin >> frh;
cout << " The temperature in Fahrenheit : " << frh << endl;
cout << " The temperature in Celsius : " << cel << endl;
return 0;
}
9. Write a Program to enter marks of five subjects and calculate the total, average, and
percentage.
#include <iostream>
using namespace std;
int main(){
float phy, math, chem, bio, ict, total, average, per;
cout<< "Enter the marks of all subject = ";
cin >> phy >> math >> bio >> chem >> ict;
return 0;
}
int main(){
float p, t, r, si, ci;
cout << "Enter the value of p, t, r = ";
cin>>p>>t>>r;
si = (p * t * r) / 100;
return 0;
}
12. Write a Program that takes minutes as input and displays the total number of hours
and minutes.
#include<iostream>
using namespace std;
int main(){
int min, restMin, h;
cout<<"Enter total minutes = ";
cin>>min;
h = min / 60;
restMin = min % 60;
cout<<min<<" minutes = "<< h << " hours and " << restMin
<< " minutes.";
return 0;
}
13. Write a program that reads a first name, last name, and year of birth and displays the
names and the year one after another sequentially.
#include<iostream>
using namespace std;
int main(){
string fName, lName;
int year;
return 0;
}
14. Write a program to convert specified days into years, weeks and the rest of the days.
#include <iostream>
using namespace std;
int main()
{
int days, years, weeks;
cout<<"Enter days: ";
cin>>days;
/* Conversion */
years = (days / 365);
weeks = (days % 365) / 7;
days = days - ((years * 365) + (weeks * 7));
return 0;
}
15. Write a program that accepts an employee's ID, totally worked hours of a month, and
the amount he received per hour. Print the employee's ID and salary for a particular
month.
#include<iostream>
int main(){
int id, workHour, hourlyRate, monthlySalary;
cout << "Enter your Employee ID: ";
cin >> id;
return 0;
}
cout<<"Result = "<<result;
return 0;
}
17. Write a program to enter any number and calculate its square root.
#include <iostream>
#include<math.h>
using namespace std;
int main(){
int num, result;
cout<<"Enter a number: ";
cin>>num;
result = sqrt(num);
18. Write a program to calculate a bike’s total consumption from the given total distance
(integer value) traveled (in km) and spent fuel (in liters, float number – 2 decimal
points.
#include <iostream>
using namespace std;
int main(){
int x;
float y;
cout<<"Input total distance in km: ";
cin>>x;
return 0;
}
return 0;
}
return 0;
}
{
int num;
cout<<"Enter a number: ";
cin>>num;
if(num %2 == 0)
cout<<num<<" is an EVEN number."<<endl;
else
cout<<num<<" is an ODD number."<<endl;
return 0;
}
22. Write a program to enter the week number and print the day of
the week.
#include<iostream>
using namespace std;
int main(){
int week;
cout<<"Enter week number (1-7): ";
cin>>week;
if(week == 1){
cout<<"Saturday";
}
else if(week == 2){
cout<<"Sunday";
}
else if(week == 3){
cout<<"Monday";
}
else if(week == 4){
cout<<"Tuesday";
}
else if(week == 5){
cout<<"Wednesday";
}
else if(week == 6){
cout<<"Thursday";
}
else if(week == 7){
cout<<"Friday";
}
else{
cout<<"Invalid Input! Please enter week number between
1-7.";
}
}
return 0;
}
int main() {
int num;
if (num % 5 == 0) {
cout << num << " is divisible by 5" << endl;
}
if (num % 11 == 0) {
cout << num << " is divisible by 11" << endl;
}
return 0;
}
25. Write a program to input any character and check whether it is the
alphabet, digit, or special character.
#include <iostream>
using namespace std;
int main() {
char ch;
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
cout << ch << " is an alphabet" << endl;
}
else {
cout << ch << " is a special character" << endl;
}
return 0;
}
26. Write a program to check whether you are eligible to make a NID
Card or not.
#include <iostream>
using namespace std;
int main() {
int age;
char nationality;
return 0;
}
27. Write a program to check whether the year is a leap year or not.
#include <iostream>
using namespace std;
int main() {
int year;
return 0;
}
int main() {
char ch;
return 0;
}
int main() {
char c;
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
cout << "The character is an alphabet.";
}
else {
cout << "The character is not an alphabet.";
}
return 0;
}
int main() {
char c;
return 0;
}
31. Write a program to input the week number and print the weekday.
#include <iostream>
using namespace std;
int main() {
int weekNum;
if (weekNum == 1) {
cout << "Monday" << endl;
}
else if (weekNum == 2) {
cout << "Tuesday" << endl;
}
else if (weekNum == 3) {
cout << "Wednesday" << endl;
}
else if (weekNum == 4) {
cout << "Thursday" << endl;
}
else if (weekNum == 5) {
cout << "Friday" << endl;
}
else if (weekNum == 6) {
cout << "Saturday" << endl;
}
else if (weekNum == 7) {
cout << "Sunday" << endl;
}
else {
cout << "Invalid input!" << endl;
}
return 0;
}
32. Write a program to input the month number and print the number
of days in that month.
#include <iostream>
using namespace std;
int main() {
int month;
if (month == 2) {
cout << "28 or 29 days depending on
whether it is a leap year.";
}
else if (month == 4 || month == 6 || month ==
9 || month == 11) {
cout << "30 days";
}
else {
cout << "31 days";
}
return 0;
}
33. Write a program to check whether two integers are equal or not.
#include <iostream>
using namespace std;
int main() {
int a, b;
if (a == b) {
cout << "The two integers are equal.";
}
else {
cout << "The two integers are not equal.";
}
return 0;
}
int main() {
int angle1, angle2, angle3, sum;
if (sum == 180) {
cout << "The triangle is valid.";
}
else {
cout << "The triangle is not valid.";
}
return 0;
}
int main() {
int side1, side2, side3;
return 0;
}
int main() {
double a, b, c, root1, root2, discriminant;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "The roots are real and different." << endl;
cout << "Root 1 = " << root1 << endl;
cout << "Root 2 = " << root2 << endl;
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
cout << "The roots are real and equal." << endl;
cout << "Root 1 = Root 2 = " << root1 << endl;
}
else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
cout << "The roots are complex and different." << endl;
cout << "Root 1 = " << realPart << " + " << imaginaryPart
<< "i" << endl;
cout << "Root 2 = " << realPart << " - " << imaginaryPart;
}
}
int main() {
char ch;
return 0;
}
38. So you will make a program (using else if ladder) where your
brother writes any alphabet and your program will tell him that it is a
vowel or consonant.
#include <iostream>
using namespace std;
int main() {
char ch;
return 0;
}
int main() {
int math, phy, chem, total;
cout << "Enter the marks in Maths, Physics, and Chemistry (out
of 100):\n";
cin >> math >> phy >> chem;
total = math + phy + chem;
if (math >= 65 && phy >= 55 && chem >= 50 && total >= 180) {
cout << "Congratulations! You are eligible for admission
to the professional course.\n";
}
else if (math + phy >= 140) {
cout << "Congratulations! You are eligible for admission
to the professional course.\n";
}
else {
cout << "Sorry, you are not eligible for admission to the
professional course.\n";
}
return 0;
}
int main() {
float temp;
if (temp < 0) {
cout << "Freezing weather" << endl;
}
else if (temp >= 0 && temp <= 10) {
cout << "Very cold weather" << endl;
}
else if (temp > 10 && temp <= 20) {
cout << "Cold weather" << endl;
}
else if (temp > 20 && temp <= 30) {
cout << "Normal in temp" << endl;
}
else if (temp > 30 && temp <= 40) {
return 0;
}
int main() {
int a, b, c, d, result;
if (c - d != 0) {
result = (a + b) / (c - d);
cout << "Result: " << result << endl;
}
return 0;
}
int main() {
float basic_salary, gross_salary, hra, da;
return 0;
43. Write a program to input electricity unit charges and calculate the
total electricity bill according to the given condition:
For the first 50 units, Rs. 0.50/unit
For the next 100 units Rs. 0.75/unit
For the next 100 units Rs. 1.20/unit
For units above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill.
#include <iostream>
using namespace std;
int main() {
int units;
float total_bill, surcharge = 0.2;
return 0;
}
44. Write a program to print the day of the week name using a
switch case.
#include <iostream>
using namespace std;
int main() {
int day;
cout << "Enter the day number (1-7): ";
cin >> day;
switch(day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid day number" << endl;
}
return 0;
}
int main() {
int month, days;
cout << "Enter the month number (1-12): ";
cin >> month;
switch(month) {
case 1:
days = 31;
break;
case 2:
days = 28;
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
break;
default:
cout << "Invalid month number" << endl;
return 0;
}
cout << "The month has " << days << " days." << endl;
return 0;
}
int main() {
char alphabet;
cout << "Enter an alphabet: ";
cin >> alphabet;
switch(alphabet) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout << alphabet << " is a vowel." << endl;
break;
default:
return 0;
}
int main() {
double num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
return 0;
}
int main() {
int num;
switch(num % 2) {
case 0:
cout << num << " is even." << endl;
break;
case 1:
cout << num << " is odd." << endl;
break;
}
return 0;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
switch(num > 0) {
case true:
cout << num << " is positive." << endl;
break;
case false:
switch(num < 0) {
case true:
cout << num << " is negative." << endl;
break;
case false:
cout << num << " is zero." << endl;
break;
}
break;
}
return 0;
}
int main() {
double a, b, c, x1, x2, discriminant;
cout << "Enter the coefficients a, b, and c: ";
cin >> a >> b >> c;
discriminant = b * b - 4 * a * c;
switch(discriminant > 0) {
case true:
x1 = (-b + sqrt(discriminant)) / (2 * a);
x2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "The roots are real and different." << endl;
cout << "x1 = " << x1 << ", x2 = " << x2 << endl;
break;
case false:
switch(discriminant < 0) {
case true:
cout << "The roots are complex and different."
<< endl;
cout << "x1 = " << -b / (2 * a) << "+" <<
sqrt(-discriminant) / (2 * a) << "i" << endl;
cout << "x2 = " << -b / (2 * a) << "-" <<
sqrt(-discriminant) / (2 * a) << "i" << endl;
break;
case false:
x1 = -b / (2 * a);
return 0;
}
int main() {
char operation;
double num1, num2;
cout << "Enter the operation (+, -, *, /): ";
cin >> operation;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
switch(operation) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2
<< endl;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2
<< endl;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2
<< endl;
break;
case '/':
if(num2 != 0) {
cout << num1 << " / " << num2 << " = " << num1 /
num2 << endl;
}
else {
cout << "Error: division by zero." << endl;
}
break;
default:
cout << "Error: invalid operation." << endl;
break;
}
return 0;
}
52. Write a program that takes the integer number of a student and
finds out the grade using a switch case statement following the
grading system.
A = 90-100
B+ = 87-89
B = 84-86
B- = 80-83
C+ = 77-79
C = 74-76
C- = 70-73
D+ = 65-69
D = 60-64
F = Below 60
#include <iostream>
using namespace std;
int main() {
int score;
cout << "Enter the student's score: ";
cin >> score;
char grade;
switch(score/10) {
case 10:
case 9:
grade = 'A';
break;
case 8:
if(score >= 87) {
grade = 'B';
}
else if(score >= 84) {
grade = 'B';
cout << "+";
}
else if(score >= 80) {
grade = 'B';
cout << "-";
}
break;
case 7:
if(score >= 77) {
grade = 'C';
}
else if(score >= 74) {
grade = 'C';
cout << "+";
}
else if(score >= 70) {
grade = 'C';
cout << "-";
}
break;
case 6:
if(score >= 65) {
grade = 'D';
}
else if(score >= 60) {
grade = 'D';
cout << "+";
}
break;
default:
grade = 'F';
break;
}
cout << "The student's grade is " << grade << endl;
return 0;
}
53. Write a program to Print your name 20 times using a for loop,
while loop and do while loop.
#include <iostream>
using namespace std;
int main() {
int i = 1;
return 0;
}
54. Write a program to print 1-10 using a for loop, while loop and do
while loop.
#include <iostream>
using namespace std;
int main() {
int i;
return 0;
}
int main() {
int i, sum = 0;
return 0;
}
int main() {
while(true) {
cout << "This is an infinite loop." << endl;
}
return 0;
}
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Even numbers up to " << n << " are: ";
for(int i=2; i<=n; i+=2) {
cout << i << " ";
}
cout << endl;
return 0;
}
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Odd numbers up to " << n << " are: ";
for(int i=1; i<=n; i+=2) {
cout << i << " ";
}
cout << endl;
return 0;
}
59. Write a program to find the sum of all natural numbers between 1
to n.
#include <iostream>
using namespace std;
int main() {
int n, sum=0;
cout << "Enter a positive integer: ";
cin >> n;
cout << "The sum of all natural numbers between 1 to " << n <<
" is: " << sum << endl;
return 0;
}
60. Write a program to find the sum of all even numbers between 1 to
n.
#include <iostream>
using namespace std;
int main() {
int n, sum=0;
cout << "Enter a positive integer: ";
cin >> n;
cout << "The sum of all even numbers between 1 to " << n << "
is: " << sum << endl;
return 0;
}
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Natural numbers in reverse order from " << n << " to
1 are: ";
for(int i=n; i>=1; i--) {
cout << i << " ";
}
cout << endl;
return 0;
}
62. Write a program to print those numbers from 1 to 100 which are
divisible by 7.
#include<iostream>
using namespace std;
int main() {
for(int i=1;i<=100;i++) {
if(i%7 == 0) {
cout<<i<<" ";
}
}
return 0;
}
int main() {
for(char c='a';c<='z';c++) {
cout<<c<<" ";
}
return 0;
}
int main() {
int num, count=0;
cout<<"Enter a number: ";
cin>>num;
while(num!=0) {
num/=10;
count++;
}
cout<<"The number of digits in the given number is:
"<<count<<endl;
return 0;
}
int main() {
int num, last_digit;
cout<<"Enter a number: ";
cin>>num;
last_digit = num%10;
cout<<"The last digit of the given number is:
"<<last_digit<<endl;
return 0;
}
int main() {
int num, first_digit;
cout<<"Enter a number: ";
cin>>num;
while(num>=10) {
num/=10;
}
first_digit = num;
cout<<"The first digit of the given number is:
"<<first_digit<<endl;
return 0;
}
67. Write a program to find the sum of the first and last digits of a
number.
#include<iostream>
using namespace std;
int main() {
int num, first_digit, last_digit, sum;
cout<<"Enter a number: ";
cin>>num;
last_digit = num%10;
while(num>=10) {
num/=10;
}
first_digit = num;
68. Write a program to swap the first and last digits of a number.
#include<iostream>
using namespace std;
int main() {
int num, temp, last_digit, first_digit, digits=0;
cout<<"Enter a number: ";
cin>>num;
temp = num;
last_digit = temp%10;
while(temp>=10) {
temp/=10;
digits++;
}
first_digit = temp;
int multiplier = 1;
for(int i=0;i<digits;i++) {
multiplier *= 10;
}
int main() {
int num, sum = 0;
cout << "Enter a number: ";
cin >> num;
int main() {
int num, product = 1;
cout << "Enter a number: ";
cin >> num;
int main() {
int num, reversed = 0;
cout << "Enter a number: ";
cin >> num;
int main() {
int num, original, reversed = 0;
cout << "Enter a number: ";
cin >> num;
original = num;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
if (original == reversed)
cout << original << " is a palindrome.";
else
cout << original << " is not a palindrome.";
return 0;
}
int main() {
int num, freq[10] = {0};
cout << "Enter a number: ";
cin >> num;
return 0;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
return 0;
}
int main() {
string ones[] = {"", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"};
string tens[] = {"", "", "twenty", "thirty", "fourty",
"fifty", "sixty", "seventy", "eighty", "ninety"};
string teens[] = {"ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
"nineteen"};
int num, digit;
string word;
if (num == 0) {
76. Write a program to print all ASCII characters with their values.
#include <iostream>
int main() {
for (int i = 0; i <= 127; i++) {
cout << "ASCII value of " << char(i) << " is " << i <<
endl;
}
return 0;
}
77. Write a program to find the power of a number using for loop.
#include <iostream>
using namespace std;
int main() {
int base, exponent, result = 1;
cout << base << "^" << exponent << " = " << result << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Factors of " << num << " are: ";
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
cout << i << " ";
}
}
cout << endl;
return 0;
}
int main() {
int num, fact = 1;
cout << num << "! = " << fact << endl;
return 0;
80. Write a program to find the HCF (GCD) and LCM of two
numbers.
#include <iostream>
using namespace std;
int main() {
int num1, num2, hcf, lcm;
cout << "HCF of " << num1 << " and " << num2 << " is " << hcf
<< endl;
cout << "LCM of " << num1 << " and " << num2 << " is " << lcm
<< endl;
return 0;
}
int main() {
int num, count = 0;
return 0;
}
82. Write a program to find the sum of all prime numbers between 1
to n.
#include <iostream>
using namespace std;
bool isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
int n, sum = 0;
cout << "Enter a number: ";
cin >> n;
cout << "The sum of all prime numbers between 1 and " << n <<
" is: " << sum << endl;
return 0;
}
void primeFactors(int n) {
while (n % 2 == 0) {
cout << 2 << " ";
n /= 2;
}
if (n > 2) {
cout << n;
}
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Prime factors of " << n << " are: ";
primeFactors(n);
return 0;
}
while(num != 0){
rem = num % 10;
sum += (rem * rem * rem);
num /= 10;
}
if(cNum == sum){
cout<<cNum<<" is armstrong number.";
}
else{
cout<<cNum<<" isn't armstrong number.";
}
}
#include <math.h>
using namespace std;
bool isArmstrong(int n) {
int originalNum, numDigits, sum = 0;
originalNum = n;
numDigits = (int) log10(n) + 1;
while (n > 0) {
int remainder = n % 10;
sum += power(remainder, numDigits);
n /= 10;
}
return (sum == originalNum);
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Armstrong numbers between 1 and " << n << " are: ";
for (int i = 1; i <= n; i++) {
if (isArmstrong(i)) {
cout << i << " ";
}
}
return 0;
}
int main() {
int n, sum = 0;
if (sum == n) {
cout << n << " is a Perfect number." << endl;
}
else {
cout << n << " is not a Perfect number." << endl;
}
return 0;
}
int main() {
int n, sum;
cout << "Perfect numbers between 1 and " << n << " are: ";
for (int i = 1; i <= n; i++) {
sum = 0;
for (int j = 1; j <= i/2; j++) {
if (i % j == 0) {
sum += j;
}
}
if (sum == i) {
cout << i << " ";
}
}
return 0;
}
int main() {
int n, fact, sum = 0, temp;
temp = n;
while (temp != 0) {
fact = 1;
int digit = temp % 10;
for (int i = 1; i <= digit; i++) {
fact *= i;
}
sum += fact;
temp /= 10;
}
if (sum == n) {
cout << n << " is a Strong number." << endl;
}
else {
cout << n << " is not a Strong number." << endl;
}
return 0;
}
int main() {
int n, fact, sum, temp;
cout << "Strong numbers between 1 and " << n << " are: ";
for (int i = 1; i <= n; i++) {
sum = 0;
temp = i;
while (temp != 0) {
fact = 1;
int digit = temp % 10;
for (int j = 1; j <= digit; j++) {
fact *= j;
}
sum += fact;
temp /= 10;
}
if (sum == i) {
cout << i << " ";
}
}
return 0;
}
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;
int main()
{
int num, originalNum, sum = 0, product = 1, lastDigit;
return 0;
}
int main()
{
int n;
cout << "Enter the value of N: ";
cin >> n;
cout << "Spy numbers from 1 to " << n << ": ";
for(int i = 1; i <= n; i++)
{
int num = i, sum = 0, product = 1, lastDigit;
while(num > 0)
{
lastDigit = num % 10;
sum = sum + lastDigit;
product = product * lastDigit;
num = num / 10;
}
if(sum == product)
{
cout << i << " ";
}
}
return 0;
}
int main() {
int arr[5];
cout << "Enter 5 elements of the array: ";
for(int i=0; i<5; i++) {
cin >> arr[i];
}
cout << "The elements of the array are: ";
for(int i=0; i<5; i++) {
cout << arr[i] << " ";
}
return 0;
}
int main() {
int arr[6] = {1, 2, 3, 4, 5};
int n, pos;
cout << "Enter the number to insert: ";
cin >> n;
cout << "Enter the position to insert: ";
cin >> pos;
for(int i=5; i>=pos; i--) {
arr[i] = arr[i-1];
}
arr[pos-1] = n;
cout << "The updated array is: ";
for(int i=0; i<6; i++) {
int main() {
int arr[5], sum=0;
cout << "Enter 5 elements of the array: ";
for(int i=0; i<5; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << "The sum of the elements of the array is: " << sum;
return 0;
}
int main() {
int arr[5], sum=0;
cout << "Enter 5 numbers: ";
for(int i=0; i<5; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << "The sum of the numbers is: " << sum;
return 0;
}
int main() {
int arr[100], n, sum=0;
float avg;
cout << "Enter the number of elements: ";
cin >> n;
cout << "Enter the elements of the array: ";
for(int i=0; i<n; i++) {
cin >> arr[i];
sum += arr[i];
}
avg = (float) sum / n;
cout << "The sum of the elements is: " << sum << endl;
cout << "The average of the elements is: " << avg;
return 0;
}
int main() {
int arr[10] = {3, 5, 2, 7, 1, 9, 4, 8, 6, 0};
int max = arr[0], min = arr[0];
int n = 10;
cout << "Maximum element in the array is " << max << endl;
cout << "Minimum element in the array is " << min << endl;
return 0;
}
int main() {
int arr[10] = {3, -5, 2, -7, 1, 9, -4, 8, -6, 0};
int n = 10;
return 0;
}
int main() {
int arr1[5] = {1, 2, 3, 4, 5};
int arr2[5];
101. Write a program to sort all the elements of an array and find the
largest element from that array.
#include <iostream>
#include <algorithm>
int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
return 0;
}
int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
104. Write a program to count the total number of negative, even, and
odd numbers in an array.
#include <iostream>
int main() {
int arr[] = {5, -2, 9, 0, -5, 6};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int positionToDelete = 2; // Specify the position to
delete (0-based index)
return 0;
}
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int duplicateCount = 0;
cout << "Total Duplicate Elements: " << duplicateCount << endl;
return 0;
}
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
unordered_set<int> uniqueElements;
int newArray[size];
int newSize = 0;
if (uniqueElements.find(arr[i]) ==
uniqueElements.end()) {
newArray[newSize] = arr[i];
uniqueElements.insert(arr[i]);
newSize++;
}
}
return 0;
}
int main() {
int arr1[] = {1, 2, 3};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int arr2[] = {4, 5, 6};
int size2 = sizeof(arr2) / sizeof(arr2[0]);
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int reversedArray[size];
return 0;
}
112. Write a program to put the even and odd elements of an array in
two separate arrays.
#include <iostream>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 3; // Element to search for
bool found = false;
if (!found) {
cout << "Element not found in the array." << endl;
}
return 0;
}
int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int d = 2; // Number of positions to right rotate
return 0;
}
118. Suppose you have an Array with the size of 10. Your program
will input all the array elements from the user. Now using a loop,
traverse the array. During traversing, if the array contains an odd
number in the odd index, take the odd value from that odd index
from the array and make the summation of those numbers and
replace that index value with 0. Print the summation and the array.
The given sample is for your understanding. You must use your own
sample.
Example:
Before the operation:
Index 1 2 3 4 5 6 7 8 9 10
Elements 1 3 6 5 7 9 11 8 3 8
Elements:
Elements 0 3 6 5 0 9 0 8 0 8
Summation = 22
#include <iostream>
int main() {
int arr[10];
int sum = 0;
cout << "Sum of Odd Numbers in Odd Indices: " << sum <<
endl;
return 0;
}
119. The scenario in front of any virtual Bank is like Men and women
standing in a single line. It looks so bad. Now separate men and
women into different two line that looks like a gentle management
system and develop the above program to find the majority of
gender in the line.
M = male
W = Women
After operation:
M M M M M M M
W W W
#include <iostream>
#include <vector>
int main() {
vector<char> bankLine; // Use 'M' for men and 'F' for
women
while (true) {
cin >> person;
if (person == 'X' || person == 'x') {
break;
}
bankLine.push_back(person);
}
return 0;
}
given number by your teacher, then you will say “Sir, there’s no pair
of numbers equal to your number” otherwise you will show that two
numbers which addition is equal to the given number by your
teacher. Write a program to solve the situation.
#include <iostream>
#include <unordered_set>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 9; // Number given by the teacher
unordered_set<int> numbers;
return 0;
}
121. Suppose you have taken some values in an array of any size. For
example, array1 = [5, 2, 3, 1, 9, 4]. Now your friend requests you to
shift array values one cell to the right side (Right Shift). As per the
request of your friend, array 1 will be now [4, 5, 2, 3, 1, 9]. Now
write a C program to satisfy the request of your friend by choosing
an appropriate technique to shift the array values to the right.
Note: The array values and size will be defined by the user. Sample
Input: 9 3 8 2 7 1
Sample Output: 1 9 3 8 2 7
#include <iostream>
int main() {
int arr[] = {5, 2, 3, 1, 9, 4};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
#include <iostream>
int main() {
int rows = 3;
int cols = 3;
int matrix[3][3] = {{1, 2, 3},
{1, 2, 3},
{1, 2, 3}};
return 0;
}
int main() {
int matrix[rows][cols];
return 0;
}
int main() {
int rows, cols;
cout << "Enter the number of rows and columns for
matrices: ";
cin >> rows >> cols;
cout << "Enter the elements of the first matrix:" << endl;
for (int i = 0; i < rows; i++) {
// Addition
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Subtraction
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
diff[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
// Multiplication
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
product[i][j] = 0;
for (int k = 0; k < cols; k++) {
product[i][j] += matrix1[i][k] *
matrix2[k][j];
}
}
}
return 0;
}
int main() {
int rows, cols;
cout << "Enter the number of rows and columns for the
matrix: ";
cin >> rows >> cols;
return 0;
}
126. Write a program to check whether two matrices are equal or not
and find the multiply of those two matrices.
#include <iostream>
int main() {
int rows, cols;
cout << "Enter the number of rows and columns for
matrices: ";
cin >> rows >> cols;
cout << "Enter the elements of the first matrix:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix1[i][j];
}
}
}
}
if (equal) {
// Matrices are equal, calculate their product
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
product[i][j] = matrix1[i][j] * matrix2[i][j];
}
}
}
return 0;
}
127. Write a program to find the sum of the main diagonal elements of
a matrix.
#include <iostream>
int main() {
int n;
cout << "Enter the size of the square matrix: ";
cin >> n;
int matrix[n][n];
int sum = 0;
sum += matrix[i][j];
}
}
}
cout << "Sum of the main diagonal elements: " << sum <<
endl;
return 0;
}
int main() {
int n;
cout << "Enter the size of the square matrix: ";
cin >> n;
int matrix[n][n];
int sum = 0;
cout << "Sum of the minor diagonal elements: " << sum <<
endl;
return 0;
}
129. Write a program to find the sum of each row and column of a
matrix.
#include <iostream>
int main() {
int rows, cols;
cout << "Enter the number of rows and columns for the
matrix: ";
cin >> rows >> cols;
int matrix[rows][cols];
}
cout << "Row " << i + 1 << ": " << rowSum << endl;
}
return 0;
}
int main() {
int n;
cout << "Enter the size of the matrix: ";
cin >> n;
int matrix[n][n];
return 0;
}
int main() {
int n;
cout << "Enter the size of the matrix: ";
cin >> n;
int matrix[n][n];
return 0;
}
int main() {
int n;
cout << "Enter the size of the matrix: ";
cin >> n;
int matrix[n][n];
return 0;
}
133. Write a program to find the sum of the upper and lower triangular
matrix.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of the matrix: ";
cin >> n;
int matrix[n][n];
return 0;
}
int main() {
int m, n;
cout << "Enter the number of rows: ";
cin >> m;
cout << "Enter the number of columns: ";
cin >> n;
int matrix[m][n];
return 0;
}
float det = 0;
float submatrix[MAX_SIZE][MAX_SIZE];
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x)
continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
det += (x % 2 == 0 ? 1 : -1) * matrix[0][x] *
determinant(submatrix, n - 1);
}
return det;
}
int main() {
int n;
cout << "Enter the size of the square matrix: ";
cin >> n;
float matrix[MAX_SIZE][MAX_SIZE];
return 0;
}
int main() {
int n;
cout << "Enter the size of the square matrix: ";
cin >> n;
int matrix[10][10];
if (isIdentity) {
cout << "The matrix is an Identity matrix." << endl;
} else {
return 0;
}
int main() {
int m, n;
cout << "Enter the number of rows: ";
cin >> m;
cout << "Enter the number of columns: ";
cin >> n;
int matrix[10][10];
}
}
}
return 0;
}
int main() {
int n;
cout << "Enter the size of the square matrix: ";
cin >> n;
int matrix[10][10];
if (matrix[i][j] != matrix[j][i]) {
isSymmetric = false;
break;
}
}
if (!isSymmetric) {
break;
}
}
if (isSymmetric) {
cout << "The matrix is a Symmetric matrix." << endl;
} else {
cout << "The matrix is not a Symmetric matrix." <<
endl;
}
return 0;
}
cout<<"*";
}
cout<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
int i, j;
for(i = 1; i <= 4; i++){
for(j = 4; j >= i; j--){
cout<<"*";
}
cout<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
return 0;
}
#include<iostream>
using namespace std;
int main(){
for(int i = 1; i <= 5; i++){
144. Write a program to display the pattern like a right angle triangle
with a number.
1
12
123
1234
#include<iostream>
using namespace std;
int main(){
int i, j;
for(i = 1; i <= 4; i++){
for(j = 1; j <= i; j++){
cout<<j<<" ";
}
cout<<endl;
}
return 0;
}
145.
#include <iostream>
using namespace std;
int main() {
while(k != 2*i-1) {
if (count <= rows-1) {
cout << i+k << " ";
++count;
}
else {
++count1;
cout << i+k-2*count1 << " ";
}
++k;
}
count1 = count = k = 0;
146.
*
**
***
****
*****
****
***
**
*
#include <iostream>
using namespace std;
int main() {
int n = 5; // Change this to the desired number of rows
return 0;
}
148. Write a program to make such a pattern like a right angle triangle
with the number increased by 1.
#include<iostream>
using namespace std;
int main(){
//i = row number
//j = how many columns
int temp = 1;
return 0;
}
#include <iostream>
using namespace std;
int main() {
return 0;
}
int main() {
int n = 4; // Number of rows
// Print numbers
for (int j = 1; j <= i; j++) {
cout << i;
if (j < i) {
cout << " ";
}
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n = 5; // Size of the pattern
int center = n / 2; // Center position
return 0;
}
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
int n=4; // took a default value
for (int i = n; i >= 1; --i) { // loop for iterating
for (int j = 1; j <= i; ++j) { // loop for printing
cout << j << " ";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
return 0;
}
155. 1 + 2 + 3 + 4 + 5 + 6 =?
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 1; i <= 6; i++) {
sum += i;
}
cout << "Sum of numbers from 1 to 6: " << sum << endl;
return 0;
}
156. 1 + 2 + 3 + 4 + 5 + 6 + …… + 20 =?
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 1; i <= 20; i++) {
sum += i;
}
cout << "Sum of numbers from 1 to 20: " << sum << endl;
return 0;
}
int main() {
int N;
cout << "Enter a value for N: ";
cin >> N;
int sum = 0;
for (int i = 1; i <= N; i++) {
sum += i;
}
cout << "Sum of numbers from 1 to " << N << ": " << sum <<
endl;
return 0;
}
// Driver code
int main()
{
int n = 4;
cout << seriesSum(n);
return 0;
}
int main() {
int sum = 0;
for (int i = 10; i <= 50; i++) {
sum += i;
}
cout << "The summation between 10 to 50 is: " << sum <<
endl;
return 0;
}
int main() {
int oneToFifty, ontToNine;
cout << "The summation between 10 to 50 is: " << result <<
endl;
return 0;
}
161. 2 + 4 + 6 + ……… + 10 = ?
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 2; i <= 10; i += 2) {
sum += i;
}
cout << "Sum of even numbers from 2 to 10: " << sum <<
endl;
return 0;
}
int main() {
int N;
cout << "Enter a value for N: ";
cin >> N;
int sum = 0;
for (int i = 2; i <= N; i += 2) {
sum += i;
}
cout << "Sum of even numbers from 2 to " << N << ": " <<
sum << endl;
return 0;
}
int evenSum(int n)
{
return (n * (n + 1));
}
int main() {
int sum = 0;
for (int i = 10; i <= 50; i++) {
if (i % 2 == 0) {
sum += i;
}
}
cout << "The summation of even numbers between 10 to 50
is: " << sum << endl;
return 0;
}
int main() {
int sum = 0;
sum = (10 + 50) * 21 / 2;
cout << "The summation of even numbers between 10 to 50
is: " << sum << endl;
return 0;
}
166. 1 + 3 + 5 + ……… + 9 = ?
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 1; i <= 9; i += 2) {
sum += i;
}
cout << "Sum of odd numbers from 1 to 9: " << sum << endl;
return 0;
}
int main() {
int N;
cout << "Enter a value for N: ";
cin >> N;
int sum = 0;
for (int i = 1; i <= N; i += 2) {
sum += i;
}
cout << "Sum of odd numbers from 1 to " << N << ": " <<
sum << endl;
return 0;
}
int main() {
int N;
cout << "Enter a value for N: ";
cin >> N;
int sum = (N + 1) / 2 * (N + 1) / 2;
cout << "Sum of odd numbers from 1 to " << N << ": " <<
sum << endl;
return 0;
}
int main() {
int sum = 0;
cout << "Sum of odd numbers between " << 10 << " and " <<
50 << " is: " << sum << endl;
return 0;
}
Output: Sum of odd numbers between 10 and 50 is: 600
int main() {
int start = 11; // The first odd number in the range
int end = 49; // The last odd number in the range
int numberOfOdds = (end - start) / 2 + 1; // Calculate the
number of odd numbers
int sum;
cout << "Sum of odd numbers between 10 and 50 is: " << sum
<< endl;
return 0;
}
2 2 2
171. 1 + 2 + 3 = ?
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 1; i <= 3; i++) {
sum += i * i;
}
cout << "Sum of squares of numbers from 1 to 3: " << sum
<< endl;
return 0;
}
2 2 2 2
172. 1 + 2 + 3 + ……………………. + 𝑁 = ?
#include <iostream>
using namespace std;
int main() {
int N;
cout << "Enter a value for N: ";
cin >> N;
int sum = 0;
for (int i = 1; i <= N; i++) {
sum += i * i;
}
cout << "Sum of squares of numbers from 1 to " << N << ":
" << sum << endl;
return 0;
}
int main() {
int N;
cout << "Enter a value for N: ";
cin >> N;
int main() {
int n;
cout << "Enter the number of terms: ";
cin >> n;
int sum = 0;
int term = 9;
for (int i = 1; i <= n; i++) {
sum += term;
term = term * 10 + 9;
}
cout << "Sum of the series: " << sum << endl;
return 0;
}
175. Find the sum of the first 10 terms of the arithmetic series: 3, 7,
11, 15, ...
#include <iostream>
using namespace std;
int main() {
int firstTerm = 3;
int commonDifference = 4;
int n = 10; // Number of terms
176. Find the sum of the first 5 terms of the geometric series: 2, 4, 8,
16, ...
#include <iostream>
#include<math.h>
using namespace std;
int main() {
int firstTerm = 2;
int commonRatio = 2;
int n = 5; // Number of terms
177. Find the nth term of the arithmetic series: 5, 8, 11, 14, ...
#include <iostream>
using namespace std;
int main() {
int firstTerm = 5;
int commonDifference = 3;
int n;
178. Find the sum of the first 12 terms of the series: 1/2, 2/3, 3/4, 4/5,
...
#include <iostream>
using namespace std;
int main() {
int n = 12; // Number of terms
179. Find the sum of the infinite series: 1/2 + 1/4 + 1/8 + 1/16 + ...
#include <iostream>
using namespace std;
int main() {
double sum = 0.0;
double term = 1.0;
int n = 10; // You can change the value of n for more
accuracy
180. Find the sum of the first 10 terms of the series: 1 + 4 + 9 + 16 + ...
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int n = 10; // Number of terms
181. Find the nth term of the geometric series: 3, 6, 12, 24, ...
#include <iostream>
#include<math.h>
using namespace std;
int main() {
int firstTerm = 3;
int commonRatio = 2;
int n;
182. Find the sum of the first 20 terms of the series: 2, 4, 6, 8, 10, ...
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int n = 20; // Number of terms
183. Find the sum of the first 6 terms of the series: -1, -3, -5, -7, ...
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int n = 6; // Number of terms
184. Find the sum of the infinite series: 1 + 1/2 + 1/4 + 1/8 + ...
#include <iostream>
using namespace std;
int main() {
double a = 1.0; // First term
double r = 1.0 / 2.0; // Common ratio
cout << "Sum of the infinite series: " << sum << endl;
return 0;
}
int main() {
char str[100];
cout << "Length of the string: " << length << endl;
return 0;
}
int main() {
char source[100], destination[100];
strcpy(destination, source);
return 0;
}
int main() {
char str1[100], str2[100];
strcat(str1, str2);
return 0;
}
int main() {
char str1[100], str2[100];
if (result == 0) {
cout << "Both strings are equal." << endl;
} else if (result < 0) {
cout << "String 1 is less than String 2." << endl;
} else {
cout << "String 1 is greater than String 2." << endl;
}
return 0;
}
int main() {
char str[100];
return 0;
}
int main() {
char str[100];
cout << "Toggled case string: " << str << endl;
return 0;
}
int main() {
char str[100];
return 0;
}
bool isVowel(char c) {
c = tolower(c);
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c
== 'u');
}
int main() {
char str[100];
return 0;
}
int main() {
char str[100];
if (isPalindrome(str)) {
cout << "The string is a palindrome." << endl;
} else {
cout << "The string is not a palindrome." << endl;
}
return 0;
}
}
}
}
int main() {
char str[100];
reverseWords(str);
return 0;
}
195. Write a program to find the first and last occurrence of a character
in a given string.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[100];
char target;
if (firstOccurrence != -1) {
cout << "First occurrence of '" << target << "' at
index: " << firstOccurrence << endl;
cout << "Last occurrence of '" << target << "' at
index: " << lastOccurrence << endl;
} else {
cout << "Character '" << target << "' not found in the
string." << endl;
}
return 0;
}
int main() {
char str[100];
char target;
cin.getline(str, 100);
return 0;
}
int main() {
char str[100];
char target;
int count = 0;
cout << "Occurrences of '" << target << "': " << count <<
endl;
return 0;
}
int main() {
char str[100];
return 0;
}
int main() {
char str[100];
cin.getline(str, 100);
return 0;
}
int main() {
char str[100];
char target;
return 0;
}
int main() {
char str[100];
char target;
if (lastOccurrence != -1) {
for (int j = lastOccurrence; str[j] != '\0'; j++) {
str[j] = str[j + 1];
}
}
return 0;
}
int main() {
char str[100];
char target;
return 0;
}
int main() {
char str[100];
int j = 0;
for (int i = 0; i < length; i++) {
if (!unique[str[i]]) {
str[j] = str[i];
j++;
unique[str[i]] = true;
}
}
str[j] = '\0';
return 0;
}
int main() {
char str[100];
char target, replacement;
cout << "String after replacement: " << str << endl;
return 0;
}
int main() {
char str[100];
char target, replacement;
if (lastOccurrence != -1) {
str[lastOccurrence] = replacement;
}
cout << "String after replacement: " << str << endl;
return 0;
}
int main() {
char str[100];
char target, replacement;
cout << "String after replacement: " << str << endl;
return 0;
}
207. Write a program to find the first and last occurrence of a word in
a given string.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[100];
char word[20];
if (firstOccurrence != -1) {
cout << "First occurrence of '" << word << "' at
index: " << firstOccurrence << endl;
cout << "Last occurrence of '" << word << "' at index:
" << lastOccurrence << endl;
} else {
cout << "Word '" << word << "' not found in the
string." << endl;
}
return 0;
}
int main() {
char str[100];
char word[20];
return 0;
}
int main() {
char str[100];
char word[20];
cout << "Occurrences of '" << word << "': " << count <<
endl;
return 0;
}
}
break;
}
}
}
int main() {
char str[100];
char word[20];
removeFirstWord(str, word);
return 0;
}
if (lastOccurrence != -1) {
for (int j = lastOccurrence; j <= length - wordLength;
j++) {
str[j] = str[j + wordLength];
}
}
}
int main() {
char str[100];
char word[20];
removeLastWord(str, word);
return 0;
}
int main() {
char str[100];
char word[20];
removeAllOccurrences(str, word);
return 0;
}
if (i > 0) {
for (int j = 0; j <= length - i; j++) {
str[j] = str[j + i];
}
}
}
int main() {
char str[100];
trimLeadingWhitespace(str);
return 0;
}
int main() {
char str[100];
trimTrailingWhitespace(str);
return 0;
}
215. Write a program to trim both leading and trailing white space
characters from a given string.
#include <iostream>
#include <cstring>
using namespace std;
str[i] = str[j];
}
str[i] = '\0';
}
int main() {
char str[100];
trimWhitespace(str);
return 0;
}
216. Write a program to remove all extra blank spaces from a given
string.
#include <iostream>
#include <cstring>
using namespace std;
str[j] = '\0';
}
int main() {
char str[100];
removeExtraSpaces(str);
cout << "String after removing extra blank spaces: " <<
str << endl;
return 0;
}
217.
Given a string S. Print the origin string after replacing the following:
● Replace every comma character ',' with a space character.
● Replace every capital character in S with its respective small
character and Vice Versa.
#include <iostream>
#include <cctype> // For isupper and islower
using namespace std;
int main() {
string s;
getline(cin, s); // Read input as a string
for (char i : s) {
if (i == ',') {
emptyS += " ";
} else if (isupper(i)) {
emptyS += tolower(i);
} else if (islower(i)) {
emptyS += toupper(i);
}
}
return 0;
}
218. Given a string S. Print the number of times that "EGYPT" word
can be formed from S's characters.
Note: Case of the letters doesn't matter. For example: "Egypt", "egypt" and
"eGyPt" are the same.
EgYpTaz 1
pemigdbeigyypetet 2
#include <iostream>
#include <string>
#include <algorithm> // For min
#include <climits> // For INT_MAX
using namespace std;
int main() {
string s;
getline(cin, s); // Read input as a string
return 0;
}
Input Output
BRITISHEGYPTGHANA BRITISH GHANA
ITALYKOREAEGYPTEGYPTAL ITALYKOREA ALGERIA Z
GERIAEGYPTZ
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s); // Read input as a string
return 0;
}
220. Given a string S. Print the summation of its digits. It's guaranteed
that S contains only digits from 0 to 9.
Input Output
351 9
123 6
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
getline(cin, input); // Read input as a string
int sum = 0;
return 0;
}
cout<<total<<endl;
}
int main(){
output();
return 0;
}
int main() {
double num1, num2;
char operation;
double result;
switch (operation) {
case '+':
result = add(num1, num2);
break;
case '-':
result = subtract(num1, num2);
break;
case '*':
result = multiply(num1, num2);
break;
case '/':
result = divide(num1, num2);
break;
default:
cout << "Invalid operation." << endl;
return 1;
}
return 0;
}
int main() {
double number;
cout << "Cube of " << number << " is " << result << endl;
return 0;
}
int main() {
double radius;
return 0;
}
int main() {
double num1, num2;
return 0;
}
int main() {
int number;
if (isEven(number)) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}
return 0;
}
num = originalNum;
while (num > 0) {
int digit = num % 10;
sum += pow(digit, numDigits);
num /= 10;
}
int main() {
int start, end;
return 0;
}
num = originalNum;
while (num > 0) {
int digit = num % 10;
sum += pow(digit, numDigits);
num /= 10;
}
int main() {
int start, end;
}
}
cout << endl;
return 0;
}
int main() {
int rows, cols;
int mat1[MAX_ROWS][MAX_COLS];
int mat2[MAX_ROWS][MAX_COLS];
int result[MAX_ROWS][MAX_COLS];
return 0;
}
}
}
}
}
int main() {
int rows1, cols1, rows2, cols2;
int mat1[MAX_ROWS][MAX_COLS];
int mat2[MAX_ROWS][MAX_COLS];
int result[MAX_ROWS][MAX_COLS];
// Perform addition
addMatrices(mat1, mat2, result, rows1, cols1);
// Perform subtraction
subtractMatrices(mat1, mat2, result, rows1, cols1);
// Perform multiplication
multiplyMatrices(mat1, mat2, result, rows1, cols1, cols2);
// Perform division
divideMatrices(mat1, mat2, result, rows1, cols1);
return 0;
}
Pointer (6 Problems)
int main() {
int num = 42;
int* ptr = #
return 0;
}
int main() {
int num1 = 10, num2 = 20;
int* ptr1 = &num1;
int* ptr2 = &num2;
cout << "Sum of " << *ptr1 << " and " << *ptr2 << " is: "
<< sum << endl;
return 0;
int main() {
int num1 = 10, num2 = 20;
cout << "Before swapping: num1 = " << num1 << ", num2 = "
<< num2 << endl;
swapNumbers(&num1, &num2);
cout << "After swapping: num1 = " << num1 << ", num2 = "
<< num2 << endl;
return 0;
}
235. Write a program to input and print array elements using a pointer.
#include <iostream>
using namespace std;
int main() {
cout << "Enter " << size << " integers:" << endl;
return 0;
}
int main() {
const int size = 5;
int source[size] = {1, 2, 3, 4, 5};
int destination[size];
*destPtr = *srcPtr;
srcPtr++;
destPtr++;
}
return 0;
}
237. Write a program to add and multiply two matrices using pointers.
#include <iostream>
using namespace std;
int main() {
int matrix1[ROWS][COLS] = {{1, 2}, {3, 4}};
int matrix2[ROWS][COLS] = {{5, 6}, {7, 8}};
int sumMatrix[ROWS][COLS];
int productMatrix[ROWS][COLS];
return 0;
}
238. Write a program to create a file and write contents, and save and
close the file.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outputFile("sample.txt"); // Create and open a
file for writing
if (!outputFile) {
cerr << "Failed to open the file for writing." <<
endl;
return 1;
}
return 0;
}
239. Write a program to read file contents and display them on the
console.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inputFile("sample.txt"); // Open the file for
reading
if (!inputFile) {
cerr << "Failed to open the file for reading." <<
endl;
return 1;
}
string line;
return 0;
}
240. Write a program to read numbers from a file and write even, odd
and prime numbers to separate files.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main() {
ifstream inputFile("numbers.txt"); // Open the file for
reading
if (!inputFile) {
cerr << "Failed to open the file for reading." <<
endl;
return 1;
}
int num;
while (inputFile >> num) {
if (num % 2 == 0) {
evenFile << num << " ";
} else {
if (isPrime(num)) {
primeFile << num << " ";
}
}
inputFile.close();
evenFile.close();
oddFile.close();
primeFile.close();
return 0;
}
241. Write a program to copy the contents of one text file to another.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inputFile("source.txt"); // Open the source file
for reading
ofstream outputFile("destination.txt"); // Open the
destination file for writing
if (!inputFile) {
cerr << "Failed to open the source file." << endl;
return 1;
}
if (!outputFile) {
char ch;
while (inputFile.get(ch)) {
outputFile.put(ch);
}
inputFile.close();
outputFile.close();
return 0;
}
int main() {
ifstream inputFile("file.txt"); // Open the file for
reading
if (!inputFile) {
cerr << "Failed to open the file." << endl;
return 1;
}
int lineCount = 0;
string line;
inputFile.close();
cout << "Number of lines in the file: " << lineCount <<
endl;
return 0;
}
243. Write a program to search for a specific word in a text file and
display the line numbers where it occurs.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inputFile("file.txt"); // Open the file for
reading
if (!inputFile) {
cerr << "Failed to open the file." << endl;
return 1;
}
string wordToSearch;
cout << "Enter the word to search: ";
cin >> wordToSearch;
string line;
int lineNumber = 0;
inputFile.close();
return 0;
}
244. Write a program to read and display the contents of a binary file.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inputFile("binaryfile.bin", ios::binary); // Open
the binary file for reading in binary mode
if (!inputFile) {
cerr << "Failed to open the binary file." << endl;
return 1;
}
char ch;
while (inputFile.get(ch)) {
cout << ch;
}
inputFile.close();
return 0;
}
245. Write a program to merge two text files into a single text file.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inputFile1("file1.txt"); // Open the first file
for reading
ifstream inputFile2("file2.txt"); // Open the second file
for reading
ofstream outputFile("mergedfile.txt"); // Open the merged
file for writing
if (!inputFile1 || !inputFile2) {
cerr << "Failed to open one or both input files." <<
endl;
return 1;
}
if (!outputFile) {
cerr << "Failed to open the output file." << endl;
return 1;
}
char ch;
while (inputFile1.get(ch)) {
outputFile.put(ch);
}
while (inputFile2.get(ch)) {
outputFile.put(ch);
}
inputFile1.close();
inputFile2.close();
outputFile.close();
return 0;
}
int main() {
ifstream inputFile("data.csv"); // Open the CSV file for
reading
if (!inputFile) {
cerr << "Failed to open the CSV file." << endl;
return 1;
}
string line;
vector<vector<string>> data;
data.push_back(row);
}
inputFile.close();
return 0;
}
struct Student {
int rollNumber;
string name;
double marks;
};
int main() {
ifstream inputFile("student_records.bin", ios::binary); //
Open the binary file for reading in binary mode
if (!inputFile) {
cerr << "Failed to open the binary file." << endl;
return 1;
}
Student student;
while (inputFile.read(reinterpret_cast<char*>(&student),
sizeof(Student))) {
cout << "Roll Number: " << student.rollNumber << endl;
cout << "Name: " << student.name << endl;
cout << "Marks: " << student.marks << endl;
cout << "-------------------" << endl;
}
inputFile.close();
return 0;
}
248. Write a program to read a text file and count the occurrences of a
specific word.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inputFile("textfile.txt"); // Open the text file
for reading
if (!inputFile) {
cerr << "Failed to open the file." << endl;
return 1;
}
string wordToCount;
cout << "Enter the word to count: ";
cin >> wordToCount;
string word;
int count = 0;
inputFile.close();
cout << "The word '" << wordToCount << "' appears " <<
count << " times in the file." << endl;
return 0;
}
249. Write a program to read data from a CSV file containing product
information, calculate the total price of all products, and display it.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct Product {
string name;
double price;
int quantity;
};
int main() {
ifstream inputFile("products.csv"); // Open the CSV file
for reading
if (!inputFile) {
cerr << "Failed to open the CSV file." << endl;
return 1;
}
string line;
double totalPrice = 0.0;
inputFile.close();
cout << "Total price of all products: $" << totalPrice <<
endl;
return 0;
}
250. Write a program that reads a text file containing names and their
corresponding ages, and then calculates and displays the average age
of all individuals in the file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inputFile("names_and_ages.txt"); // Open the text file for reading
if (!inputFile) {
cerr << "Failed to open the file." << endl;
return 1;
}
string name;
int age;
int totalAge = 0;
int personCount = 0;
inputFile.close();
if (personCount > 0) {
double averageAge = static_cast<double>(totalAge) / personCount;
cout << "Average age of all individuals: " << averageAge << " years" << endl;
} else {
cout << "No data found in the file." << endl;
}
return 0;
}