So I am new to C, and I mainly use it to make calculators for probability and stuff like that. My current project finds the increase and decrease of 2 values.
The code is:
#include <stdio.h>
//Variables
//Main Area
int
main ()
{
float num1;
float num2;
float num3;
float num4;
float answer;
//Scanning for the first number
printf ("\n Please Enter the first Value : ");
scanf ("%f", &num1);
//scanning for the second number
printf ("\n Please Enter the second Value : ");
scanf ("%f", &num2);
//calculating
num3 = num1 - num2;
num4 = num3 / num1;
answer = num4 * 100;
//Checking if the answer is an increase or decrease
if (answer > 0) {
printf("The answer has been decreasedby: %f\n", answer);
printf("percent");
}
else {
printf("The answer has been increased by: %f\n", answer * -1);
printf("percent");
}
//Printing the answer
}
The output:
Please Enter the first Value: 9
Please Enter the second Value : 90
The answer has been increased and the final value is: 900.000000
percent
So I set all the values as Floats
rather than Ints
because Ints
only support whole numbers. But when I do get a whole number rather then displaying only the single number with no decimal points, it produces a number with the number and a bunch of zeros after the decimal point. Is there a way to detect if the number Is a whole number and just display that number?
Thanks