r/programmingrequests • • Mar 02 '20

solved C program If Else program that calculates meters 3 places in each direction (from meter up to kilo or milimeter)

I want it to calculate what a meter converts to in a if else system. So I would put in 10 or 70352 meters and it calculates up to kilo so 3 places up (deka hecto then kilometer) or downward up to millimeter. I dont know how to do both math and a if else statement. the program would prompt you to enter a meter value and it would output it in the correct form (10 meters = .1 kilometers)

0 Upvotes

14 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Mar 02 '20

So a good way to find out would be

1-9 meter

10-99 decameter

100-999 hectometer

1000-infiant would be kilo

3

u/serg06 Mar 02 '20
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
	// make sure they gave value argument
	if (argc != 2) {
		printf("wrong number of arguments!\n");
		exit(-1);
	}

	// get value
	double val = atof(argv[1]);

	/* SMALLER THAN METER */

	// millimeters
	if (val < 0.01) {
		printf("%.2f mm\n", val * 1000);
	}
	// centimeters
	else if (val < 0.1) {
		printf("%.2f cm\n", val * 100);
	}
	// decimeters
	else if (val < 1) {
		printf("%.2f dm\n", val * 10);
	}

	/* BIGGER THAN METER */

	// kilometers
	else if (val > 1000) {
		printf("%.2f km\n", val / 1000);
	}
	// hectometers
	else if (val > 100) {
		printf("%.2f hm\n", val / 100);
	}
	// dekameters
	else if (val > 10) {
		printf("%.2f dam\n", val / 10);
	}

	/* NEITHER */

	else {
		printf("%.2f m\n", val);
	}
}

1

u/[deleted] Mar 02 '20

It outputs "Wrong number of arguments". Also thank you for helping me get this far.

2

u/serg06 Mar 02 '20

It does that because I assumed you wanted to use the program like this:

$ ./temp7 0.005
5.00 mm
$ ./temp7 1350
1.35 km

1

u/[deleted] Mar 02 '20

Where would I put that in the code?

1

u/serg06 Mar 02 '20

Put what in the code?

0

u/[deleted] Mar 02 '20

Nevermind. Would you happen to know a way of how to use printscreen to take a imputed number and find out which fourmula to run it through