CPP-Simple Programs With Output
CPP-Simple Programs With Output
In this program we are using if..else statement to find out the largest of two numbers entered by
user.
#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;
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
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;
return 0;
}
Output:
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.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, temp;
cout<<"Enter 1st Number: ";
cin>>num1;
cout<<"Enter 2nd Number: ";
cin>>num2;
//swapping
temp=num1;
num1=num2;
num2=temp;
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;
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.
#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;
8
Output: