Write a C program to calculate the sum of weights
of two objects represented in Kilograms and grams
using structures.
Answers
Answered by
6
- C program to calculate the sum of weights of two objects represented in Kilograms and grams using structures is;
- #include<stdio.h>
- struct Distance {
- int kg;
- float grams;
- } w1, w2, sumofweights;
- int main() {
- printf("Enter information for 1st weight\n");
- printf("Enter kg: ");
- scanf("%d", &w1.kg);
- printf("Enter grams: ");
- scanf("%f", &w1.grams);
- printf("\nEnter information for 2nd weight\n");
- printf("Enter kg: ");
- scanf("%d", &w2.kg);
- printf("Enter grams: ");
- scanf("%f", &w2.grams);
- sumofweights.kg=w1.kg+w2.kg;
- sumofweights.grams=w1.grams+w2.grams;
- if (sumofweights.gms>1000) {
- sumofweights.gms =sumofweights.gms-1000;
- ++sumofweights.kg;
- }
- printf("\nSum of weights = %d\'-%.1f\"", sumofweights.kg, sumofweights.grams);
- return 0;
Similar questions