Programming Exercise

Programming Exercise Looping Structure P-1

Conditional Structure Programming Exercise P-6

Q.1: Write a program that inputs marks of three subjects of a student. If average marks are more than 50, it displays messages “Your average marks are more than 50” and “Congratulation to you” in two lines using two output statements. Use simple ‘if’ statement in the program.

```#include<iostream.h>
#include<conio.h>

main()
{

int num;
clrscr();
cout<<"Enter a number ? ";
cin>>num;
if(num > 0)
cout<<"The number is positive";
if(num < 0)
cout<<"The number is negative";
if(num == 0)
cout<<"The number is zero";
getch();
}```

Q.2: Write a program that inputs marks of three subjects of a student. If average marks are more than 50, it displays messages “Your average marks are more than 50” and “Congratulation to you” in two lines using two output statements. Use simple ‘if’ statement in the program.

```#include<iostream.h>
#include<conio.h>

main()
{

int sub1, sub2, sub3;
float avg;
clrscr();
cout<<"Enter marks of first subject ? ";
cin>>sub1;
cout<<"Enter marks of second subject ? ";
cin>>sub2;
cout<<"Enter marks of third subject ? ";
cin>>sub3;
avg = (sub1+sub2+sub3)/3.0;
if(avg > 50)
{
cout<<"Your average marks are more than 50"<<endl;
cout<<"Congratulation to you"<<endl;
}
cout<<"End of the program";
getch();
}```

Q.3: Write a program that inputs two numbers and finds out whether both numbers are equal.

```#include<iostream.h>
#include<conio.h>

main()
{

int x, y;
clrscr();
cout<<"Enter first number ? ";
cin>>x;
cout<<"Enter second number ? ";
cin>>y;
if(x == y)
cout<<"Both numbers are equal"<<endl;
cout<<"End of the program";
getch();
}```

Q.4: Write a program that inputs a character and determines whether it is a lowercase letter or not using a simple ‘if’ statements.

```#include<iostream.h>
#include<conio.h>

main()
{

char ch;
clrscr();
cout<<"Enter a character ? ";
cin>>ch;
if(ch>='a' && ch<='z')
cout<<"Entered character is lowercase letter"<<endl;
cout<<"End of the program";
getch();
}```

Q.5: Write a program that inputs two values and finds out the greater value using if-else structure and displays result on screen.

```#include<iostream.h>
#include<conio.h>

main()
{

int a, b;
clrscr();
cout<<"Enter first value ? ";
cin>>a;
cout<<"Enter second value ? ";
cin>>b;
if (a>b)
cout<<"First value is greater"<<endl;
else
cout<<"Second value is greater"<<endl;
cout<<"End of the program";
getch();
}```

Q.6: Write a program that inputs a number and displays whether it is an even number or odd number using logical operator “!” and if-else statement.

```#include<iostream.h>
#include<conio.h>

main()
{

int num;
clrscr();
cout<<"Enter any number ? ";
cin>>num;
if (!(num%2==0))
cout<<"Odd number"<<endl;
else
cout<<"Even number"<<endl;
getch();
}```

Q.7: Write a program that inputs a single letter in a char variable. It displays “You are male” if ‘m’ is input and displays “You are female” if ‘f’ is input. It should display “Invalid input” if other letter is input.

```#include<iostream.h>
#include<conio.h>

main()
{

char ch;
clrscr();
cout<<"Enter a single character ? ";
cin>>ch;
if (!(ch=='m'|| ch=='f'))
cout<<"Invalid input"<<endl;
if (ch=='m')
cout<<"You are male"<<endl;
if (ch=='f')
cout<<"You are female"<<endl;
getch();
}```

Q.8: Write a program that inputs a number and calculates the square root of the number if the number is greater than 0. Otherwise it displays a message “Number is negative or zero”.

```#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
double res;
int num;
clrscr();
cout<<"Enter a number ? ";
cin>>num;
if(num > 0)
{
res = sqrt(num);
cout<<"Square root of "<<num<<" is = "<<res;
}
else
cout<<"Number is negative or zero ";
getch();
}```

Q.9: Write a program that converts “Small Alphabet” into “Capital Alphabet” and vice versa.

```#include<iostream.h>
#include<conio.h>
main()
{
char ch, res;
clrscr();
cout<<"Enter any alphabet ? ";
cin>>ch;
if(ch>='a' && ch<='z')

res = ch-32;

else
res = ch+32;
cout<<"Converted alphabet is : "<<res;
getch();
}```

Q.10: Write a program that displays/prints the largest and smallest value entered by a user ten times in a single variable during the program execution.

```#include<iostream.h>
#include<conio.h>
main()
{
int smallest, largest, count, num;
clrscr();
count = 1;
cout<<"Enter a number ? ";
cin>>num;
smallest = largest = num;
abc:
cout<<"Enter a number ? ";
cin>>num;
if(smallest > num)
smallest = num;
if (largest < num)
largest = num;
count++;
if(count < 10) goto abc;
// res = ch+32;
cout<<"Largest entered value is : "<<largest<<endl;
cout<<"Smallest entered value is : "<<smallest<<endl;
getch();
}```

Q.11: A year is a leap year if it is divisible by 4, except that any year divisible by 100 is a leap year only if it is divisible by 400. Write a program that inputs a year such as 1996, 1800, and 2010, and displays “Leap year” if it is a leap year, otherwise displays “Not a leap year”.

```#include<iostream.h>
#include<conio.h>
main()
{

int year;
clrscr();
cout<<"Enter a year ? ";
cin>>year;
if(year % 4 == 0 || year % 100 == 0 && year % 400 == 0)
cout<<"Leap year";
else
cout<<"Not a leap year";
getch();
}```

Q.12: Write a program that inputs an integer value and finds out whether the integer is divisible by 3 or not using conditional operator.

```#include<iostream.h>
#include<conio.h>
main()
{
int x;
clrscr();
cout<<"Enter a number ? ";
cin>>x;
(x%3==0)? cout<<x<< " is divisible by 3": cout<<x<< " is not divisible by 3";
getch();
}```