Programming Exercise

Conditional Structure Programming Exercise P-7

Programming Exercise Looping Structure P-1

Q.13 Write a program that inputs an integer value from the keyboard and finds out if the value is odd or even.

```#include<iostream.h>
#include<conio.h>
main()
{
int x;
clrscr();
cout<<"Enter an integer value ? ";
cin>>x;
(x%2==0)? cout<<" Entered integer value is even": cout<<"Entered integer value is odd";
getch();
}```

Q.14 Write a program using conditional operator to determine whether the year entered through keyboard is leap year or not.

```#include<iostream.h>
#include<conio.h>
main()
{
int year;
clrscr();
cout<<"Enter a year ? ";
cin>>year;
(year%4 == 0)? cout<<"Leap year": cout<<"Not a leap year";
getch();
}```

Q.15 Write a program that inputs five values and finds out the greatest value using nested ‘if’ structure.

```#include<iostream.h>
#include<conio.h>
main()
{
float n1, n2, n3, n4, n5, largest;
clrscr();
cout<<"Enter first value ? ";
cin>>n1;
cout<<"Enter second value ? ";
cin>>n2;
cout<<"Enter third value ? ";
cin>>n3;
cout<<"Enter fourth value ? ";
cin>>n4;
cout<<"Enter fifth value ? ";
cin>>n5;

if (n1>n2)
if (n1>n3)
if(n1>n4)
if(n1>n5)
largest = n1;
else
largest = n5;
else
if(n4>n5)
largest = n4;
else
largest = n5;
else
if(n3>n4)
if(n3>n5)
largest = n3;
else
largest = n5;
else
if(n4>n5)
largest = n4;
else
largest = n5;
else

if (n2>n3)
if(n2>n4)
if(n2>n5)
largest = n2;
else
largest = n5;
else
if(n4>n5)
largest = n4;
else
largest = n5;
else
if(n3>n4)
if(n3>n5)
largest = n3;
else
largest = n5;
else
if(n4>n5)
largest = n4;
else
largest = n5;

cout<<"Greatest value is : "<<largest;
getch();
}```

Q.16 Write a program that inputs temperature and displays a message according to the following data:// Temperature Message// Greater than 35 Hot day// Between 25 and 35 Pleasant day// Less than 25 Cool day

```#include<iostream.h>
#include<conio.h>
main()
{
int temp;
clrscr();
cout<<"Enter temperature ? ";
cin>>temp;
if(temp >35)
cout<<"Hot day";
else if(temp >= 25)
cout<<"Pleasant day";
else
cout<<"Cool day";
getch();

}```

Q.18 Write a program that converts ASCII number to character or character to ASCII number. The program should display the menu for conversion options. Interaction with the program should be as follows:// 1- Convert ASCII to Character// 2- Convert Character to ASCII// Enter your option ? 1// Enter a number ? 65// The corresponding character is : A

```#include<iostream.h>
#include<conio.h>
main()
{
int n, op;
char ch;
clrscr();
cout<<"1- Convert ASCII to Character"<<endl;
cout<<"2- Convert Character to ASCII"<<endl;
cin>>op;
if(op == 1)
{
cout<<"Enter ASCII number ? ";
cin>>n;
ch = n;
cout<<"Corresponding character of " <<n<< " is :"<<ch;
}
else if(op == 2)
{
cout<<"Enter a character ? ";
cin>>ch;
n = ch;
cout<<"ASCII value of "<<ch<<" is : "<<n;
}
getch();
}```

Q.19 Write a program that inputs any number of month of the year and displays the name and number of days of the entered month. For example, if 4 is entered for the number of month of the year, program should display “April has 30 days”.

```#include<iostream.h>
#include<conio.h>
main()
{
int no_month;
clrscr();
cout<<"Enter number of month of the year [1-12]?"<<endl;
cin>>no_month;
if(no_month == 1)
cout<<"January has 31 days ";
else if(no_month == 2)
cout<<"February has 28 days ";
else if(no_month == 3)
cout<<"March has 31 days ";
else if(no_month == 4)
cout<<"April has 30 days ";
else if(no_month == 5)
cout<<"May has 31 days ";
else if(no_month == 6)
cout<<"June has 30 days ";
else if(no_month == 7)
cout<<"July has 31 days ";
else if(no_month == 8)
cout<<"August has 31 days ";
else if(no_month == 9)
cout<<"September has 30 days ";
else if(no_month == 10)
cout<<"October has 31 days ";
else if(no_month == 11)
cout<<"November has 30 days ";
else if(no_month == 1)
cout<<"December has 31 days ";
getch();
}```

