0% found this document useful (0 votes)
3 views34 pages

Computer Programming Questions

Questions and solutions on C++ topics

Uploaded by

Eternal Playz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views34 pages

Computer Programming Questions

Questions and solutions on C++ topics

Uploaded by

Eternal Playz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1. Program to find the number is even or odd.

#include<iostream>
using namespace std;
int main(){
int a;
cout<<"enter the Number \n";
cin>>a;
if(a%2==0){
cout<<"given number is even"<<endl;
}
else
{
cout<<"Given number is odd";
}
return 0;
}
[Link] to find out greatest of three given numbers.
#include<iostream>
using namespace std;
int main(){
int a,b,c;
cout<<"give the three number whose you want to comparison";
cin>>a>>b>>c;
if(a>=b && a>=c){
cout<<a<<" is the greatest number"<<endl;
}
else if(b>=a & b>=c){
cout<<b<<" is the greatest number "<<endl;
}
else{
cout<<c<<" is the greatest number "<<endl;
}
return 0;
}
Q3. Program to find whether the entered year is leap year or not.
#include<iostream>
using namespace std;
bool LeapYear(int year){
if(year%4==0 && ( year%100!=0 || year%400 ==0)){
return true;}
else{
return false;}
}
int main(){
int year;
cout<<"enter the year";
cin>>year;

if (LeapYear(year))
{ cout<<"year is a leap year"<<endl;
}
else{
cout<<"it is not a leap year";
}
return 0;
}
Q4. Program to find whether the given number is prime or composite.
#include<iostream>
using namespace std;
bool PrimeOrNot(int number){
int rem;
for(int i=2;i<number;i++){
rem=number%i;
if(rem==0){
return false;
break;
}}
return true;}
int main(){
int num;
cin>>num;
if(num==0 || num==1){
cout<<"given number is neither a prime nor composite"<<endl;
}
else if(PrimeOrNot(num)){
cout<<"given number '"<<num<< "'is a prime number "<<endl;
}
else{
cout<<"its a composite number"<<endl;}
return 0;}
Q5. Program to print the sum of first n natural numbers
#include<iostream>
using namespace std;
int Sum(int n){
int result=0;
for(int i=1;i<=n;i++){
result=result + i;
}
return result;
}
int main(){
int n;
cout<<"enter the n where you want to addition"<<endl;
cin>>n;
int ans=Sum(n);
cout<<"the sum of first "<<n<< "number is = "<< ans;
return 0; }
Q6. Program to find sum of digits of n digit number.

