You are on page 1of 9

1) C++ Program to Find Largest of Two Numbers(Entered by User)

In this program we are using if..else statement to find out the largest of two numbers entered by
user.

Example: Program to find the largest of two entered numbers

#include <iostream>
using namespace std;

int main()
{
int num1, num2;
cout<<"Enter first number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;
if(num1>num2)
{
cout<<"First number "<<num1<<" is the largest";
}
else
{
cout<<"Second number "<<num2<<" is the largest";
}
return 0;
}
Output:

1
2) C++ Program to Check whether an input number is Prime or not

A number which is only divisible by itself and 1 is known as prime number, for example: 5 is a
prime number because it is only divisible by itself and 1.

#include <iostream>
using namespace std;
int main(){
int num;
bool flag = true;
cout<<"Enter any number(should be positive integer): ";
cin>>num;

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


if(num % i == 0) {
flag = false;
break;
}
}
if (flag==true)
cout<<num<<" is a prime number";
else
cout<<num<<" is not a prime number";
return 0;
}
Output:

Enter any number(should be positive integer): 149


149 is a prime number

2
3) C++ Program to find the sum of n natural numbers

Numbers 1, 2, 3, …., n are known as natural numbers. This program takes the value of n and
finds the sum of first n natural numbers.

For example, If user enters 5 as the value of n then the sum of first n(n=5) natural numbers
would be:
1+2+3+4+5 = 15

Example: Program to calculate the sum of n natural numbers

To understand this program you should have the knowledge of C++ while loop.

#include
using namespace std;
int main(){
int n, sum=0;
cout<<"Enter the value of n(should be a positive integer): ";
//Storing the input integer value into the variable n
cin>>n;

/* If user enters value of n as negative then display an


* error message to user that the value of n is invalid
*/
if(n<=0){
cout<<"Invalid value of n";
}
else{
// We are using while loop to calculate the sum
int i=1;
while(i<=n){
sum=sum+i;
i++;
}
cout<<"Sum of first n natural numbers is: "<<sum;
}

return 0;
}
Output:

Enter the value of n(should be a positive integer): 5


Sum of first n natural numbers is: 15

3
4) C++ Program to Swap Two Numbers using Third variable

Here we will see a program to swap two numbers using a temporary third variable. The
steps are as follows:

1. User is asked to enter the value of first and second number. The input values are stored in
first(num1) and second(num2) variable.
2. Assigning the value of first variable to the third variable.
3. Assigning the value of second variable to the first variable.
4. Assigning the value of third variable to the second variable.

Example: Program to swap two numbers

#include <iostream>
using namespace std;

int main()
{
int num1, num2, temp;
cout<<"Enter 1st Number: ";
cin>>num1;
cout<<"Enter 2nd Number: ";
cin>>num2;

//displaying numbers before swapping


cout<<"Before Swapping: First Number: "<<num1<<" Second Number: "<<num2;

//swapping
temp=num1;
num1=num2;
num2=temp;

//displaying numbers after swapping


cout<<"\nAfter Swapping: First Number: "<<num1<<" Second Number: "<<num2;
return 0;
}

4
Output:

5
5) C++ Program to Find largest element in an array

This program finds the largest element in an array. User is asked to enter the value of n(number
of elements in array) then program asks the user to enter the elements of array. The program
then finds the largest element and displays it.

#include <iostream>
using namespace std;
int main(){
//n is the number of elements in the array
int n, largest;
int num[50];
cout<<"Enter number of elements you want to enter: ";
cin>>n;

/* Loop runs from o to n, in such a way that first


* element entered by user is stored in num[0], second
* in num[1] and so on.
*/
for(int i = 0; i < n; i++) {
cout<<"Enter Element "<<(i+1)<< ": ";
cin>>num[i];
}
// Storing first array element in "largest" variable
largest = num[0];
for(int i = 1;i < n; i++) {
/* We are comparing largest variable with every element
* of array. If there is an element which is greater than
* largest variable value then we are copying that variable
* to largest, this way we have the largest element copied
* to the variable named "largest" at the end of the loop
*
*/
if(largest < num[i])
largest = num[i];
}
cout<<"Largest element in array is: "<<largest;
return 0;
}
Output:

Enter number of elements you want to enter: 5


Enter Element 1: 19
Enter Element 2: 21
Enter Element 3: 3
Enter Element 4: 89
Enter Element 5: 13
Largest element in array is: 89

6
6) C++ Program to Convert Lowercase to Uppercase

Here we will see two programs for lowercase to uppercase conversion. First program
converts lowercase character to uppercase and the second program converts lowercase string
to uppercase string.

ASCII value of lowercase char a to z ranges from 97 to 122


ASCII value of uppercase char A to Z ranges from 65 to 92
For conversion we are subtracting 32 from the ASCII value of input char.

#include <iostream>
using namespace std;

int main()
{
char ch;
cout<<"Enter a character in lowercase: ";
cin>>ch;
ch=ch-32;
cout<<"Entered character in uppercase: "<<ch;
return 0;
}
Output:

7
7) Program to convert Lowercase String to Uppercase String

In this program user is asked to enter a string and then the program converts that input string
into an uppercase string.
Logic used here: Looping through all the characters of the input string and checking whether the
character lies in the ASCII range 97 to 122 (all the lowercase chars lies in this range). If the
character is found to be in this range then the program converts that character into an
uppercase char by subtracting 32 from the ASCII value.

#include <iostream>
#include <string>
using namespace std;

int main()
{
char s[30];
int i;
//display a message to user to enter the string
cout<<"Enter the String in lowercase: ";
//storing the string into the char array
cin>>s;

/* running the loop from 0 to the length of the string


* to convert each individual char of string to uppercase
* by subtracting 32 from the ASCII value of each char
*/
for(i=0;i<=strlen(s);i++) {
/* Here we are performing a check so that only lowercase
* characters gets converted into uppercase.
* ASCII value of a to z(lowercase chars) ranges from 97 to 122
*/
if(s[i]>=97 && s[i]<=122)
{
s[i]=s[i]-32;
}
}
cout<<"The entered string in uppercase: "<<s;
return 0;
}

8
Output:

You might also like