conversion error
-
I am writing a C program to convert input value to hours minutes. eg : input : 126, output : 2 : 6, input : 45 , output : 0 : 45. For values like 3663, its printing 61 : 3 1 : 1 instead of 61 : 3. My logic is : int t1,t2,r=0,n,t; printf("Enter time\n"); scanf("%d",&t1); while(t1>0){ printf("%d\n",t1); if(t1<60){ break; } t=t1%60; r=r+t; t1=t1/60; printf("%d : %d\n",t1,t); } What is wrong here?
-
I am writing a C program to convert input value to hours minutes. eg : input : 126, output : 2 : 6, input : 45 , output : 0 : 45. For values like 3663, its printing 61 : 3 1 : 1 instead of 61 : 3. My logic is : int t1,t2,r=0,n,t; printf("Enter time\n"); scanf("%d",&t1); while(t1>0){ printf("%d\n",t1); if(t1<60){ break; } t=t1%60; r=r+t; t1=t1/60; printf("%d : %d\n",t1,t); } What is wrong here?
It is your
while
loop that checkst1
which gets the number of hours assigned in the loop. With input 3663,t1
is set to 63 within the first loop iteration which is then processed and shown as "1:1". Add the missingscanf()
call at the end of the loop to read the next user input and remove the unnecessary lines:scanf("%d", &t1);
while (t1 > 0){
printf("%d\n", t1);
t = t1 % 60;
t1 = t1 / 60;
printf("%d : %d\n", t1, t);
scanf("%d", &t1);
} -
I am writing a C program to convert input value to hours minutes. eg : input : 126, output : 2 : 6, input : 45 , output : 0 : 45. For values like 3663, its printing 61 : 3 1 : 1 instead of 61 : 3. My logic is : int t1,t2,r=0,n,t; printf("Enter time\n"); scanf("%d",&t1); while(t1>0){ printf("%d\n",t1); if(t1<60){ break; } t=t1%60; r=r+t; t1=t1/60; printf("%d : %d\n",t1,t); } What is wrong here?
Indent your code properly, and use meaningful names for your variables and the problem becomes clearer:
// no need for a while loop as you only have a single value to convert
int minutes = t1 % 60; // remainder = 3
int hours = t1 / 60; // 3663 / 60 = 61You could add a third calculation to convert the hours to days and hours.