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;
}
Comments
Post a Comment