Programming Fundamentals: Conditional Statements (Session 3)

A Task

  A task is given to 3 persons to complete it within a particular time. If the person exceeds the time limit he will be disqualified. Only those who complete it within the given time limit is qualified. Among the qualified persons, the person who completes the task first will be rewarded.
Write a program to find the person who is rewarded.

Input Format:
First input corresponds to the Time limit for the task in hours.  Second,Third and Fourth Inputs correspond to the number of hours Taken by the first , second and third persons respectively to complete the task.

Output Format:
Display the person who Completes first.
[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output 1:
Enter the Time Limit:
10
Enter the time taken by the first person:
5
Enter the time taken by the second person:
6
Enter the time taken by the third person:
4
Third person wins!!!

Sample Input and Output 2:
Enter the Time Limit:
5
Enter the time taken by the first person:
3
Enter the time taken by the second person:
6
Enter the time taken by the third person:
4
First person wins!!!

Sample Input and Output 3:
Enter the Time Limit:
4
Enter the time taken by the first person:
9
Enter the time taken by the second person:
6
Enter the time taken by the third person:
7

No person wins:-(

#include<stdio.h>
int main()
{
  int a,b,c,x;
  printf("Enter the Time Limit:\n");
  scanf("%d",&x);
  printf("Enter the time taken by the first person:\n");
  scanf("%d",&a);
  printf("Enter the time taken by the second person:\n");
  scanf("%d",&b);
  printf("Enter the time taken by the third person:\n");
  scanf("%d",&c);
  if((a<x)&(a<b)&(a<c))
    printf("First person wins!!!");
  else if((b<x)&(b<a)&(b<c))
    printf("Second person wins!!!");
  else if((c<x)&(c<a)&(c<b))
    printf("Third person wins!!!");
  else
    printf("No person wins:-(");
  return 0;
}

BASIC CALCULATOR

Write a C program to simulate a basic calculator. [+,-,*,/,%]. Use switch statement.

Input Format:
The first line of the input consists of an integer which corresponds to a. The second line of the input consists of a character which corresponds to the operator. The third line of the input consists of an integer which corresponds to b.

Output format:
Output consists of a single line [a op b]. Refer to sample output for details.

Sample Input 1:
3
+
5

Sample Output 1:
The sum is 8

Sample Input 2:
7
-
6

Sample Output 2:
The difference is 1

Sample Input 3:
4
*
3

Sample Output 3:
The product is 12

Sample Input 4:
12
/
3

Sample Output 4:
The quotient is 4

Sample Input 5:
4
%
2

Sample Output 5:
The remainder is 0

Sample Input 6:
5
^
2

Sample Output 6:
Invalid Input

#include<stdio.h>

int main()

{

  int a,b;

  char c;

  scanf("%d %c",&a,&c);

  switch(c)

  {

    case '+':scanf("%d",&b);printf("The sum is %d",a+b);break;

    case '-':scanf("%d",&b);printf("The difference is %d",a-b);break;

    case '*':scanf("%d",&b);printf("The product is %d",a*b);break;

    case '/':scanf("%d",&b);printf("The quotient is %d",a/b);break;

    case '%':scanf("%d",&b);printf("The remainder is %d",a%b);break;

    default:printf("Invalid Input");break;

  }

  return 0;

}

BETTER or NOT

One criteria for evaluating 2 different colleges is based on the student strength.

Write a C program to compare 2 colleges based on the student strength.

Input Format:
Input consists of 2 integers. The first integer corresponds to the number of students in college 1 and the second integer corresponds to the number of students in college 2.

Output Format:
Output consists of the string “College 1 is better” or “College 2 is better”.
Refer sample input and output for further formatting specifications.

Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]

Enter the number of students in college 1
1000
Enter the number of students in college 2
1200
College 2 is better

#include<stdio.h>

int main()

{

  int a,b;

  printf("Enter the number of students in college 1\n");

  scanf("%d",&a);

  printf("Enter the number of students in college 2\n");

  scanf("%d",&b);

  if(a>b)

    printf("College 1 is better");

  else

    printf("College 2 is better");

  return 0;

}

Blood bank

A team from Rotract club had planned to conduct a rally to create awarness among the coimbatore people to donate blood. They conducted the rally successfully . Many of the coimbatore people realized it and came forward to donate their blood to near by blood bank.  The eligibility criteria for donating blood is people should be above 18 and his/ her weight should be above 40. There was a huge crowd and staff in blood bank found it difficult to manage the crowd. So they decided to keep a system and ask the people to enter their age and weight in system. If a person is eligible he / she will be allowed inside.
Write a  program and  feed it to  the system  to find whether a person is eligible or not.
Input Format:
Input consists of two integers which corresponds to age and weight of a person respectively.
Output Format :
Display whether the person is eligible or not.
Sample input and output 1:
[All text in bold corresponds to input and the rest corresponds to output]
Enter your Age:
19
Enter your Weight:
50
Eligible to donate.
Sample input and output 2:
Enter your Age:
17
Enter your Weight:
50
Not Eligible to donate

#include<stdio.h>

int main()

{

  int a,w;

  printf("Enter your Age:\n");

  scanf("%d",&a);

  printf("Enter your Weight:\n");

  scanf("%d",&w);

  if((a>18)&(w>40))

    printf("Eligible to donate.");

  else

    printf("Not Eligible to donate.");

  return 0;

}

CALCULATE GRADE

Write a program that accepts the marks in 3 subjects of a student , calculates the average mark of the student and prints the student's grade. If the average mark is greater than or equal to 90, then the grade is 'A'. If the average mark is 80 and between 80 and 90, then the grade is 'B'. If the average mark is  70 and between 70 and 80, then the grade is 'C'. If the average mark is  60 and between 60 and 70, then the grade is 'D'. If the average mark is 50 and between 50 and 60, then the grade is 'E'. If the average mark is less than 50, then the grade is 'F'.

Input Format:
Input consists of 3 lines. Each line consists of an integer.

Output Format:
Output consists of a single line. Refer sample output for the format.

Sample Input 1 :
45
45
45

Sample Output 1 :
The grade is F

Sample Input 2:
91
95
100

Sample Output 2:
The grade is A

#include<stdio.h>

int main()

{

  int a,b,c;

  scanf("%d %d %d",&a,&b,&c);

  a=(a+b+c)/3;

  printf("The grade is ");

  if(a>=90)

    printf("A");

  else if(a>=80)

    printf("B");

  else if(a>=70)

    printf("C");

  else if(a>=60)

    printf("D");

  else if(a>=50)

    printf("E");

  else

    printf("F");

  return 0;

}

CHARACTER  UPPER OR LOWER

Write a  program that accepts a character as input and checks whether it is an uppercase letter or lowercase letter or neither.

Input Format:
Input consists of a single character.

Output Format:
Output consists of a single line. Refer sample output for the format.

Sample Input 1 :
c

Sample Output 1 :
c is lowercase letter

Sample Input 2:
A

Sample Output 2:
A is uppercase letter

Sample Input 3:
5

Sample Output 2:
5 is neither an uppercase or lowercase letter

#include<stdio.h>

int main()

{

  char c;

  scanf("%c",&c);

  if((c>=65)&(c<=90)) 

    printf("%c is uppercase letter",c);

  else if((c>=97)&(c<=122))

    printf("%c is lowercase letter",c);

  else

    printf("%c is neither an uppercase or lowercase letter",c);

  return 0;

}

DIVISIBLE BY 2 OR 3

Write a  program that prints “yes” if the given integer is divisible by 2 or 3 and “no” otherwise.

Input Format:
Input consists of a single integer.

Output Format:
Output consists of a single line. Refer sample output for the format.

Sample Input 1 :
21

Sample Output 1 :
yes

Sample Input 2:
19

Sample Output 2:
no

#include<stdio.h>

int main()

{

  int x=0;

  scanf("%d",&x);

  if((x%2==0)|(x%3==0))

    printf("yes");

  else

    printf("no");

  return 0; 

}

DIVISIBLE BY 7 AND 3

Write a program to find whether a given number is divisible by both 7 and 3.

Input Format:
Input consists of a single integer.

Output Format:
Output consists of a single line. Refer sample output for the format.

Sample Input 1:
21

Sample Output 1 :
21 is divisible by both 7 and 3

Sample Input 2:
18

Sample Output 2:
18 is not divisible by both 7 and 3

#include<stdio.h>

int main()

{

  int x=0;

  scanf("%d",&x);

  if((x%7==0) & (x%3==0))

    printf("%d is divisible by both 7 and 3",x);

  else

    printf("%d is not divisible by both 7 and 3",x);

  return 0;

}

DOLL SHOW

In london, every year during dasara there will be a very grand doll show. People try to invent new new dolls of different varieties. The best sold doll's creator will be awarded with cash prize. So people broke their head to create dolls innovatively. Knowing this competition, Mr.Lokpaul tried to create a doll which sings  only when a even number is pressed and the number should be not be zero and greater than 100.
So write a  program to help Mr.Lokpaul to win.
Input Format:
Input Consists of Single Integer which Corresponds to Number pressed by the user to the doll.
Output Format:
Display whether the doll will Sing or not. Output consists of the string "Doll will sing" or "Invalid number".
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output.]
Press a number:
56
Doll will sing

