In: Computer Science
Program Code to copy
#include<stdio.h>
int main() {
int kg1, kg2, gm1, gm2, sumKg, sumGm;
printf("Enter first weight in kg and grams : ");
scanf("%d %d", &kg1, &gm1);
printf("Enter second weight in kg and grams : ");
scanf("%d %d", &kg2, &gm2);
//if sum of grams is > 1000 then it is included in
kg.
//Selection is performed on the sum of grams.
if( gm1 + gm2 > 1000){
sumKg = kg1 + kg2 + 1;
sumGm = gm1 + gm2 - 1000 ;
}//else if sum of grams is < 1000 then it remains in
g.
else{
sumKg = kg1 + kg2 ;
sumGm = gm1 + gm2 ;
}
printf("Sum of weights : %d kg %d g", sumKg, sumGm);
return(0);
}
Program Screenshot and output
Code and Output
Case 1: sum of both grams is > 1000
Case 2: sum of both grams is < 1000