Thursday 15 October 2020

conditional statements in C with example questions

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/dummy1-1/description/

decision making statements in c

Simple if:
Write a C Program to input Employee Id and basic pay of an employee and then print
Employee Id, HRA and Special allowance. Consider HRA is 20% of basic or Rs. 7000/ whichever
is less and Special allowance is 10% of basic pay whenever basic exceeds Rs 10000, Otherwise it
is 5% of basic pay.

if else:
Read in one character from the user (this may be 'Y', 'y', 'N', 'n'). If the character is 'Y' or 'y' display "YES". If the character is 'N' or 'n' display "NO". No other character will be provided as input.

https://www.hackerrank.com/challenges/30-conditional-statements/problem
Given an integer, , perform the following conditional actions:
  • If  is odd, print Weird
  • If  is even and in the inclusive range of  to , print Not Weird
  • If  is even and in the inclusive range of  to , print Weird
  • If  is even and greater than , print Not Weird
Nested if else:
Write a program to Prepare the current bill based on the following slab rates using conditional or Ternary operators, relational and logical operators. Find its time complexity.
Slab rate for below 500 units of consumption
Units
Slab rate per unit
Less than 250
1.45 Rs
Less than 500
2.60 Rs.
Slab rate for 500 units and above units of consumption
Units
Slab rate per unit
Less than 500
3.00 Rs.
500 and above
5.00 Rs.
]

Else if Ladder:

Each number on the telephone dial (except 0 and 1) corresponds to three alphabetic characters. Those correspondences are: 2 ABC 3 DEF 4 GHI 5 JKL 6 MNO 7 PRS 8 TUV 9 WXY Write a function to find the number dialled for given character. (10M)

#include<stdio.h>

void display(char ch)

{

if(ch=='A' || ch=='B' || ch=='C')

printf("2\n");

else if(ch=='D' || ch=='E' || ch=='F')

printf("3\n");

else if(ch=='G' || ch=='H' || ch=='I')

printf("4\n");

else if(ch=='K' || ch=='L' || ch=='M')

printf("5\n");

else if(ch=='N' || ch=='O' || ch=='P')

printf("6\n");

else if(ch=='Q' || ch=='R' || ch=='S')

printf("7\n");

else if(ch=='T' || ch=='U' || ch=='V')

printf("8\n");

else if(ch=='W' || ch=='X' || ch=='Y')

printf("9\n");

}

int main()

{

char ch;

printf("Enter a character:");

scanf("%c",&ch);

display(ch);

return 0;

}

A company decides to give bonus to all its employees on Diwali. A 5% bonus on salary is given to the male workers and 10% of bonus on salary to the female workers. Write a C function to read the salary and gender of the employee from a file. Calculate the bonus that must be given to the employee and display the salary that the employee will get.

Solution:

voidprintBonus()

{

float Salary,bonus;

char gender;

FILE *fp;

fp = fopen("Salary.txt","r");

scanf("%f %c",&Salary,&gender);

fclose(fp);

if(gender=='f'|| gender =='F')

bonus = 0.1*salary;

else

bonus = 0.05*salary;

printf("The Bonus: %f",bonus);

}

4) Design a flowchart to calculate the custom duty based on the following: Assume that imported goods from foreign countries are classified into 4 categories for imposing custom duty % of custom duty

Category on value of growth

1 10

2 15

3 17.5

4 25

Develop a program for current billing System based on the given requirements using if else
ladder.
Units Consumed Rate of Charge
0-200 Rs. 0.50 per unit
201- 400 Rs. 0.65 per unit in excess of 200 units
401- 600 Rs. 0.80 per unit in excess of 400 units
601 and above Rs. 1.00 per unit in excess of 600 units