#include<stdio.h>

int main()

{

  int x=0;

  printf("Press a number:\n");

  

  scanf("%d",&x);

  if((x<=100)&(x>0)&(x%2==0))

    printf("Doll will sing");

  else

    printf("Invalid number");

  return 0;

}

FEES CALCULATOR

Write a program that displays the fees that a student needs to pay at the end of the semester. Use switch statement.

Input Format:

            The first line of the input consists of a character from the set {A, B, C, D}. A corresponds to a day scholar student with the required attendance percentage. B  corresponds to a day scholar student without the required attendance percentage. C corresponds to a  hostel student with the required attendance percentage. D corresponds to a hostel student without the required attendance percentage.
            The second line of the input consists of an integer which corresponds to the number of regular  papers for which the student is appearing in the examination.
            The third line of the input consists of an integer which corresponds to the  fee to be paid per regular  paper.
            The fourth line of the input consists of an integer which corresponds to the number of backup(arrear) papers for which the student is appearing in the examination.
            The fifth line of the input consists of an integer which corresponds to the  fee to be paid per arrear paper.

Output Format:
The output consists of a single line. Refer to Sample output for format details.
[ There is a condonation fee or Rs. 5000 for lack of attendance and the hostel students need to pay the last month mess bill of Rs.1500 along with the examination fee.]