Q.20 Write a program that calculates income tax for three categories of income groups. The tax is computed as follows://i. If the income is less than Rs. 25,000, the tax is 5% of the income.//ii. If the income is equal to Rs. 25,000, the tax is Rs. 1250.//iii. If the income is greater than Rs. 25,000, the tax is Rs. 1250 plus 15% of the income exceeding Rs. 25,000.

```#include<iostream.h>
#include<conio.h>
main()
{
int income;
double tax;
clrscr();
cout<<"Enter income ? "<<endl;
cin>>income;
if(income < 25000)
tax = income * 5.0/100.0;
else if(income == 25000)
tax = 1250;
else if(income > 25000)
tax = 1250 + (income-25000)*15.0/100.0;
cout<<"Income Tax = "<<tax;
getch();
}```

Q.21 Write a program that calculates the commission of a salesman of a departmental store according to the following://i. If sale is less than Rs. 50, there is no commission.//ii. If sale is greater than or equal to Rs. 50 and less than or equal to Rs.500, the commission is 10% of the sale.//iii. If sale is greater than Rs.500, the commission is Rs 50+15% of the sale above Rs. 500

```#include<iostream.h>
#include<conio.h>
main()
{
int sale;
double commission;
clrscr();
cout<<"Enter sale ? ";
cin>>sale;
if(sale < 50)
commission = 0;
else if(sale >=50 && sale <=500)
commission = sale*10.0/100.0;
else if(sale > 500)
commission = 50 + (sale-500)*15.0/100.0;
cout<<"commission of a salesman = "<<commission;
getch();
}```

Q.22 Write a program that inputs marks obtained by a student in a subject. The total marks are 100. The program finds out grade of the student using the if-else nested structure. The grade is found out as follows:// If marks are equal to or greater than 90, grade is “A+”.// If marks are equal to or greater than 70, and less than 90, grade is “A”.// If marks are equal to or greater than 50, and less than 70, grade is “B”.// If marks are less than 50, grade is “F”.

```#include<iostream.h>
#include<conio.h>
main()
{
int marks;
clrscr();
cout<<"Enter marks of a student ? ";
cin>>marks;
if(marks >=90)
else if(marks >=70 && marks < 90)
else if(marks >=50 && marks < 70)
else if(marks < 50)
getch();
}```

Q.23 Write a program that inputs the radius of a circle. It calculates area of circle if user enters 1 as choice. It calculates circumference of the circle if the user enters 2 as choice.

```#include<iostream.h>
#include<conio.h>
main()
{
int op;
clrscr();
cout<<"Enter radius of circle ? ";

cout<<"1-Area of circle "<<endl;
cout<<"2-Circumference of circle "<<endl;
cin>>op;
switch(op)
{

case 1:
cout<<"Area of circle :"<<area;
break;
case 2:
cout<<"Circumference of circle :"<<circum;
break;
}
getch();
}```

Q.24 Write a program that inputs an integer number and displays the corresponding name of the month using switch statement. For example, if a user enters an integer value 3 then “March” should be displayed and so on. If value is not between 1 and 12 then program should display a message “Invalid Month”.

```#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
cout<<"Enter number of month ? ";
cin>>n;
switch(n)
{

case 1:
cout<<"January";
break;
case 2:
cout<<"February";
break;
case 3:
cout<<"March";
break;
case 4:
cout<<"April";
break;
case 5:
cout<<"May";
break;
case 6:
cout<<"June";
break;
case 7:
cout<<"July";
break;
case 8:
cout<<"August";
break;
case 9:
cout<<"September";
break;
case 10:
cout<<"October";
break;
case 11:
cout<<"November";
break;
case 12:
cout<<"December";
break;
default:
cout<<"Invalid Month";
}
getch();
}```

Q.25 Write a program that gets an integer value and tests whether the given number is odd or even using switch statement.

```#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
cout<<"Enter any number ? ";
cin>>n;
switch(n%2)
{

case 0:
cout<<n<<" is even number";
break;
case 1:
cout<<n<<" is odd number";
break;
}
getch();
}```

Q.26 Write a program that inputs a single digit (0 to 9) from the keyboard and displays the input value in words. For example, if the input is 9, then print Nine. Use the switch statement.

```#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
cout<<"Enter any digit between 0 to 9 ? ";
cin>>n;
switch(n)
{

case 1:
cout<<"One";
break;
case 2:
cout<<"Two";
break;
case 3:
cout<<"Three";
break;
case 4:
cout<<"Four";
break;
case 5:
cout<<"Five";
break;
case 6:
cout<<"Six";
break;
case 7:
cout<<"Seven";
break;
case 8:
cout<<"Eight";
break;
case 9:
cout<<"Nine";
break;

default:
cout<<"Invalid input";
}
getch();
}```