calculates the fine (if any). The fee structure is as follows:
  1. If the book is returned on or before the expected return date, no fine will be charged (i.e.: .
  2. If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, .
  3. If the book is returned after the expected return month but still within the same calendar year as the expected return date, the .
  4. If the book is returned after the calendar year in which it was expected, there is a fixed fine of .
Switch Case:
Write a program to read a number between 1 to 7 and then display its corresponding day name.
Write a program to input a digit and print it in words?

Write a C program print total number of days in a month using switch case.
Total days in a month is given by below table.
MonthTotal days
January, March, May, July, August, October, December31 days
February28/29 days
April, June, September, November30 days

#include <stdio.h> int main() { int month; /* Input month number from user */ printf("Enter month number(1-12): "); scanf("%d", &month); switch(month) { /* Group all 31 days cases together */ case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("31 days"); break; /* Group all 30 days cases together */ case 4: case 6: case 9: case 11: printf("30 days");
break; /* Remaining case */ case 2: printf("28/29 days"); break; default: printf("Invalid input! Please enter month number between 1-12"); } return 0; }
Write a C program to create Simple Calculator using switch case.
  1. #include <stdio.h>
  2. int main() {
  3. char operator;
  4. double n1, n2;
  5. printf("Enter an operator (+, -, *, /): ");
  6. scanf("%c", &operator);
  7. printf("Enter two operands: ");
  8. scanf("%lf %lf",&n1, &n2);
  9. switch(operator)
  10. {
  11. case '+':
  12. printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
  13. break;
  14. case '-':
  15. printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
  16. break;
  17. case '*':
  18. printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
  19. break;
  20. case '/':
  21. printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
  22. break;
  23. // operator doesn't match any case constant +, -, *, /
  24. default:
  25. printf("Error! operator is not correct");
  26. }
  27. return 0;
  28. }

C program to find maximum between two numbers using switch case
#include <stdio.h> int main() { int num1, num2; /* Input two numbers from user */ printf("Enter two numbers to find maximum: "); scanf("%d%d", &num1, &num2); /* Expression (num1 > num2) will return either 0 or 1 */ switch(num1 > num2) { /* If condition (num1>num2) is false */ case 0: printf("%d is maximum", num2); break; /* If condition (num1>num2) is true */ case 1: printf("%d is maximum", num1); break; } return 0; }

Practice Questions:
 Q. Write a C function that takes input as Salaesman-id, total sales amount of a salesman and prints salesman-id and his incentives. The incentives are,
1) 30% of total sales amount when total sales exceeds Rs. 50,000
2) 20% of total sales amount or Rs 8,000 whichever is less when total sales exceeds Rs. 20,000
3) 5% of total sales amount when total sales are less than 20,000

Q. Develop an algorithm and program to find the total bill amount in shopping mall by reading total amount for purchased grocery. Services and taxes are calculated as shown below
                            service tax        discount
1. if bill <=500         5%                    1%
2. if bill <=1500    10%                   5%
3. if bill <=2000     15%                 10%
4. if bill <=2500     20%                 15%
5. else                    25%                  20%

Q. A factory gives the following rates of commission for the monthly sales of its product        discount
sales                            Commission
below Rs 10000                No commission  
10011-15000                    5% commission
15001-20000                    7.5% commission
Above 20000                    10% commission 
Write a program to read the sales of individual customers and print their
commission and name of the customer

Q) Age calculator program to find the age.
If your date of birth is 28/07/2020 and u want to calculate your age as on 10/08/2021.
complete the month to get the date 01/08/2020...to complete the month we need to add
(31-28) days = 3 days.
To complete the year to get 01/01/2021, we need to add 12-(7+1)=12-8=4 months
To get get 10/08/2021 we need to add 10 days and 8 months to 01/01/2021.
So the age is 3+10 days and 4+8 months i.e, 1 year 13 days.


Q) Raju started learning programming and tries to solve at least one problem on his own.
He started solving the problem at 15:10 and completed it at 16:05.
How long did he take to solve the problem? (Give your answer in minutes).

Program:
#include<stdio.h> int calcMinutes(int sh,int sm, int eh, int em); int main() { int sh,sm,eh,em,answer; sh=15,sm=10,eh=16,em=5; answer=calcMinutes(sh,sm,eh,em); if(answer == -1) printf("invalid input"); else printf("minutes taken to solve the problem = %d minutes",answer); return 0; } int calcMinutes(int sh,int sm, int eh, int em) { if(eh==sh &&em>=sm) return em-sm; if(eh >sh) return (eh-(sh+1))*60+(60-sm)+em; return -1; }

Problem Comprehension:

 condition 1: if endingHoursis equal to startingHours then

endingMinutes must be >startingMinutes.

Ex: 15:10 and 15: 30 then answer=endingMinutes – startingMinutes = 30-10 = 20 minutes

·         condition 2: if endingHours>startingHours

      Ex:  15:10 and 18: 05 then 

          if the time is 15:10 then after 50 minutes it will be 16:00 ...60-startingMinutes

          now find the difference between 16:00 and 18:00 that is 2 hours i.e endingHours-(staringHours+1)

          add 5 minutes in 18:05

      so, formula= (endingHours-(staringHours+1))*60+(60-startingMinutes)+endingMinutes

Q

     Q) Meghana was filling application form for the job of Assistant professor in KLU. 

         The application requires the age of Meghana as on 08/2021. 

         If Meghana is born on 02/2005. What is the age in years she needs to fill in application form.



No comments:

Post a Comment