Sample Input 1:
A
5
100
1
200

Sample Output 1:
The fee to be paid is Rs.700

Sample Input 2:
B
5
100
1
200

Sample Output 2:
The fee to be paid is Rs.5700

Sample Input 3:
C
5
100
1
200

Sample Output 3:
The fee to be paid is Rs.2200

Sample Input 4:
D
5
100
1
200

Sample Output 4:
The fee to be paid is Rs.7200

#include<stdio.h>

int main()

{

  int a,a1,b,b1;

  char c;

  scanf("%c %d %d %d %d",&c,&a,&a1,&b,&b1);

  a=(a*a1)+(b*b1);

  printf("The fee to be paid is Rs.");

  char x=c;

  switch(x)

  {

    case 'A':printf("%d",a);break;

    case 'B':printf("%d",a+5000);break;

    case 'C':printf("%d",a+1500);break;

    case 'D':printf("%d",a+6500);break;

  }

  return 0;

}

HOTEL TARIFF CALCULATOR

Write a C program to calculate the hotel tariff. The room rent is 20% high during peak seasons [April-June, November-December] . Use Switch statement.

Input Format:
The first line of the input contains an integer which corresponds to the number of the month. [ January is 1, Feb is 2 and so on].  The second line of the input consists of a floating point number which corresponds to the room rent per day. The third line of the input consists of an integer which corresponds to the number of days stayed in the hotel.

Output Format:
Output consists of a single line which displays the hotel tariff to be payed.  Hotel tariff should be displayed correct to 2 decimal places. Refer  sample output  for format details.

Sample Input 1:
3
1500
2

Sample Output 1:
Hotel Tariff: Rs.3000.00

Sample Input 2:
4
2000
2

Sample Output 2:
Hotel Tariff: Rs.4800.00


Sample Input 3:
14

Sample Output 3:
Invalid Input

#include<stdio.h>

int main()

