Question

In: Computer Science

​Write a program which requests two weights in kilograms and grams and prints the sum of...

​Write a program which requests two weights in kilograms and grams and prints the sum of the weights. For example, if the weights are 3kg 500g and 4kg 700g, your program should print 8kg 200g. in c programming selection logic

Solutions

Expert Solution

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


Related Solutions

Write a program that prints the sum of its command-line arguments (assuming they are numbers). For...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For example, java Adder 3 2.5 -4.1 should print The sum is 1.4
Write a c program that prints the final sum of all values from 1 to n...
Write a c program that prints the final sum of all values from 1 to n only when n is a positive value. The program is to print "Poor!" when the final sum is less than 70, print "Good" when the sum is between 71 and 90. or "Great!" when the sum is 91 or better.
in java Write an application that gets two numbers from the user and prints the sum,...
in java Write an application that gets two numbers from the user and prints the sum, product, difference and quotient of the two numbers in a GUI.
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
In python write a program which prints the longest substring of numbers which occur in ascending...
In python write a program which prints the longest substring of numbers which occur in ascending order s=342153476757561235
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
write a program to find the maximum possible sum such that no two chosen numbers are...
write a program to find the maximum possible sum such that no two chosen numbers are adjacent either vertically, horizontally, or diagonally. code in java
Write a JAVA program which prints the root node after the "remove" procedure the the splay...
Write a JAVA program which prints the root node after the "remove" procedure the the splay tree.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT