C++ Exercise 2
C++ Exercise 2
1. C++ Program to check whether an integer entered by the user is odd or even
#include<iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
// True if remainder is 0
if( number%2 == 0 )
cout<< number << " is an even number";
else
cout<< number << " is an odd number";
return 0;
}
2. C++ program to check uppercase or lowercase alphabets.
#include<iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter any character: ";
cin >> ch;
if(ch >= 'a' && ch <= 'z')
{
cout << ch<< " is lowercase alphabet.: ";
}
else if(ch >= 'A' && ch <= 'Z')
{
cout << ch<< " is uppercase alphabet.: ";
}
else
{
cout << ch<< " is not an alphabet.: ";
}
return 0
}
3. C++ program to check entered character vowel or consonant.
#include<iostream>
using namespace std;
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 1
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
int main()
{
char ch;
cout << "Enter any character: ";
cin >> ch;
// Condition for vowel checking
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
cout << ch << " is a vowel";
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
cout << ch << " is a consonant";
}
return 0;
}
4. C++ program to check whether a character is alphabet, digit or special character.
#include<iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter any character";
cin >> ch;
// Alphabet checking condition
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
cout << ch << " is an Alphabet";
}
else if(ch >= '0' && ch <= '9')
{
cout << ch << " is a Digit";
}
else
{
cout << ch << " is a Special Character";
}
return 0;
}
5. C++ program to find the eligibility of admission for an engineering course based on the criteria.
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 2
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
#include<iostream>
using namespace std;
int main()
{
int p, c, m, t, mp;
cout << "Eligibility Criteria for an engineering:" <<endl;
cout << "Marks in Mathematics >= 65" <<endl;
cout << "Marks in Physics >= 55" <<endl;
cout << "Marks in Chemestry >= 50" <<endl;
cout << "Total in all three subject >= 180" <<endl;
cout << "or Total in Maths and Physics >= 140" <<endl;
cout << "-------------------------------------" <<endl;
cout << "Input the marks obtained in Physics :" <<endl;
cin >> p;
cout << "Input the marks obtained in Chemistry :" <<endl;
cin >> c;
cout << "Input the marks obtained in Mathematics :" <<endl;
cin >> m;
cout << "Total marks of Mathematics, Physics and Chemistry :" << m + p + c << endl;
cout << "Total marks of Maths and Physics :" << m + p;
if (m>=65)
if(p>=55)
if(c>=50)
if((m + p + c) >= 180 || (m + p) >= 140)
cout << "The candidate is eligible for admission." <<endl;
else
cout << "The candidate is not eligible." <<endl;
else
cout << "The candidate is not eligible."<<endl;
else
cout << "The candidate is not eligible."<<endl;
else
cout << "The candidate is not eligible."<<endl;
}
6. C++ program to calculate the total marks, percentage and division of student.
7. C++ Program to find the largest number among three number.
8. C++ program to check number is positive, negative or zero.
9. C++ program to print day name of week.
10. C++ program to accept two integers and check whether they are equal or not.
11. C++ program to determine a candidate’s age is eligible for casting the vote or not.
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 3
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 4
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 5
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
Chapter 3: LOOP
1. Write C++ program to print alphabets from a to z
#include <iostream>
using namespace std;
int main()
{
int character=0;
char ch='A';
do
{
character=int(ch);
cout<<ch<<" ";
character++;
ch=char(character);
}
while(ch<='Z');
}
2. Write C++ program to print multiplication table of a given number
#include <iostream>
using namespace std;
int main()
{
int num=0, i;
//Reading number
cout<<"Enter number to print table: "<<endl;
cin>>num;
for(i=1; i<=10; i++)
{
//Printing table of number entered by user
cout<<num<<" x "<<i << " = "<<num*i<<endl;
}
return 0;
}
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 6
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
//Reading number
cout<<"Enter any number: "<<endl;
cin>>num;
//Adding sum of digit in total variable
for(total = 0; num > 0; num = num/10)
total = total + (num%10);
//Printing sum of digit
cout<<"Sum of digits: "<< total;
return 0;
}
4. Write C++ program to find sum of even numbers between 1 to n
#include <iostream>
using namespace std;
int main()
{
int i, num, sum=0;
//Reading number
cout<<"Enter any number: "<<endl;
cin>>num;
for(i=2; i<=num; i+=2)
{
//Adding current even number to sum variable
sum += i;
}
cout<<"Sum of all even number between 1 to " << num << ": "<<sum;
return 0;
}
5. Write C++ program to find the sum of first and last digit of any number
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num, sum=0, firstDigit, lastDigit;
//Reading a number from user
cout<<"Enter any number:";
cin>>num;
lastDigit = num % 10;
firstDigit = num;
while(num >= 10)
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 7
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
{
num = num / 10;
}
firstDigit = num;
//Finding sum of first and last digit
sum = firstDigit + lastDigit;
cout<<"Sum of first and last digit: "<<sum;
return 0;
}
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 8
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num, sum = 0, i, r;
//Reading a number from user
cout<<"Enter any number to check its Armstrong or not:";
cin>>num;
//Finding armstrong number or not
for(i = num; i>0; i=i/10)
{
r = i%10;
sum = sum + r * r * r;
}
if ( num == sum ){
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 9
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 10
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 11
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
int main ()
{
int i,j,k;
for(i=0; i<=6; i++)
{
cout<<endl;
for(k=6; k>i; k--)
cout<< “ ” ;
for(j=0;j<=k+i;j++)
Cout<<”*”;
}
for(i=0;i<=3;i++)
{
cout<<endl;
for(k=1; k>=0;k--)
cout<<” “;
for(j=2;j<=10;j++)
cout<<”@”;
}
cout<<”\n\n”;
}
3. String Palindrome Program in C using for loop
#include <stdio.h>
#include <string.h>
int main()
{
char str[30];
int n = 0, flag = 0, i = 0, j = 0;
printf("Enter String:");
scanf("%s", &str);
n = strlen(str);
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 13
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
if (str[i] != str[j])
{
flag = 1;
break;
}
}
if (flag == 1)
printf("No, it is not a palindrome");
else
printf("Yes, it is a palindrome");
return 0;
}
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 14
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 15
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
switch(gender)
{
case 'M':
case 'm':
cout<<"Male";
break;
case 'F':
case 'f':
cout<<"Female";
break;
default:
cout<<"Unspecified Gender"<<endl;
}
return 0;
}
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 16
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 17
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 18
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
//Program to find out the number which computer has supposed randomly
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 19
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
5. Functions
#1 Write C++ program to find cube of a number using function
#include <iostream>
using namespace std;
// Function declaration
double cube(double num);
int main()
{
int num;
double c;
//Inputting number from user
cout<<"Enter any number: "<<endl;
cin>>num;
c = cube(num);
cout<<"Cube of " <<num << " is "<<c;
return 0;
}
//Function to find cube of any number
double cube(double num)
{
return (num * num * num);
}
#2. Write C++ program to check even or odd using functions
#include <iostream>
using namespace std;
int isEven(int num)
{
return !(num & 1);
}
int main(){
int num;
// Inputting number from user
cout<<"Enter any number: ";
cin>>num;
// If isEven() function return 0 then the number is even
if(isEven(num))
{
//printing even number
cout<<"The entered number is even.";
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 20
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
}
else
{
//printing odd number
cout<<"The entered number is odd.";
}
return 0;
}
#3.Write C++ program to find diameter, circumference and area of circle using function
#include <iostream>
#include <math.h>
using namespace std;
//All Function declaration
double getDiameter(double radius);
double getCircumference(double radius);
double getArea(double radius);
int main()
{
float radius, diameter, circle, area;
// Inputting radius of circle from user
cout<<"Enter radius of circle: ";
cin>>radius;
diameter = getDiameter(radius); // Calling getDiameter function
circle = getCircumference(radius); // Calling getCircumference function
area = getArea(radius); // Calling getArea function
cout<<"Diameter of the circle: "<<diameter <<" units"<<endl;
cout<<"Circumference of the circle: "<< circle<<" units"<<endl;
cout<<"Area of the circle:"<< area<<" sq. units"<<endl;
return 0;
}
// Calculating diameter of circle whose radius is given
double getDiameter(double radius)
{
return (2 * radius);
}
//Calculating circumference of circle whose radius is given
double getCircumference(double radius)
{
return (2 * M_PI * radius); // PI = 3.14
}
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 21
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 22
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE
#include <iostream>
using namespace std;
// Function declaration
unsigned long long Findfact(int num);
int main()
{
int num;
unsigned long long factorial;
// Inputting an integer from user
cout<<"Enter any number: ";
cin>>num;
factorial = Findfact(num);
cout<<"Factorial of " <<num<< " is "<<factorial;
return 0;
}
unsigned long long Findfact(int num)
{
// Base condition
if(num == 0)
return 1;
else
return num * Findfact(num - 1);
}
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg 0912995263/0936747488 23