{

  int a,c;float b;

  scanf("%d %f %d",&a,&b,&c);//printf("Hotel Tarrif: Rs.");

  b=b*c;

  int d=a;

  switch(d)

  {

    case 1:printf("Hotel Tariff: Rs.%.2f",b);break;

    case 2:printf("Hotel tariff: Rs.%.2f",b);break;

    case 3:printf("Hotel Tariff: Rs.%.2f",b);break;

    case 4:printf("Hotel Tariff: Rs.%.2f",b+0.2*b);break;

    case 5:printf("Hotel Tariff: Rs.%.2f",b+0.2*b);break;

    case 6:printf("Hotel Tariff: Rs.%.2f",b+0.2*b);break;

    case 7:printf("Hotel tariff: Rs.%.2f",b);break;

    case 8:printf("Hotel Tariff: Rs.%.2f",b);break;

    case 9:printf("Hotel Tariff: Rs.%.2f",b);break;

    case 10:printf("Hotel Tariff: Rs.%.2f",b);break;

    case 11:printf("Hotel Tariff: Rs.%.2f",b+0.2*b);break;

    case 12:printf("Hotel Tariff: Rs.%.2f",b+0.2*b);break;

    default:printf("Invalid Input");

  }

  return 0;

}

IN  OUT

Ms. Sita, the faculty handling programming lab for you is very strict. Your seniors have told you that she will not allow you to enter the week's lab if you have not completed atleast half the number of problems given last week. Many of you didn't understand this statement and so they requested the good programmers from your batch to write a program to find whether a student will be allowed into a week's lab given the number of problems given last week and the number of problems solved by the student in that week.
Can you help in writing this program?

Input Format:
Input consists of 2 integers. The first integer corresponds to the number of problems given and the second integer corresponds to the number of problems solved.

Output Format:
Output consists of the string “IN” or “OUT”.
Refer sample input and output for further formatting specifications.

Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]

Enter the number of problems given
8
Enter the number of problems solved
3
OUT

#include<stdio.h>

int main()

{

  printf("Enter the number of problems given\n");

  int a,b;

  scanf("%d",&a);

  printf("Enter the number of problems solved\n");

  scanf("%d",&b);

  if(b>=(a/2))

    printf("IN");

  else

    printf("OUT");

  return 0;

}

LEAP YEAR

Write a  program to check whether a given year is a leap year or not.

Input Format:
Input consists of a single integer.

Output Format:
Output consists of a single line. Refer sample output for details.

Sample Input 1:
1988

Sample Output 1:
1988 is a leap year

Sample Input 2:
1994

Sample Output 2:
1994 is not a leap year

Hint:
In general terms the algorithm for calculating a leap year is as follows...
A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.
Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.

#include<stdio.h>

int main()

{

  int x;

  scanf("%d",&x);

  if(x%4==0)

  {

    if(x%400==0)

    printf("%d is a leap year",x);

  else if(x%100==0)

    printf("%d is not a leap year",x);

  else

    printf("%d is a leap year",x);

  }

  else

    printf("%d is not a leap year",x);

  return 0;

}

MAXIMUM OF 2

Write a program to find the maximum of 2 numbers.

Input Format:
Input consists of 2 lines. Each line consists of an integer.

Output Format:
Output consists of a single line. Refer sample output for the format.

Sample Input 1:
3
5

Sample Output 1:
5 is the maximum number

#include<stdio.h>

int main()

{

  int x,y;

  scanf("%d %d",&x,&y);

  if(x>y)

    printf("%d is the maximum number",x);

  else

    printf("%d is the maximum number",y);

  return 0; 

}

MAXIMUM OF 3

Write a C program to find the maximum of 3 numbers.

Input Format:
Input consists of 3 lines. Each line consists of an integer.

Output Format:
Output consists of a single line. Refer sample output for the format.

Sample Input :
3
5
1

Sample Output :
5 is the maximum number

#include<stdio.h>

int main()

{

  int x,y,z;

  scanf("%d %d %d",&x,&y,&z);

  if((x>y)&(x>z))

    printf("%d is the maximum number",x);

  if((y>x)&(y>z))

    printf("%d is the maximum number",y);

  if((z>x)&(z>y))

    printf("%d is the maximum number",z);

  return 0; 

}

Mickey mouse 

#include<stdio.h>

int main()

{

  int x;

  printf("Enter the Balloon's number:\n");

  scanf("%d",&x);

  if((x%3==0)&(x%7==0))

    printf("This balloon can fly to miney.");

  else

    printf("This balloon cannot fly to miney.");

  return 0;

}

MONTHS 

