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

C++ Exercise 2

Uploaded by

Fahmi Tofik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

C++ Exercise 2

Uploaded by

Fahmi Tofik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE

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

3. Write C++ program to print sum of digits enter by user


#include <iostream>
using namespace std;
int main()
{
int num, total;

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

6. Write C++ program to reverse a number using while loop


#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num, rev = 0;
//Reading a number from user
cout<<"Enter any number:";
cin>>num;
//finding reverse number using while loop
while (num > 0) {
rev = rev * 10;
rev = rev + num % 10;
num = num / 10;
}
cout<<"Reversed number is: "<<rev;
return 0;
}
7. Write C++ program to check whether a number is Armstrong number or not
An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.
Example: 407 = 43 + 03 + 73 = 64 + 0 + 343 = 407

#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

cout<<num<<" is an armstrong number."<<endl;;


}
else{
cout<<num<<" is not an armstrong number."<<endl;;
}
return 0;
}
8. Write a C++ program to check a enter number is Prime number or not using while loop
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num, i, f;
//Reading a number from user
cout<<"Enter any number:";
cin>>num;
f = 0;
i = 2;
while(i <= num/2)
{
if(num%i == 0)
{
f=1;
break;
}
i++;
}
if(f == 0)
cout<<num<<" is a Prime Number"<<endl;
else
cout<<num<<" is Not a Prime Number"<<endl;
return 0;

9. }Write C++ program to find HCF of two numbers


#include <iostream>
using namespace std;
int main()
{
int i, num1, num2, min, HCF=1;

Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg  0912995263/0936747488 10
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE

//Read two numbers from user


cout<<"Enter any two numbers:";
cin>>num1;
cin>>num2;
// Find min number between two numbers
min = (num1<num2) ? num1 : num2;
for(i=1; i<=min; i++)
{
if(num1%i==0 && num2%i==0)
{
HCF = i;
}
}
cout<<"HCF of "<<num1<< " and "<< num2<< " is: " <<HCF;
return 0;
}

10. Write C++ program to find Armstrong numbers between 1 to n


11. Write a C++ program to check whether a number is palindrome or not
12. Write C++ program to find LCM of two numbers

Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg  0912995263/0936747488 11
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE

1. Write a program in C to check whether a number is a palindrome or not.


#include <stdio.h>
void main(){
intnum,r,sum=0,t;
printf("Input a number: ");
scanf("%d",&num);
for(t=num;num!=0;num=num/10){
r=num % 10;
sum=sum*10+r;
}
if(t==sum)
printf("%d is a palindrome number.\n",t);
else
printf("%d is not a palindrome number.\n",t);
}
2. Program to check whether the number is Armstrong or not using loop in C++:
#include <iostream>
using namespace std;
int main ( )
{
int n, r, sum = 0, m;
cout<<"Enter n: ";
cin>> n;
cout<<endl;
m = n;
while(n > 0)
{
r = n % 10;
n = n / 10;
sum = sum + r * r * r;
}
cout<<"Number is ";
if(sum == m)
cout<<"Armstrong";
else
cout<<"not Armstrong";
return 0;
}
// a program that prints out the *and @using for loop
#include <iostream>
using namespace std;
Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg  0912995263/0936747488 12
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);

for (i = 0, j = n - 1; i<= j; i++, j--)


{

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

1. Write C++ program to create simple calculator using switch Statement


#include <iostream>
using namespace std;
int main(){
char mathoperator;
double firstNumber, secondNumber;
//Reading mathoperator from user
cout<<"Enter an operator (+, -, *,): ";
cin>>mathoperator;
//Reading operands from user
cout<<"Enter two operands: ";
cin>>firstNumber;
cin>>secondNumber;
switch(mathoperator)
{
cout << firstNumber << " + " << secondNumber << " = " << firstNumber+secondNumber;
break;
case '-':
cout << firstNumber << " - " << secondNumber << " = " << firstNumber-secondNumber;
break;
case '*':
cout << firstNumber << " * " << secondNumber << " = " << firstNumber*secondNumber;
break;
case '/':
cout << firstNumber << " / " << secondNumber << " = " << firstNumber/secondNumber;
break;
// operator doesn't match any case constant (+, -, *, /)
default:
cout<<"Error! please enter correct operator";
}
return 0;
}
2. Write C++ program to print gender Male Female program
#include <iostream>
using namespace std;
int main(){
char gender;
//Reading gender from user
cout<<"Enter gender (M/m or F/f): ";
cin>>gender;

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

3. Write C++ Program to Add Two Matrices


#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout<<"Enter number of rows (between 1 and 100): ";
cin>>r;
cout<<"Enter number of columns (between 1 and 100): ";
cin>>c;
cout<<"\nEnter elements of First matrix:\n";
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
cout<<"Enter element a"<<i+1<<j+1<<": ";
cin>>a[i][j];
}
cout<<"Enter elements of Second matrix:\n";
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
cout<<"Enter element a"<<i+1<<j+1<<": ";
cin>>b[i][j];
}

Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg  0912995263/0936747488 16
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE

// Adding Two matrices


for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
// Displaying the result
cout<<"\nSum of two matrix is: \n\n";
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout<<"\t"<<sum[i][j];
if(j==c-1)
{
cout<<"\n\n";
}
}
return 0;
}

4. Write C++ program to find maximum and minimum element in array


#include <iostream>
#define MAX_SIZE 100 //Maximum size of the array
using namespace std;
int main()
{
int arr[100];
int i, max, min, size;
// Reading array sizr & elements in the array
cout<<"Enter size of the array: ";
cin>>size;
cout<<"Enter elements in the array: ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
/* Supposes the first element as maximum and minimum */
max = arr[0];
min = arr[0];
/*

Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg  0912995263/0936747488 17
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE

* Finds maximum and minimum in all array elements.


*/
for(i=1; i<size; i++)
{
// Finding max number
//if cuurent element of array is greater than max
if(arr[i]>max)
{
max = arr[i];
}
// Finding min number
// If current element of array is smaller than min
if(arr[i]<min)
{
min = arr[i];
}
}
//Finding the maximum and minimum element
cout<<"Maximum element is: "<<max<<endl;
cout<<"Minimum element is: "<<min<<endl;
return 0;
}

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

//Finding area of circle whose radius is given


double getArea(double radius)
{
return (M_PI * radius * radius); // PI = 3.14
}
#4. Write C++ program to find LCM of two numbers using recursion
#include <iostream>
using namespace std;
// Function declaration
int Findlcm(int x, int y);
int main()
{
int num1, num2, LCM;
// Inputting two numbers from user
cout<<"Enter any 2 numbers to find LCM: "<<endl;
cin>>num1;
cin>>num2;
if(num1 > num2)
LCM = Findlcm(num2, num1);
else
LCM = Findlcm(num1, num2);
cout<<"LCM of "<<num1 << " and "<< num2 <<" is: "<<LCM;
return 0;
}
int Findlcm(int x, int y)
{
static int multiple = 0;
// Increments multiple by adding max value to it
multiple += y;
if((multiple % x == 0) && (multiple % y == 0))
{
return multiple;
}
else
{
return Findlcm(x, y);
}
}

Address:- Ayertena Next to Sami Café @ 3rd Floor of Zewditu Bldg  0912995263/0936747488 22
EAGLE LANGUAGE, COMPUTER & ENGINEERING SOFTWARE TRAINING INSTITUTE

#5. Write C++ program to find factorial of a number using recursion

#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

You might also like