Tutorial_09

 R.S.R. Ranathunga

ICT/2022/139


2. Swap two values stored in two different variables. 

#include<stdio.h>

int main(){

int a=5, b=10, temp=0;

temp=a;

a=b;

b=temp;

printf("The values of a=%i and b=%i", a,b);

return 0;

}


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

#include<stdio.h>

int main(){

int num=0;

printf("Enter number:");

scanf("%i", &num);

if(num<0)

printf("Negative");

else if(num==0)

printf("Zero");

else

printf("Positive");

return 0;

}


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

#include <stdio.h>

int main(){

int n;

printf("Enter Year:");

scanf("%i", &n);

if(n%4==0)

printf("Leap Year");

else

printf("Not Leap Year");

return 0;

}


5. 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 a, b;

printf("Enter Number 1 : ");

scanf("%i", &a);

printf("Enter Number 2 : ");

scanf("%i", &b);

if(b==0)

printf("Error");

else

if(a%b==0)

printf("Number 1 is evenly divisible by number 2");

else

printf("Number 1 is not evenly divisible by number 2");

return 0;

}


6. 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(){

int num1, num2;

float result;

printf("Enter number 1: ");

scanf("%i", &num1);

printf("Enter number 2: ");

scanf("%i", &num2);

if(num2==0){

printf("Error\n");

}else{

result=(float)num1/(float)num2;

printf("%.3f", result);

}

return 0;

}


7. 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. 


int main(){


int number;

printf("Enter Integer : ");

scanf("%i", &number);

printf("Digit:");

if(number==0){

printf("Error(zero number) ");

}else{

while(number>0){

int digit=number%10;

if(digit==9)

printf(" Nine");

else if(digit==8)

printf(" Eight");

else if(digit==7)

printf(" Seven");

else if(digit==6)

printf(" Six");

else if(digit==5)

printf(" Five");

else if(digit==4)

printf(" Four");

else if(digit==3)

printf(" Three");

else if(digit==2)

printf(" Two");

else if(digit==1)

printf(" One");

else if(digit==0)

printf(" Zero");

number=number/10;

}

}

printf("\n");

return 0;

}


8. 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 

#include <stdio.h>

int main(){

int m1, m2, m3, m4, m5, total=0;

float avg=0, percentage=0;

printf("Enter Physics subject marks: ");

scanf("%i", &m1);

printf("Enter Chemistry subject marks: ");

scanf("%i", &m2);

printf("Enter Mathematics subject marks: ");

scanf("%i", &m3);

printf("Enter Biology subject marks: ");

scanf("%i", &m4);

printf("Enter Computer subject marks: ");

scanf("%i", &m5);

total=m1+m2+m3+m4+m5;

avg=(float)total/5;

percentage=((float)total/500)*100;

printf("Percenatage of the marks:%.2f\n", percentage);

if(percentage>=90)

printf("Grade A");

else if(percentage>=80)

printf("Grade B");

else if(percentage>=70)

printf("Grade C");

else if(percentage>=60)

printf("Grade D");

else if(percentage>=40)

printf("Grade E");

else if(percentage<40)

printf("Grade F");

return 0;

}



9. 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%


#include <stdio.h>

int main(){

float salary, gross_salary=0, HRA=0, DA=0;

printf("Enter Basic Salary: ");

scanf("%f", &salary);

if(salary<=10000){

HRA=salary*0.2;

DA=salary*0.8;

gross_salary=salary+HRA+DA;}

else if(salary<=20000){

HRA=salary*0.25;

DA=salary*0.9;

gross_salary=salary+HRA+DA;}

else if(salary>20000){

HRA=salary*0.3;

DA=salary*0.95;

gross_salary=salary+HRA+DA;}

printf("Gross Salary is: %.3f", gross_salary);

return 0;

}


10. 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.


#include<stdio.h>

int main(){

float number, value;

char operator;

printf("Type in your expression:\n ");

scanf("%f %c", &number,&operator);

switch(operator){

case 'S':

printf("%.6f Contents of Accumulator\n", number);

break;

case 'E':

printf("%.6f End\n", number);

break;

case '+':

scanf("%f", &value);

printf("%.6f\n", number+value);

break;

case '-':

scanf("%f", &value);

printf("%.6f\n", number-value);

break;

case '*':

scanf("%f", &value);

printf("%.6f\n", number*value);

break;

case '/':

if(number==0)

printf("Division by zero\n");

else

scanf("%f", &value);

printf("\n%.6f Contents of Accumulator\n", number/value);

break;

default:

printf("Unkown Operator\n");

break;

}

return 0;

}

11. 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
#include <stdio.h>
int main(){
int units;
float bill=0, surcharge=0, total_bill=0;
printf("Enter unit charges: ");
scanf("%i", &units);
if(units<=50){
bill=units*0.50;
}else if(units<=150){
bill=((units-50)*0.75)+25;
}else if(units<=250){
bill=((units-150)*1.20)+100;
}else{
bill=((units-250)*1.50)+220;
}  
surcharge=bill*0.20;
total_bill=bill+surcharge;
printf("Total Bill is %.2f rupees", total_bill);
return 0;
}

12. 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
#include <stdio.h>
int main(){
int counts;
float amount=0;
printf("Enter no of Envelopes: ");
scanf("%i", &counts);
if(counts<=1000){
amount=counts*0.75;
}else if(counts<=1500){
amount=counts*1.00;
}else if(counts<=2000){
amount=counts*1.15;
}else{
amount=counts*1.25;
}  
printf("Total amount due is %.2f rupees", amount);
return 0;
}

13. 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.
#include <stdio.h>
int main(){
int money;
int note5000=0, note2000=0, note1000=0, note500=0, note100=0, note50=0, note20=0;
printf("Enter monetary value: ");
scanf("%i", &money);
note5000=money/5000;
money%=5000; 
note2000=money/2000;
money%=2000;
note1000=money/1000;
money%=1000;
note500=money/500;
money%=500;
note100=money/100;
money%=100;
note50=money/50;
money%=50;
note20=money/20;
money%=20;
printf("Number of 5000 notes: %i\n", note5000);
printf("Number of 2000 notes: %i\n", note2000);
printf("Number of 1000 notes: %i\n", note1000);
printf("Number of 500 notes: %i\n", note500);
printf("Number of 100 notes: %i\n", note100);
printf("Number of 50 notes: %i\n", note50);
printf("Number of 20 notes: %i", note20);
return 0;
}

14. Display Age, Birthday, and Gender using a given National Identity Card number.
#include <stdio.h>
int main(){
int age=24; 
int birthday=12;
char birth_month[]="January";
int National_ID_Number, ID=2000012208;
char gender[]="Male";
printf("Enter National ID Card Number: ");
scanf("%i", &National_ID_Number);
if(National_ID_Number==ID){
printf("Age : ");
printf("%i\n", age);
printf("Birthday : ");
printf("%s %i\n", birth_month,birthday);
printf("Gender : ");
printf("%s", gender);
}else
printf("Input Valid ID Number...");
return 0;
}

Comments