You are on page 1of 3

Week 1

Outcome 1 : you should be able to write a program that read input from the
user and do mathematical operation in c and print the output.
Ex1: read two integers and find / print the output of all primitive
mathematical operation (+,-.*, /, %)
Code :
#include <stdio.h>
int main() {
int a=0, b=0;
printf("please enter two integers\n");
scanf("%d%d",&a,&b);
printf("the value of a is %d\n",a);
printf("the value of b is %d\n",b);
int result;
result=a+b;
printf("%d+%d=%d\n",a,b,result);
result =a-b;
printf("%d-%d=%d\n",a,b,result);
result =a*b;
printf("%dx%d=%d\n",a,b,result);
result=a/b;
printf("%d/%d=%d\n",a,b,result);
result =a%b;
printf("%d mod %d = %d\n",a,b,result);
return 0;}
Ex 2: Read integer number of 4 digits and print the decimal value for each
digit as follows:
N=5678
Output should be 5678= 5000+600+70+8

#include <stdio.h>

int main(void) {
int n;
int d1,d2,d3,d4;
printf("please enter number of 4 digits\n");
scanf("%d",&n);
d1=n%10;
d2=(n/10)%10;
d3=((n/10)/10)%10;
d4=((n/10)/10/10%10);
printf("%d=%d+%d+%d+%d\n",n,d1,d2*10,d3*100,d4*1000);
return 0;
}

Practice :
1.(Easy) read temperature in c and print the equivalent temperature in
Fahrenheit
2.( intermediate) find and print the average for three integers as float
3.(challenging) read a birth data as year month day and find the age in days .
#include <stdio.h>

int main() {
int birth_year, birth_month, birth_day;
int current_year, current_month, current_day;
int age_in_days;

// Input birthdate
printf("Enter birthdate in format YYYY MM DD: ");
scanf("%d %d %d", &birth_year, &birth_month, &birth_day);

// Input current date


printf("Enter current date in format YYYY MM DD: ");
scanf("%d %d %d", &current_year, &current_month, &current_day);

// Calculate age in days


age_in_days = (current_year - birth_year) * 365 + (current_month -
birth_month) * 30 + (current_day - birth_day);

// Print result
printf("Age in days: %d\n", age_in_days);

return 0;
}

You might also like