0% found this document useful (0 votes)
18 views5 pages

Week 2 Exercises

Week 2 covers writing programs in C that perform mathematical operations using functions. This includes reading input from the user, performing operations like addition and multiplication, and printing the output. Specific tasks covered are generating random numbers, calculating averages of integers, and determining a person's age in days from their birthdate. Functions are defined and called to encapsulate code and make programs more modular. The math library and rand() function are used to generate random integers within a specified range.

Uploaded by

waelabood51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Week 2 Exercises

Week 2 covers writing programs in C that perform mathematical operations using functions. This includes reading input from the user, performing operations like addition and multiplication, and printing the output. Specific tasks covered are generating random numbers, calculating averages of integers, and determining a person's age in days from their birthdate. Functions are defined and called to encapsulate code and make programs more modular. The math library and rand() function are used to generate random integers within a specified range.

Uploaded by

waelabood51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Week 2

Outcome 2: you should be able to write a program that read input from the
user and do mathematical operation in c and print the output. ALL using
function
● math library functions
● Random number generation
Function definition and function calling
Ex1: read two integers and find / print the output of all primitive
mathematical operation (+,-.*, /, %)
Code : (using functions)
Using function
#include <stdio.h>

float sum(float a,float b) {


float s;
s=a+b;
return s;}

int main() {
float i , j ;
printf("please enter two float\n");
scanf("%f%f",&i,&j);
float result = sum(i,j );
printf(" result =%f\n", result);
// printf("%f\n",sum(6,7));
return 0;}
Ex 2: Read integer number of 4 digits and find the sum for all digit as follows:
N=5678
Output should be 26= 5+6+7+8

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;}
Using function
#include <stdio.h>
int computeAgeInDays(int bd,int bm,int by,int cd,int cm,int cy){
return (cy-by)*365+(cm-bm)*30+(cd-bd);
}
float sum(float a,float b) {
return 12;}

int main() {
float i, j ;
//printf("please enter two float\n");
//scanf("%f%f",&i,&j);
float result = sum(sum(4,20),j );
printf(" age =%d\n", computeAgeInDays(22,3,2022,26,2,2023));
// printf("%f\n",sum(6,7));

return 0;}
● math library functions
● Random number generation
Using rand() function to generate a random number in a range from a to b
and randomize the rand using time.h and srand(time(NULL) function
Example generate a random from 5 to 20
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
// Seed the random number generator
srand(time(NULL));
// Generate a random number between 5 and 25
int random_num = rand() % 21 + 5;
// Print the random number
printf("Random number between 5 and 25: %d\n", random_num);

return 0;}

You might also like