I think due to while loop the fahr variable is changing everytime the loop is executed and because of that the variable conv is changing. But I don’t know how to fix it. So can anyone please help me out?
#include<stdio.h>
main() {
int n,step;
float fahr,cel,conv;
conv=fahr+(n*step);
printf("This program makes 'n' number of list for fahrenheit to celcius converted numbers.\n");
printf("Please enter the number that is 'n'- \n");
scanf("%d",&n);
printf("\nPlease enter the fahrenheit value- \n");
scanf("%f",&fahr);
printf("\nPlease enter step count- \n");
scanf("%d", &step);
printf("\nFahrenheit\tCelcius\n");
cel=((float)5/(float)9)*(fahr-32);
while(fahr<=conv){
printf("%f\t%f\n",fahr,cel);
fahr=fahr+step;
};
}
EDIT -
So heres the updated version I wrote. This version can do two types of conversion now and it can restart the program. @DavidC.Rankin can you please check this one and suggest me what should I change? I didn't use fputs because I don't know how it works. Thanks a lot.
#include <stdio.h>
int main()
{
int n, step, i, conv ;
double fahr, cel ;
start:
puts("\nThis program can make 'n' number of lists of two type of conversions-\n\n(1) Fahrenheit to Celcius conversion\n\n(2) Celcius to Fahrenheit conversion\n");
puts("Which type of conversion would you like to use?\n(please enter the number)\n");
scanf("%d",&i);
if(i==1){
puts("\nThis program makes 'n' number of list for \n'Fahrenheit to Celcius converted numbers'\nstarting from the given Fahrenheit value.\n");
puts("Please enter the value of 'n'- ");
scanf("%d", &n);
puts("\nPlease enter the Fahrenheit value- ");
scanf("%lf", &fahr);
puts("\nPlease enter step count- ");
scanf("%d", &step);
puts("\n\tFahrenheit\tCelcius"
"\n\t----------\t-------");
conv = fahr + (n * step);
cel = (5. / 9.) * (fahr - 32);
while (fahr <= conv)
{
printf("\t%lf\t%lf\n",fahr,cel);
fahr = fahr + step;
cel = (5. / 9.) * (fahr - 32);
};
goto again;
}
else if(i==2){
puts("\nThis program makes 'n' number of list for \n'Celcius to Fahrenheit converted numbers'\nstarting from the given Celcius value.\n");
puts("Please enter the value of 'n'- ");
scanf("%d", &n);
puts("\nPlease enter the Celcius value- ");
scanf("%lf", &cel);
puts("\nPlease enter step count- ");
scanf("%d", &step);
puts("\n\tCelcius\tFahrenheit"
"\n\t-------\t----------");
conv = cel + (n * step);
fahr = ((9. * cel) / 5.) + 32;
while (cel <= conv)
{
printf("\t%lf\t%lf\n",cel,fahr);
cel = cel + step;
fahr = ((9. * cel) / 5.) + 32;
};
goto again;
}
else{
puts("Invalid Input! Please try again\n\n\n\n");
goto start;
};
again:
puts("Would you like to restart the program?\n"
"\tyes=1 no=0\n");
scanf("%d",&i);
if(i == 1){
goto start;
}
else if(i == 0){
puts("The program has ended.");
}
else{
puts("Wrong Input!\n");
goto again;
};
}