#include<iostream>
using namespace std;
int main(){
int n,dig;
int sum=0;
cin>>n;
while(n!=0){
dig=n%10;
sum=sum+dig;
n=n/10;
}
cout<<"sum of digits is "<<sum;
return 0;
}
Q7. Program to find reverse of a number
#include<iostream>
#include<math.h>
using namespace std;
int ReverseOfNumber(int n){
int rev=0;
while(n!=0){
rev = rev*10 + n%10 ;
n=n/10;
}
return rev;
}
int main(){
int num;
cin>>num;
cout<<ReverseOfNumber(num)<<" is the reverse of a number.";
return 0;
}
Q8. Program to print the day of week according to day number entered by the user
#include <iostream>
using namespace std;
int main() {
string daysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"};
int dayNumber;
cout << "Enter a day number (1-7): ";
cin >> dayNumber;
if (dayNumber >= 1 && dayNumber <= 7) {
cout << "The day of the week is: " << daysOfWeek[dayNumber - 1] << endl;
} else {
cout << "Invalid day number entered." << endl;
}
return 0;
}
[Link] to print Pascal’s triangle
#include <iostream>
using namespace std;
void printPascalTriangle(int n) {
int coef = 1;
for (int i = 0; i < n; i++) {
for (int space = 1; space <= n - i; space++) {
cout << " ";
}
for (int j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
cout << coef << " ";
}
cout << endl;
}
}
int main() {
int numRows;
cout << "Enter the number of rows for Pascal's Triangle: ";
cin >> numRows;
printPascalTriangle(numRows);
return 0;}
[Link] to print Floyd’s triangle
#include <iostream>
using namespace std;
void printFloydTriangle(int rows) {
int number = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
cout << number << " ";
number++;
}
cout << endl;
}
}
int main() {
int numberOfRows;
cout << "Enter the number of rows for Floyd's Triangle: ";
cin >> numberOfRows;
printFloydTriangle(numberOfRows);
return 0;
}
Q11. Program to print all the prime numbers between a given range
#include <iostream>
using namespace std;
bool isPrime(int num) {
if (num <= 1) {
return false; }
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false; } }
return true;}
void printPrimeNumbers(int start, int end) {
cout << "Prime numbers between " << start << " and " << end << ":\n";
for (int num = start; num <= end; num++) {
if (isPrime(num)) {
cout << num << " ";}}
cout << endl;}
int main() {
int start_range, end_range;
cout << "Enter the start of the range: ";
cin >> start_range;
cout << "Enter the end of the range: ";
cin >> end_range;
printPrimeNumbers(start_range, end_range);
return 0; }
Q12. Program to print all the 3 digit Armstrong numbers
#include <iostream>
#include <cmath>
using namespace std;
bool isArmstrong(int num) {
int originalNum, remainder, result = 0, n = 0;
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;}
return (result == num);}
int main() {
cout << "All 3-digit Armstrong numbers:" << endl;
for (int i = 100; i < 1000; ++i) {
if (isArmstrong(i)) {
cout << i << endl; }}
return 0;}
Q13. Program to print factorial of a number using function
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1); }}
int main() {
int num;
cout << "Enter a number to calculate its factorial: ";
cin >> num;
if (num < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
int result = factorial(num);
cout << "Factorial of " << num << " is: " << result << endl;}
return 0;
}
Q14. Program to print factors of a given number using a function
#include <iostream>
using namespace std;
void printFactors(int num) {
cout << "Factors of " << num << " are: ";
for (int i = 1; i <= num; ++i) {
if (num % i == 0) {
cout << i << " , ";
}
}
cout << endl;
}
int main() {
int number;
cout << "Enter a number to find its factors: ";
cin >> number;
if (number <= 0) {
cout << "Factors are not defined for non-positive numbers." << endl;
} else {
printFactors(number);
}
return 0;
}
Q15. Program to swap values of two variables using call by value or call by reference
#include <iostream>
using namespace std;
void swapValues(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x, y;
cout << "Enter the value of x: ";
cin >> x;
cout << "Enter the value of y: ";
cin >> y;
cout << "Before swapping - x: " << x << ", y: " << y << endl;
swapValues(x, y);
cout << "After swapping - x: " << x << ", y: " << y << endl;
return 0;
}
Q16. Program to print sum of first n natural numbers using recursion.
#include <iostream>
using namespace std;
int sumOfNaturals(int n) {
if (n == 0) {
return 0;
} else {
return n + sumOfNaturals(n - 1);
}
}
int main() {
int n;
cout << "Enter a positive integer (n): ";
cin >> n;
if (n < 0) {
cout << "Please enter a positive integer." << endl;
} else {
int sum = sumOfNaturals(n);
cout << "Sum of first " << n << " natural numbers is: " << sum << endl;
}
return 0;
}
Q17. Program to print Fibonacci series upto n terms using recursion
#include <iostream>
using namespace std;
void fibonacci(int n, int a = 0, int b = 1, int count = 0) {
if (count < n) {
cout << a << " ";
fibonacci(n, b, a + b, count + 1);
}
}
int main() {
int terms;
cout << "Enter the number of terms for Fibonacci series: ";
cin >> terms;
if (terms <= 0) {
cout << "Number of terms should be positive." << endl;
} else {
cout << "Fibonacci series up to " << terms << " terms is: ";
fibonacci(terms);
cout << endl;
}
return 0;
}
Q18. Program to covert decimal to binary/octall using recursion
#include <iostream>
using namespace std;
void decToBinary(int n) {
if (n > 1) {
decToBinary(n / 2);}
cout << n % 2;}
void decToOctal(int n) {
if (n > 7) {
decToOctal(n / 8);}
cout << n % 8;}
int main() {
int decimalNumber;
cout << "Enter a decimal number: ";
cin >> decimalNumber;
if (decimalNumber < 0) {
cout << "Please enter a non-negative integer." << endl;
} else {
cout << "Binary representation: ";
decToBinary(decimalNumber);
cout << " \n Octal representation: ";
decToOctal(decimalNumber);
cout << endl;}
return 0;}
Q19. Program to find the largest and the smallest element in the array
#include <iostream>
using namespace std;
int main() {
int arr[] = {4, 9, 2, 7, 5, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int smallest = arr[0], largest = arr[0];
for (int i = 1; i < n; ++i) {
if (arr[i] < smallest) {
smallest = arr[i];
}
if (arr[i] > largest) {
largest = arr[i];
}
}
cout << "Smallest element: " << smallest << endl;
cout << "Largest element: " << largest << endl;
return 0;
}
Q20. Program to multiply two matrices
#include <iostream>
using namespace std;
int main() {
int matrix1[100][100], matrix2[100][100], result[100][100];
int rows1, cols1, rows2, cols2;
cout << "Enter the number of rows and columns for first matrix: ";
cin >> rows1 >> cols1;
cout << "Enter elements of first matrix:" << endl;
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols1; ++j) {
cin >> matrix1[i][j];
}
}
cout << "Enter the number of rows and columns for second matrix: ";
cin >> rows2 >> cols2;
cout << "Enter elements of second matrix:" << endl;
for (int i = 0; i < rows2; ++i) {
for (int j = 0; j < cols2; ++j) {
cin >> matrix2[i][j];
}
}
if (cols1 != rows2) {
cout << "Matrix multiplication not possible." << endl;
return 0;
}
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
result[i][j] = 0;
}
}
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
for (int k = 0; k < cols1; ++k) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
cout << "Resultant Matrix after multiplication:" << endl;
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}
Q21. Program to find reverse of a string
#include <iostream>
#include <string>
using namespace std;
string reverseString(const string &str) {
string reversedStr = "";
int len = [Link]();
for (int i = len - 1; i >= 0; --i) {
reversedStr += str[i];
}
return reversedStr;
}
int main() {
string inputString;
cout << "Enter a string: ";
getline(cin, inputString);
string reversed = reverseString(inputString);
cout << "Original string: " << inputString << endl;
cout << "Reversed string: " << reversed << endl;
return 0;
}
Q22. Program to find entered string is palindrome or not
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(const string &str) {
int left = 0;
int right = [Link]() - 1;
while (left < right) {
if (str[left] != str[right]) {
return false;}
++left;
--right;}
return true;}
int main() {
string inputString;
cout << "Enter a string: ";
getline(cin, inputString);
if (isPalindrome(inputString)) {
cout << "The entered string is a palindrome." << endl;
} else {
cout << "The entered string is not a palindrome." << endl;}
return 0;}
Q23. Program to count the number of words in multiword string
#include <iostream>
#include <sstream>
using namespace std;
int countWords(const string &str) {
stringstream ss(str);
string word;
int wordCount = 0;
while (ss >> word) {
wordCount++;
}
return wordCount;
}
int main() {
string inputString;
cout << "Enter a multi-word string: ";
getline(cin, inputString);
int numOfWords = countWords(inputString);
cout << "Number of words in the string: " << numOfWords << endl;
return 0;
}
Q24. Program to access elements of an array using pointers
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int *ptr = arr;
cout << "Elements of the array using pointers:" << endl;
for (int i = 0; i < size; ++i) {
cout << "Element " << i << ": " << *ptr << endl;
ptr++;
}
return 0;
}
Q25. Program to print transpose of a matrix, by passing matrix to a function
#include <iostream>
using namespace std;
void transposeMatrix(int matrix[][100], int rows, int cols) {
int transpose[cols][rows];
for (int i = 0; i < cols; ++i) {
for (int j = 0; j < rows; ++j) {
transpose[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 << transpose[i][j] << " ";}
cout << endl;}}
int main() {
int rows, cols;
int matrix[100][100];
cout << "Enter the number of rows and columns of the matrix: ";
cin >> rows >> cols;
cout << "Enter elements of the matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> matrix[i][j];}}
transposeMatrix(matrix, rows, cols);
return 0;}
Q26. Program to add two distances (inch -feet system) using structures
#include <iostream>
using namespace std;
struct Distance {
int feet;
float inches;
};
Distance addDistances(const Distance &d1, const Distance &d2) {
Distance result;
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
if ([Link] >= 12.0) {
[Link] -= 12.0;
[Link]++;
}
return result;
}
int main() {
Distance distance1, distance2, sum;
cout << "Enter first distance:" << endl;
cout << "Feet: ";
cin >> [Link];
cout << "Inches: ";
cin >> [Link];
cout << "\nEnter second distance:" << endl;
cout << "Feet: ";
cin >> [Link];
cout << "Inches: ";
cin >> [Link];
sum = addDistances(distance1, distance2);
cout << "\nSum of distances: " << [Link] << " feet and " << [Link] << " inches" <<
endl;
return 0;}
Q27. Program to add two complex numbers by passing structure to a function
#include <iostream>
using namespace std;
struct Complex {
float real;
float imag;
};
Complex addComplex(const Complex& num1, const Complex& num2) {
Complex result;
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
return result;
}
int main() {
Complex num1, num2, sum;
cout << "Enter real and imaginary parts of first complex number: ";
cin >> [Link] >> [Link];
cout << "Enter real and imaginary parts of second complex number: ";
cin >> [Link] >> [Link];
sum = addComplex(num1, num2);
cout << "Sum = " << [Link] << " + " << [Link] << "i" << endl;
return 0;
}
Q28. Program for creating a class BankAccount with attributes like account_number,
balance and customer_name, and methods like deposit, withdraw, and check_balance.
Use paramterized Constructor to initialize balance as Rs 5000.
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
int account_number;
float balance;
string customer_name;
public:
BankAccount(int acc_number, string cust_name) : account_number(acc_number),
customer_name(cust_name) {
balance = 5000.0f;
}
void deposit(float amount) {
if (amount > 0) {
balance += amount;
cout << "Deposit of Rs " << amount << " successful\n";
} else {
cout << "Invalid amount for deposit\n";
}
}
void withdraw(float amount) {
if (balance >= amount && amount > 0) {
balance -= amount;
cout << "Withdrawal of Rs " << amount << " successful\n";
} else {
cout << "Insufficient balance or invalid amount for withdrawal\n";
}
}
void check_balance() {
cout << "Account Number: " << account_number << endl;
cout << "Customer Name: " << customer_name << endl;
cout << "Current Balance: Rs " << balance << endl;
}
};
int main() {
BankAccount myAccount(12345, "John Doe");
myAccount.check_balance();
[Link](2000);
[Link](1000);
myAccount.check_balance();

return 0;
}
Q29. Program for creating class named “Shape”, having overloaded function “area()”
to calculate area of Shape Objects: rectangle, square, circle
#include <iostream>
#define PI 3.14159
using namespace std;
class Shape {
public:
float area(float length, float width) {
return length * width;}
float area(float side) {
return side * side;}
float area(float radius, char shape = 'c') {
if (shape == 'c')
return PI * radius * radius;
else
return 0;}};
int main() {
Shape shapeObj;
float rectArea = [Link](5.0, 8.0);
cout << "Area of Rectangle: " << rectArea << endl;
float squareArea = [Link](6.0);
cout << "Area of Square: " << squareArea << endl;
float circleArea = [Link](4.0, 'c');
cout << "Area of Circle: " << circleArea << endl;
return 0;
}
Q30. Program for creating class named “Complex” which allows addition of two
complex numbers using the overloaded operator + (binary addition). Overloaded
constructor should be used to assign/initialize the real and imaginary parts of complex
number
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imaginary;
public:
Complex(float r = 0.0, float i = 0.0) : real(r), imaginary(i) {}
Complex operator+(const Complex& other) {
Complex result;
[Link] = real + [Link];
[Link] = imaginary + [Link];
return result;}
void display() {
cout << "Complex number: " << real << " + " << imaginary << "i" << endl;}};
int main() {
Complex complex1(3.0, 4.0);
Complex complex2(1.5, 2.5);
Complex result = complex1 + complex2;
cout << "Result of addition: ";
[Link]();
return 0;}
Q31. Program to illustrate function over riding
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "Inside Base class" << endl;
}
};
class Derived : public Base {
public:
void display() override {
cout << "Inside Derived class" << endl;
}
};
int main() {
Base baseObj;
Derived derivedObj;
[Link]();
[Link]();
Base* ptr = &derivedObj;
ptr->display();
return 0;
}

You might also like