0% found this document useful (0 votes)
12 views14 pages

C++ Programming Basics and Examples

The document provides 15 code examples in C++ to demonstrate basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions and arrays. Each code example is accompanied by its input, output and brief explanation.

Uploaded by

srikrishna30233
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)
12 views14 pages

C++ Programming Basics and Examples

The document provides 15 code examples in C++ to demonstrate basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions and arrays. Each code example is accompanied by its input, output and brief explanation.

Uploaded by

srikrishna30233
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

Week 1 – OODP

RA2211047010007

DARANJAIE K

[Link] AI

A Section

1. Write a program in C++ to print the sum of two numbers.

Code:
#include <iostream>
Using namespace std;
int main() {
double num1, num2;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

cout << "Sum: " << num1 + num2 << endl;

return 0;
}

Input:
Enter two numbers: 4 5

Output:
Sum: 9

2. Write a C++ program to display the operation of pre and post increment and
decrement.
Code:
#include <iostream>
using namespace std;
int main() {
int num = 5;

cout << "Initial value of num: " << num << endl;
cout << "Pre-increment: " << ++num << endl;
cout << "Pre-decrement: " << --num << endl;

// Reset num
num = 5;
cout << "Post-increment: " << num++ << endl;
cout << "Post-decrement: " << num-- << endl;

cout << "Value after all operations: " << num << endl;

return 0;
}

Output:
Initial value of num: 5
Pre-increment: 6
Pre-decrement: 5
Post-increment: 5
Post-decrement: 6
Value after all operations: 5
3. Write a C++ program to add two numbers and accept them from the
Keyboard

Code:
#include <iostream>
using namespace std;
int main()
{
double num1, num2;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

double sum = num1 + num2;

cout << "Sum: " << sum << endl;

return 0;
}

Input:
Enter two numbers: 4 5

Output:
9

4. Write a C++ program that swaps two numbers.


Code:
#include <iostream>
using namespace std;
int main()
{
double num1, num2;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

num1 = num1 + num2;


num2 = num1 - num2;
num1 = num1 - num2;

cout << "After swapping: First number = " << num1 << ", Second number = " << num2 <<
endl;

return 0;
}

Input:
Enter two numbers: 4 5

Output:
After swapping: First number = 5, Second number = 4

5. Write a program in C++ to convert temperature in Kelvin to Celsius.


Code:
#include <iostream>
using namespace std;

int main() {
double kelvin;
cin >> kelvin;
cout << kelvin - 273.15 << " C" << endl;
return 0;
}

Input:
54

Output:
-219.15 C

6. C++ Program to Check Whether Number is Even or Odd

Code:
#include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter a Number: ";

cin >> number;

cout << (number % 2 == 0 ? "Even" : "Odd") << endl;


return 0;

Input:

Enter a Number: 5

Output:

Odd

7. Write a program in C++ to check whether a number is positive, negative or

zero.

Code:

#include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter a Number: ";

cin >> number;

if (number > 0)

cout << "Positive" << endl;

else if (number < 0)

cout << "Negative" << endl;

else

cout << "Zero" << endl;

return 0;
}

Input:

Enter a Number: 5

Output:

Positive

8. C++ Program to Check Leap Year

Code:

#include <iostream>

using namespace std;

int main() {

int year;

cout << "Enter a year: ";

cin >> year;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

cout << year << " is a leap year." << endl;

else

cout << year << " is not a leap year." << endl;

return 0;

Input:

Enter a year: 2040


Output:

2040 is a leap year

9. C++ Program to find Largest of 3 numbers

Code:

#include <iostream>

using namespace std;

int main() {

double num1, num2, num3;

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

if (num1 >= num2 && num1 >= num3) {

cout << "Largest number: " << num1 << endl;

} else if (num2 >= num1 && num2 >= num3) {

cout << "Largest number: " << num2 << endl;

} else {

cout << "Largest number: " << num3 << endl;

return 0;

Input:

Enter three numbers: 4 7 9

Output:
Largest number: 9
10. Find out the sum of the digits of a number?

Code:

#include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

int sum = 0;

while (number > 0) {

sum += number % 10;

number /= 10;

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

return 0;

Input:

Enter a number: 24

Output:

Sum of digits: 6

11. Can you check whether a number is prime or not?


Code:

#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;

int main() {

int number;

cin >> number;

cout << (isPrime(number) ? "Prime" : "Not Prime") << endl;

return 0;

Input:

Output:

Prime

12. Find the factorial of a number using C++.

Code:

#include <iostream>

using namespace std;


int factorial(int n) {

return (n <= 1) ? 1 : n * factorial(n - 1);

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

cout << "Factorial: " << factorial(number) << endl;

return 0;

Input:

Enter a number: 5

Output:

Factorial: 120

13. C++ Program to Calculate Sum of Natural Numbers

Code:

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Numbers upto: ";

cin >> n;
int sum = n * (n + 1) / 2;

cout << "Sum of natural numbers up to " << n << " is: " << sum << endl;

return 0;

Input:

Numbers upto: 8

Output:

Sum of natural numbers up to 8 is: 36

14. C++ program to check whether a number is Armstrong number or not.

Code:

#include <iostream>

#include <cmath>

using namespace std;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

int originalNumber = number;

int sum = 0;

int numDigits = log10(number) + 1;


while (number > 0) {

int digit = number % 10;

sum += pow(digit, numDigits);

number /= 10;

if (sum == originalNumber)

cout << "Armstrong number." << endl;

else

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

return 0;

Input:

Enter a number: 5

Output:

Armstrong number

15. C++ Program to Find Largest Element of an Array .

Code:

#include <iostream>

using namespace std;

int main() {

int size;

cout << "Enter size of array: ";

cin >> size;


int arr[size];

cout << "Enter " << size << " Numbers: ";

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

cin >> arr[i];

int largest = arr[0];

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

if (arr[i] > largest) {

largest = arr[i];

cout <<"Largest: " << largest << endl;

return 0;

Input:

Enter size of array: 4

Enter 4 Numbers: 2 4 5 6

Output:

Largest: 6

You might also like