Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>

void main()
{ 
    int start, end, step, C,  F, n;
   
    printf("Enter the start of temperature (in C): ");
    scanf(" %d", &start);
    printf("Enter the end of temperature (in C): ");
    scanf(" %d", &end);
    printf("Enter the step of temperature (in C): ");
    scanf(" %d", &step);
    
    printf("C   F");
    printf("\n---  ---");
    n = 0;
    C = start;
    while(n <= step)
    {
        F = (C*9/5) + 32;
        printf("\n %.d   %.d",C,F);
        C = C + step + 1 - 1;
        n = n + 1; 
    }
}


What I have tried:

I tried adjusting the while function
Posted
Updated 3-Jun-22 3:35am
Comments
yoii 3-Jun-22 4:06am    
Enter the start of temperature (in C): -10
Enter the end of temperature (in C): 10
Enter the step of temperature (in C): 5

C F
--- ---
-10 14
-5 23
0 32
5 41
10 50

Output-2~!@
Enter the start of temperature (in C): 5
Enter the end of temperature (in C): 25
Enter the step of temperature (in C): 7

C F
--- ---
5 41
12 53
19 66
THIS IS THE OUTPUT IM SUPPOSED TO GET.

Your loop is incorrect, it should go from start to end. So a for loop would probably make more sense:
C++
for(C = start; C <= end; C += step)
{
    F = (C*9/5) + 32;
    printf("\n %d   %d",C,F); // integers do not have decimal points
}

If you want more accurate conversions then use float types for C and F.
 
Share this answer
 
v2
step is not what you think
C++
n = 0;
C = start;
while(n <= step)
{
    F = (C*9/5) + 32;
    printf("\n %.d   %.d",C,F);
    C = C + step + 1 - 1;
    n = n + 1;
}

Try something like
C++
n = 0;
C = start;
while(n <= step)
while(C <= end)
{
    F = (C*9/5) + 32;
    printf("\n %.d   %.d",C,F);
    C = C + step + 1 - 1;
    n = n + 1;
}


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900