Program Control Structures – Decision Making & Branching (Tutorial)

Tutorial 09



01. Swap two values stored in two different variables.

#include <stdio.h>
int main()

{

int a = 10;
int b = 20;
int c;
c = a;
a = b;
b = c;
printf("%i\n%i", a,b);
return 5;
}

02. Check whether an entered number is negative, positive or zero.

#include<stdio.h>
int main()

{

int value;
printf ("Enter your value: ");
scanf("%i", &value);
if (value == 0)
printf("Your number is zero");

else
if (value > 0)
printf("Your Value is Positive");
else
printf("Your value is Negetive");
return 5;
}

03. Check whether an entered year is leap year or not.

#include<stdio.h>
int main()

{

int year;
int value;
printf ("Enter Year : ");
scanf("%i" , &year);
value = year % 4;
if (value == 0)
printf("Leap Year");
else
printf("Not a leap year");
return 5;
}

04. Write a program that asks the user to type in two integer values at the terminal. Test these two numbers to determine if the first is evenly divisible by the second, and then display an appropriate message at the terminal.

#include <stdio.h>
int main()

{

int value1;
int value2;
int value3;

printf("Enter The Number 1 :");
scanf("%i", &value1);

printf("Enter The Number 2 :");
scanf("%i", &value2);

value3 = value1 % value2;

if (value3 == 0)
printf("Number 1 is evenly divisable by Number 2");
else
printf("Number 1 is not evenly divisably by Number 2");
return 5;
}


05. Write a program that accepts two integer values typed in by the user. Display the result of dividing the first integer by the second, to three-decimal-place accuracy. Remember to have the program check for division by zero.

#include<stdio.h>
int main ()

{

float value1;
float value2;
float value3;
printf("Enter The Number 1 :");
scanf("%f", &value1);
printf("Enter The Number 2 :");
scanf("%f", &value2);
value3 = value1 / value2;
printf("Value is :%.3f" ,value3 );
return 0;
}

06. Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types in 932, the program should display nine three two. Remember to display “zero” if the user types in just a 0.

#include <stdio.h>
int main()

{

int number;
int value;
printf("Enter the Number : ");
scanf("%i",&number);
if (number != 0)
{  
value = number / 100;
printf("%i\n" , value);
number = number % 100;
value = number / 10;
printf("%i\n" , value);
number = number % 10;
value = number / 1;
printf("%i\n" , value);
number = number % 1;
}
else
{
printf("Your Value is zero");
}
return 0;
}

07. Input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer.

Calculate percentage and grade according to following:

a. Percentage >= 90% : Grade A

b. Percentage >= 80% : Grade B

c. Percentage >= 70% : Grade C

d. Percentage >= 60% : Grade D

e. Percentage >= 40% : Grade E

f. Percentage < 40% : Grade F



08. Input basic salary of an employee and calculate its Gross salary according to following:

(note: HRA and DA are allowances)

a. Basic Salary <= 10000 : HRA = 20%, DA = 80%

b. Basic Salary <= 20000 : HRA = 25%, DA = 90%

c. Basic Salary > 20000 : HRA = 30%, DA = 95% 




09. Write a program that acts as a simple “printing” calculator. The program should allow the user to type in expressions of the form number operator: The following operators should be recognized by the program: + - * / S E


The S operator tells the program to set the “accumulator” to the typed-in number.
The E operator tells the program that execution is to end.

The arithmetic operations are performed on the contents of the accumulator with the number that was keyed in acting as the second operand. The following is a “sample run” showing how the program should operate:


Begin Calculations
10 S Set Accumulator to 10
= 10.000000 Contents of Accumulator
2 / Divide by 2
= 5.000000 Contents of Accumulator
55 - Subtract 55
-50.000000
100.25 S Set Accumulator to 100.25
= 100.250000
4 * Multiply by 4
= 401.000000
0 E End of program
= 401.000000
End of Calculations.
Make certain that the program detects division by zero and also checks for unknown operators.

Click Here To Get The Code!


10.  Input electricity unit charges and calculate total electricity bill according to the given condition:
a. For first 50 units Rs. 0.50/unit
b. For next 100 units Rs. 0.75/unit
c. For next 100 units Rs. 1.20/unit
d. For unit above 250 Rs. 1.50/unit
e. An additional surcharge of 20% is added to the bill

Click Here To Get The Code!


11.  An envelope manufacturing company hires people to make envelopes. They provide all the raw material needed and pay at the following rates. Write a program to input the no of envelopes made and to calculate and print the amount due
Envelopes         Rate
1-1000               75 cents
1001-1500         1 rupee
1501-2000         1 rupee and 15 cents
2001                  1 rupee and 25 cents

Click Here To Get The Code!


12. Find the number of separate Notes and coins required to represent a given monetary value. E,g, 2700 required 1 - 2000 note, 1- 500 note and 2-100 notes.

Click Here to Get The Code!


13. Display Age, Birthday, and Gender using a given National Identity Card number.

Post a Comment

Previous Post Next Post