Write a C program that accepts an integer as input and displays the corresponding month in words. [ January, February, March, April, May, June, July, August, September, October, November, December]. Use Switch statement.

Input Format:
Input consists of a single integer.

Output Format:
Refer sample output for details.

Sample Input 1:
3
Sample Output 1:
March

Sample Input 2:
55
Sample Output 2:
Invalid month

#include<stdio.h>

int main()

{

  int x;

  scanf("%d",&x);

  switch(x)

  {

    case 1:printf("January");break;

    case 2:printf("February");break;

    case 3:printf("March");break;

    case 4:printf("April");break;

    case 5:printf("May");break;

    case 6:printf("June");break;

    case 7:printf("July");break;

    case 8:printf("August");break;

    case 9:printf("September");break;

    case 10:printf("October");break;

    case 11:printf("November");break;

    case 12:printf("December");break;

    default:printf("Invalid month");

  }

  return 0;

}

NEW or OLD

When parents take their kids for Engineering Counselling, they always go with apreconceived notion that older the college, better will be the quality of education offered. There is a help desk in front of the counselling hall to tell out of the colleges in which seats are available, which college is the older one.
Nowadays, engineering counselling goes on for a month and the help desk needs to function on all days. So the Dean, Admissions decided to automate this task. Can you help him in this task?
Given the year of establishment of 2 colleges, write a  program to determine which college is the older one.

Input Format:
Input consists of 2 integers. The first integer corresponds to the year of establishment of college 1 and the second integer corresponds to the year of establishment of college 2.

Output Format:
Output consists of the string “College 1 is older” or “College 2 is older”.
Refer sample input and output for further formatting specifications.

Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]

Enter the year of establishment of college 1
2002
Enter the year of establishment of college 2
2008
College 1 is older

#include<stdio.h>

int main()

{

  int a,b;

  printf("Enter the year of establishment of college 1\n");

  scanf("%d",&a);

  printf("Enter the year of establishment of college 2\n");

  scanf("%d",&b);

  if(a<b)

    printf("College 1 is older");

  else

    printf("College 2 is older");

  return 0;

}

ODD OR EVEN

Write a program to find whether a given integer is an odd number or even number.

Input Format:
Input consists of a single integer.

Output Format:
Output consists of a single line. Refer sample output for the format.

Sample Input 1:
3
Sample Output 1:
3 is an odd number

Sample Input 2:
44
Sample Output 2:
44 is an even number

#include<stdio.h>

int main()

{

  int x=0;

  scanf("%d",&x);

  if(x%2==0)

    printf("%d is an even number",x);

  else

    printf("%d is an odd number",x);

  return 0;

}

Y2K Problem Detector 

Write a program that asks a user for their birth year encoded as two digits (like "62") and for the current year, also encoded as two digits (like "99"). The program is to correctly write out the users age in years.


Input Format:
Input consists of 2 integers. The first integer corresponds to the last 2 digits of the birth year. The second integer corresponds to the last 2 digits of the current year.

Output Format:
Refer sample input and output for further formatting specifications.


Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
Enter Year of Birth
62
Enter Current year
99
Your age is 37

The program will have to determine when a two digit value such as "62" corresponds to a year in the 20th century ("1962") or the 21st century. Here is another run of the program, where "00" is taken to mean the year 2000:

Enter Year of Birth
62
Enter Current year
00
Your age is 38

Assume that ages are not negative. Another run of the program:

Enter Year of Birth
27
Enter Current year
07
Your age is 80

In the following run, the age of the person could be 6 or 106 depending on the assumptions. Assume that the age will always be less than or equal to 100.

Enter Year of Birth
01
Enter Current year
07
Your age is 6


Enter Year of Birth
62
Enter Current year
99
Your age is 37


#include<stdio.h>
int main()
{
  int x,y;
  printf("Enter Year of Birth\n");
  scanf("%d",&x);
  printf("Enter Current year\n");
  scanf("%d",&y);
  if(y-x>0)
    printf("Your age is %d",(y-x));
  else
    printf("Your age is %d",(y-x+100));
  return 0;
}
SHARE
    Blogger Comment
    Facebook Comment

3 comments :

  1. months program is same as mickey mouse please do change it

    ReplyDelete
  2. In or out problem if the no. of problems are 1 and solved are 0 it is accepting which is wrong

    ReplyDelete

loading...