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

use java Write a program that, given two binary numbers represented as strings, prints their sum...
use java Write a program that, given two binary numbers represented as strings, prints their sum in binary. The binary strings are comma separated, two per line. The final answer should not have any leading zeroes. In case the answer is zero, just print one zero i.e. 0 Input: Your program should read lines from standard input. Each line contains two binary strings, separated by a comma and no spaces. Output: For each pair of binary numbers print to standard...
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.
Write a PL/SQL program that prints following: if sum of its digits raised to the power...
Write a PL/SQL program that prints following: if sum of its digits raised to the power n is equal to number itself, where n is total digits in number. If the Input is: 129, then output of the program will be: 738
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...
Write a Fortran program that reads in two NxN matrices A & B, and prints their...
Write a Fortran program that reads in two NxN matrices A & B, and prints their element-wise sum (A+B), element-wise difference (A-B), element-wise division (A/B), element-wise product (A*B), and their matrix product (matmul(A,B)), on the standard output.
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 in C++ that calculates the sum of two fractions. The program should ask...
Write a program in C++ that calculates the sum of two fractions. The program should ask for the numerators and denominators of two fractions and then output the sum of the two fractions. You will need to write four functions for this program, one to read the inputted data, one to calculate the sum of the two fractions, one to find the Greatest Common Divider (GCD) between the numerator and denominator and a function that will display the end result....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT