Q29: Write a C++ program to calculate the summation of the following sequence for nth terms.
𝑆𝑢𝑚 = 1/1 + 2/4 + 3/9 + ....
𝑆𝑢𝑚 = 1/1 + 2/4 + 3/9 + ....
#include <iostream>
using namespace std;
int main()
{
int n;
double sum = 0;
cout<<" enter the value of n : ";
cin>>n;
for( int i = 1; i <= n; i++)
{
sum += float (i) / (i*i);
}
cout<<" the summation the following sequence for "<<n<<" is : "<<sum<<endl;
return 0;
}
👍1
Q30: C++ code to find the summation of the following series.
Sum = a + a^2 + a^3 + ... + a^n
Sum = a + a^2 + a^3 + ... + a^n
#include <iostream>
using namespace std;
int main() {
int a, n;
int sum = 0;
cout << "Enter the value of 'a': ";
cin >> a;
cout << "Enter the value of 'n': ";
cin >> n;
// Calculating the summation of the series
for (int i = 1; i <= n; ++i) {
int term = 1;
for (int j = 1; j <= i; ++j) {
term *= a;
}
sum += term;
}
cout << "The summation of the series is: " << sum << endl;
return 0;
}
❤1
Q31: C++ code to find the summation of the following series for nth terms.
𝑆𝑢𝑚 = 1/a^1 + 2/a^2 + 3/a^3 + ....
𝑆𝑢𝑚 = 1/a^1 + 2/a^2 + 3/a^3 + ....
#include <iostream>
using namespace std;
int main() {
int a, n;
float sum = 0.0;
float power_of_a = 1.0;
cout << "Enter the value of 'a': ";
cin >> a;
cout << "Enter the value of 'n': ";
cin >> n;
// Calculating the summation of the series
for (int i = 1; i <= n; ++i) {
power_of_a *= a; // Update power_of_a by multiplying it by a
sum += float(i) / power_of_a;
}
cout << "The summation of the series up to the " << n << "th term is: " << sum << endl;
return 0;
}
❤1
Q32: C++ code to find the summation of the following series.
Sum = 1 + 2! + 3! + ⋯ + n!
Sum = 1 + 2! + 3! + ⋯ + n!
#include <iostream>
using namespace std;
int main() {
int n;
int sum = 0;
int factorial = 1;
cout << "Enter the value of 'n': ";
cin >> n;
for (int i = 1; i <= n; ++i) {
factorial *= i;
sum += factorial;
}
cout << "The summation of the series is: " << sum << endl;
return 0;
}
❤1👍1
Q33: Grades in the programming lab are classified according to letters A case of Excellent, B case of Well done, C case of done, D case of Pass and F case of Try again.
Write C++ code to implement this logic using Switch statement.
Write C++ code to implement this logic using Switch statement.
#include <iostream>
using namespace std;
int main ()
{
char grade ;
cout << " take one from this ( A , B, C, D, F) ";
cin >> grade;
switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
cout << "Well done" << endl;
break;
case 'C' :
cout << "done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
❤1
Q34: Create a C++ program that takes five integers as input and determines whether their product is positive or negative.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3, num4, num5;
int product;
cout << "Enter five integers: ";
cin >> num1 >> num2 >> num3 >> num4 >> num5;
product = num1 * num2 * num3 * num4 * num5;
if (product > 0)
{
cout << "The product is positive." << endl;
}
else if (product < 0)
{
cout << "The product is negative." << endl;
}
else
{
cout << "The product is zero." << endl;
}
return 0;
}
❤2
Q35: Write a C++ program that takes a day number (1-7) as input and prints the corresponding day of the week.
#include <iostream>
using namespace std;
int main()
{
int daynumber;
cout << " enter the day ( 1 -7 ) : ";
cin >> daynumber;
switch (daynumber)
{
case 1:
cout<< " sunday "<<endl;
break;
case 2:
cout<< " monday "<<endl;
break;
case 3:
cout<< " tuseday "<<endl;
break;
case 4:
cout<< " wednesday "<<endl;
break;
case 5:
cout<< " thursday "<<endl;
break;
case 6:
cout<< " friday "<<endl;
break;
case 7:
cout<< " saturday "<<endl;
break;
default:
cout<<" not defined/true "<<endl;
}
return 0;
}
❤1
Q36: Write a C++ program that determines if a person is eligible to vote based on their age. Consider the legal voting age as 18.
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<" enter your age : ";
cin>>age;
if ( age >= 18)
{
cout<< " you are eligible to vote "<<endl;
}
else
{
cout<< " you are not eligible to vote "<<endl;
}
return 0;
}
❤1
Q37: Write C++ program takes an input of a person first name and uses a switch statement to output the corresponding person's age (consider three persons)
#include <iostream>
using namespace std;
int main()
{
char firstName;
cout << "Enter a person's first initial (A/B/C): ";
cin >> firstName;
switch (firstName)
{
case 'A':
case 'a':
cout << "The age of person with first initial A is: 25" << endl;
break;
case 'B':
case 'b':
cout << "The age of person with first initial B is: 30" << endl;
break;
case 'C':
case 'c':
cout << "The age of person with first initial C is: 35" << endl;
break;
default:
cout << "Unknown person" << endl;
}
return 0;
}
Q38: Implement a C++ program that converts an amount in one currency to another. Ask the user for the input currency, amount, and desired currency.
#include <iostream>
using namespace std;
int main()
{
float currency, currency1, ratio;
cout<<" enter the your currency : ";
cin>>currency1;
cout<<" enter the different between the currencies : ";
cin>>ratio;
currency = currency1 / ratio;
cout<<currency<<endl;
return 0;
}
👎1
Q39: Write C++ Program which computes area of circle. The program takes radius of the circle as an input, calculates the area of the circle and outputs it on the screen.
(Area =𝜋𝑟²).
(Area =𝜋𝑟²).
#include <iostream>
using namespace std;
int main()
{
float r, area;
cout<<" enter the radius : ";
cin>>r;
area = r * r * 3.14;
cout<<" the area is : "<<area<<endl;
return 0;
}
👍1
Q40: Write C++ code to computes area of a shape using switch case.
The program takes shape as an input and identifies the shape using a switch case. The statements then take required values as input, computes the area and prints it.
(assume two shapes only, square and rectangle)
The program takes shape as an input and identifies the shape using a switch case. The statements then take required values as input, computes the area and prints it.
(assume two shapes only, square and rectangle)
#include <iostream>
using namespace std;
int main()
{
char shape;
float side, length, width, area;
cout << "Enter the shape (S for square, R for rectangle): ";
cin >> shape;
switch (shape)
{
case 'S':
case 's':
cout << "Enter the side length of the square: ";
cin >> side;
area = side * side;
break;
case 'R':
case 'r':
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
area = length * width;
break;
default:
cout << "Invalid shape" << endl;
}
cout << "The area of the shape is: " << area << endl;
return 0;
}
❤1👍1
Q41: Implement a C++ code to decide the car type depending on the number of seats.
Print an appropriate massage to indicate a car type.
(Saloon has four seats, SUV has seven seats and Bus has more than seven seats).
Print an appropriate massage to indicate a car type.
(Saloon has four seats, SUV has seven seats and Bus has more than seven seats).
#include <iostream>
using namespace std;
int main() {
int numSeats;
cout << "Enter the number of seats in the car: ";
cin >> numSeats;
if (numSeats == 4)
{
cout << "This is a Saloon car." << endl;
}
else if (numSeats == 7) {
cout << "This is an SUV." << endl;
}
else if (numSeats > 7 && numSeats < 100 )
{
cout << "This is a Bus." << endl;
}
else
{
cout << "Invalid number of seats." << endl;
}
return 0;
}
❤1
Q42: Consider two integer numbers A=7 and B=14.
Write C++ program to implement the following assignment operator.
1- Left shift operator. (shift by 2).
2- Modulus operator.
Write C++ program to implement the following assignment operator.
1- Left shift operator. (shift by 2).
2- Modulus operator.
#include <iostream>
using namespace std;
int main()
{
int A = 7, B = 14;
A = A << 2;
B = B << 2;
B = A % B;
cout << "After left shifting A by 2: " << A << endl;
cout << "After left shifting B by 2: " << B << endl;
cout << "After using modulus operator on B: " << B << endl;
return 0;
}
❤1
Q43: Consider two integer numbers C=8 and D=4.
Write C++ program to implement the following assignment operator.
1- Right shift operator. (shift by 1)
2- Bitwise AND assignment operator.
Write C++ program to implement the following assignment operator.
1- Right shift operator. (shift by 1)
2- Bitwise AND assignment operator.
#include <iostream>
using namespace std;
int main()
{
int C = 8, D = 4;
C = C >> 1;
D = D >> 1;
C &= D;
cout << "After right shifting C by 1: " << C << endl;
cout << "After right shifting D by 1: " << D << endl;
cout << "After using bitwise AND operato on C,D : " << C << endl;
return 0;
}
❤1
Q44: Consider two integer numbers N=11 and M=9.
Write C++ program to implement the following assignment operator.
1- Divide AND assignment operator.
2- Bitwise inclusive OR and assignment operator.
Write C++ program to implement the following assignment operator.
1- Divide AND assignment operator.
2- Bitwise inclusive OR and assignment operator.
#include <iostream>
using namespace std;
int main()
{
int N = 11, M = 9;
N /= M;
N |= M;
cout << "After using Divide AND assignment operator : " << N << endl;
cout << "After using Bitwise inclusive OR and assignment operator : " << N << endl;
return 0;
}
Q45: Consider two integer numbers X=1 and Y=0.
C++ program to implement the following logical operator.
1- Logical AND operator.
2- Logical NOT Operator.
بهذا تكتب متغيرات بغير اسم او تشيل bool وتكتب int
بالاثنين صح
C++ program to implement the following logical operator.
1- Logical AND operator.
2- Logical NOT Operator.
#include <iostream>
using namespace std;
int main()
{
int X = 1, Y = 0;
cout << "After using Logical AND operator X,Y: " << (X&&Y) << endl;
cout << "After using Logical NOT Operator on X : " << !X << endl;
cout << "After using Logical NOT Operator on Y : " << !Y << endl;
return 0;
}
بهذا تكتب متغيرات بغير اسم او تشيل bool وتكتب int
بالاثنين صح
❤1
Q46: Write C++ code to determine the size of various data type on a computer.
(use character and Boolean only).
(use character and Boolean only).
#include <iostream>
using namespace std;
int main()
{
char character;
bool boolean;
cout << "Size of char: " << sizeof(character) << " bytes" << endl;
cout << "Size of bool: " << sizeof(boolean) << " bytes" << endl;
return 0;
}
❤1
Q47: Write C++ code to determine the size of various data type on a computer.
(use Floating point and integer only).
(use Floating point and integer only).
#include <iostream>
using namespace std;
int main()
{
int integer;
float floatingPoint;
cout << "Size of int: " << sizeof(integer) << " bytes" << endl;
cout << "Size of float: " << sizeof(floatingPoint) << " bytes" << endl;
return 0;
}
❤1
Q48: Write C++ code to determine the size of various data type on a computer.
(use character and double only).
(use character and double only).
#include <iostream>
using namespace std;
int main()
{
char character;
double doubleonly;
cout << "Size of int: " << sizeof(character) << " bytes" << endl;
cout << "Size of float: " << sizeof(doubleonly) << " bytes" << endl;
return 0;
}
Q49: Consider two integer numbers U=15 and V=8.
C++ program to implement the following operator description.
1- Checks if the values of two operands are equal or not.
2- Checks if the value of left operand is greater than or equal to the value of right operand.
C++ program to implement the following operator description.
1- Checks if the values of two operands are equal or not.
2- Checks if the value of left operand is greater than or equal to the value of right operand.
#include <iostream>
using namespace std;
int main()
{
int U = 15, V = 8;
bool areEqual = (U == V);
bool isGreaterOrEqual = ( U >= V);
cout << "Are the values of U and V equal? " << areEqual << endl;
cout << "Is U greater than or equal to V? " << isGreaterOrEqual << endl;
return 0;
}
❤1