Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
63 views18 pages

C++ Programs for Basic Operations

The document contains a series of C++ programs that demonstrate various programming concepts, including arithmetic operations, number swapping, control structures, and data structures. Each program is accompanied by a short explanation of its functionality. The programs cover a wide range of topics, from basic input/output to more complex operations like matrix manipulation and string handling.

Uploaded by

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

C++ Programs for Basic Operations

The document contains a series of C++ programs that demonstrate various programming concepts, including arithmetic operations, number swapping, control structures, and data structures. Each program is accompanied by a short explanation of its functionality. The programs cover a wide range of topics, from basic input/output to more complex operations like matrix manipulation and string handling.

Uploaded by

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

UNIT I

Program 1: Arithmetic Operations on Two Integers

#include <iostream>

using namespace std;

int main() {

int a, b;

cout << "Enter two integers: ";

cin >> a >> b;

cout << "Sum: " << a + b << endl;

cout << "Diff: " << a - b << endl;

cout << "Prod: " << a * b << endl;

cout << "Quot: " << a / b << endl;

return 0;

Short Explanation: Takes two integers and performs +, −, ×, ÷ operations.

Program 2: Swap Two Numbers Using Third Variable

#include <iostream>

using namespace std

int main() {

int x, y, t;

cout << "Enter two numbers: ";

cin >> x >> y;

t = x;

x = y;

y = t;

cout << "After swap: x = " << x << ", y = " << y << endl;

return 0;

Short Explanation: Uses temp t to swap values of x and y.


Program 3A: Swap Without Third Variable (Using Math)

#include <iostream>

using namespace std;

int main() {

int x, y;

cout << "Enter two numbers: ";

cin >> x >> y;

x = x + y;

y = x - y;

x = x - y;

cout << "After swap: x = " << x << ", y = " << y << endl;

return 0;

Short Explanation: Uses addition-subtraction to swap.

Program 3B: Swap Without Third Variable (Using Bit Manipulation)

#include <iostream>

using namespace std;

int main() {

int x, y;

cout << "Enter two numbers: ";

cin >> x >> y;

x = x ^ y;

y = x ^ y;

x = x ^ y;

cout << "After swap: x = " << x << ", y = " << y << endl;

return 0;

Short Explanation: Uses XOR bitwise operations to swap.


UNIT II

Program 1: Find Odd or Even

#include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

if (number % 2 == 0)

cout << "Even number" << endl;

else

cout << "Odd number" << endl;

return 0;

Program 2: Greatest Among Three Numbers

#include <iostream>

using namespace std;

int main() {

int a, b, c;

cout << "Enter three numbers: ";

cin >> a >> b >> c;

if (a >= b && a >= c)

cout << "Greatest number is: " << a << endl;

else if (b >= a && b >= c)

cout << "Greatest number is: " << b << endl;

else

cout << "Greatest number is: " << c << endl;

return 0;

}
Program 3: Average of Three Subjects

#include <iostream>

using namespace std;

int main() {

float s1, s2, s3;

cout << "Enter marks of 3 subjects: ";

cin >> s1 >> s2 >> s3;

float avg = (s1 + s2 + s3) / 3;

cout << "Average marks = " << avg << endl;

return 0;

Program 4: Print Numbers from 1 to 10

#include <iostream>

using namespace std;

int main() {

for (int i = 1; i <= 10; i++) {

cout << i << " ";

cout << endl;

return 0;

}
Program 5: Print Numbers from 10 to 1

#include <iostream>

using namespace std;

int main() {

for (int i = 10; i >= 1; i--) {

cout << i << " ";

cout << endl;

return 0;

Program 6: Print Odd and Even Numbers from 1 to 20

#include <iostream>

using namespace std;

int main() {

cout << "Even numbers: ";

for (int i = 1; i <= 20; i++) {

if (i % 2 == 0)

cout << i << " ";

cout << "\nOdd numbers: ";

for (int i = 1; i <= 20; i++) {

if (i % 2 != 0)

cout << i << " ";

cout << endl;

return 0;

}
Program 7: Check if a Number is Prime

#include <iostream>

using namespace std;

int main() {

int num, count = 0;

cout << "Enter a number: ";

cin >> num;

if (num <= 1) {

cout << "Not prime" << endl;

return 0;

for (int i = 2; i < num; i++) {

if (num % i == 0)

count++;

if (count == 0)

cout << "Prime number" << endl;

else

cout << "Not prime" << endl;

return 0;

}
Program 8: Check Armstrong Number (3-digit)

#include <iostream>

#include <cmath>

using namespace std;

int main() {

int num, sum = 0, temp, digit;

cout << "Enter a number: ";

cin >> num;

temp = num;

while (temp > 0) {

digit = temp % 10;

sum += digit * digit * digit;

temp /= 10;

if (sum == num)

cout << "Armstrong number" << endl;

else

cout << "Not an Armstrong number" << endl;

return 0;

}
Program 9: Factorial of a Number

#include <iostream>

using namespace std;

int main() {

int num;

long long fact = 1;

cout << "Enter a number: ";

cin >> num;

for (int i = 1; i <= num; i++) {

fact *= i;

cout << "Factorial = " << fact << endl;

return 0;

Program 10: Fibonacci Series (First n terms)

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter number of terms: ";

cin >> n;

int a = 0, b = 1;

cout << "Fibonacci series: " << a << " " << b << " ";
for (int i = 3; i <= n; i++) {

int c = a + b;

cout << c << " ";

a = b;

b = c;

cout << endl;

return 0;

Program 11: Separating Digits of a Number

#include <iostream>

using namespace std;

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

cout << "Digits: ";

while (num > 0) {

int digit = num % 10;

cout << digit << " ";

num /= 10;

cout << endl;

return 0;

}
Program 12: Sum of All Digits

#include <iostream>

using namespace std;

int main() {

int num, sum = 0;

cout << "Enter a number: ";

cin >> num;

while (num > 0) {

sum += num % 10;

num /= 10;

cout << "Sum of digits = " << sum << endl;

return 0;

Program 13: Reverse of a Number

#include <iostream>

using namespace std;

int main() {

int num, rev = 0;

cout << "Enter a number: ";

cin >> num;

while (num > 0) {

int digit = num % 10;

rev = rev * 10 + digit;

num /= 10;

cout << "Reversed number = " << rev << endl;

return 0;

}
Program 14: Check Perfect Number

#include <iostream>

using namespace std;

int main() {

int num, sum = 0;

cout << "Enter a number: ";

cin >> num;

for (int i = 1; i < num; i++) {

if (num % i == 0)

sum += i;

if (sum == num)

cout << "Perfect number" << endl;

else

cout << "Not a perfect number" << endl;

return 0;

Program 15: Check Palindrome Number

#include <iostream>

using namespace std;

int main() {

int num, temp, rev = 0;

cout << "Enter a number: ";

cin >> num;


temp = num;

while (temp > 0) {

int digit = temp % 10;

rev = rev * 10 + digit;

temp /= 10;

if (rev == num)

cout << "Palindrome number" << endl;

else

cout << "Not a palindrome number" << endl;

return 0;

Program 16: Print & Input Array Elements – Normal and Reverse

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter array size: ";

cin >> n;

int arr[n];

cout << "Enter " << n << " elements: ";

for (int i = 0; i < n; i++)

cin >> arr[i];

cout << "Array in order: ";

for (int i = 0; i < n; i++)


cout << arr[i] << " ";

cout << "\nArray in reverse: ";

for (int i = n - 1; i >= 0; i--)

cout << arr[i] << " ";

cout << endl;

return 0;

Program 17: Matrix Addition, Subtraction, Multiplication

#include <iostream>

using namespace std;

int main() {

const int n = 2;

int A[n][n] = {{1, 2}, {3, 4}};

int B[n][n] = {{5, 6}, {7, 8}};

int sum[n][n], diff[n][n], prod[n][n];

cout << "Matrix A:\n";

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++)

cout << A[i][j] << " ";

cout << endl;

cout << "Matrix B:\n";

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++)

cout << B[i][j] << " ";


cout << endl;

cout << "Addition:\n";

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

sum[i][j] = A[i][j] + B[i][j];

cout << sum[i][j] << " ";

cout << endl;

cout << "Subtraction:\n";

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

diff[i][j] = A[i][j] - B[i][j];

cout << diff[i][j] << " ";

cout << endl;

cout << "Multiplication:\n";

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

prod[i][j] = 0;

for (int k = 0; k < n; k++)

prod[i][j] += A[i][k] * B[k][j];

cout << prod[i][j] << " ";

cout << endl;

}
return 0;

Program 18: Check Palindrome String

#include <iostream>

#include <string>

using namespace std;

int main() {

string str, rev = "";

cout << "Enter a string: ";

cin >> str;

for (int i = [Link]() - 1; i >= 0; i--)

rev += str[i];

if (rev == str)

cout << "Palindrome string" << endl;

else

cout << "Not a palindrome string" << endl;

return 0;

}
Program 19: Print All Substrings of a String

#include <iostream>

#include <string>

using namespace std;

int main() {

string str;

cout << "Enter a string: ";

cin >> str;

cout << "Substrings:\n";

for (int i = 0; i < [Link](); i++) {

for (int j = 1; j <= [Link]() - i; j++) {

cout << [Link](i, j) << endl;

return 0;

Program 20: Add Two Strings (Concatenation)

#include <iostream>

#include <string>

using namespace std;

int main() {

string s1, s2;

cout << "Enter first string: ";

cin >> s1;

cout << "Enter second string: ";

cin >> s2;

string res = s1 + s2;

cout << "Concatenated string: " << res << endl;

return 0;

}
21. Create a structure named Student with members for name, roll number, marks in three
subjects. Write a program that takes input for multiple students' records and displays the average
marks for each student

#include <iostream>

using namespace std;

struct Student {

string name;

int roll;

float marks[3];

};

int main() {

int n;

cout << "Enter number of students: ";

cin >> n;

Student s[n];

for (int i = 0; i < n; i++) {

cout << "Enter name: ";

cin >> s[i].name;

cout << "Enter roll number: ";

cin >> s[i].roll;

cout << "Enter marks of 3 subjects: ";

for (int j = 0; j < 3; j++)

cin >> s[i].marks[j];

cout << "\nAverage marks:\n";

for (int i = 0; i < n; i++) {


float total = 0;

for (int j = 0; j < 3; j++)

total += s[i].marks[j];

float avg = total / 3;

cout << s[i].name << " (Roll " << s[i].roll << "): " << avg << endl;

return 0;

You might also like