0% found this document useful (0 votes)
23 views

Program To Add Two Integers: C++ Program Ex No: 22

This document contains 4 C++ programs: 1) A program to add two integers that gets user input, calculates the sum, and displays the output. 2) A program that swaps the values of two variables using a temporary variable. 3) A program that checks if a user-input number is even or odd using the modulo operator. 4) A program that calculates the sum of natural numbers from 1 to a user-input number using a for loop.

Uploaded by

Tharun Aadi R
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)
23 views

Program To Add Two Integers: C++ Program Ex No: 22

This document contains 4 C++ programs: 1) A program to add two integers that gets user input, calculates the sum, and displays the output. 2) A program that swaps the values of two variables using a temporary variable. 3) A program that checks if a user-input number is even or odd using the modulo operator. 4) A program that calculates the sum of natural numbers from 1 to a user-input number using a for loop.

Uploaded by

Tharun Aadi R
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
You are on page 1/ 3

C++ Program

Ex No: 22

Program to Add Two Integers

#include <iostream>
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;

cout << "Enter two integers: ";


cin >> firstNumber >> secondNumber;

// sum of two numbers in stored in variable sumOfTwoNumbers


sumOfTwoNumbers = firstNumber + secondNumber;

// Prints sum
cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers;

return 0;
}

Ex No: 23

Swap Numbers (Using Temporary Variable)

#include <iostream>
int main()
{
int a = 5, b = 10, temp;

cout << "Before swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

temp = a;
a = b;
b = temp;

cout << "\nAfter swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

return 0;
}

Ex No: 24

Check Whether Number is Even or Odd using if


else

#include <iostream>
int main()
{
int n;

cout << "Enter an integer: ";


cin >> n;

if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";

return 0;
}

Ex No: 25

Sum of Natural Numbers using loop

#include <iostream>
int main()
{
int n, sum = 0;

cout << "Enter a positive integer: ";


cin >> n;

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


sum += i;
}

cout << "Sum = " << sum;


return 0;
}

You might also like