my problem is output for this weight conversion pounds to kilogram that starts from 100 to 300.
-
#include
int main()
{
int i,j;
float pound, kilogram;printf("Pound --------- Kilos\\n\\n"); do{ j=1;
do{
printf("%5d\t",i*j);
printf("%.2f lbs = %.2f Kg\n", pound, kilogram);
j++;
}while(j<=100);
printf("\n");
i++;
}while(i<=300);
return 0;
} -
#include
int main()
{
int i,j;
float pound, kilogram;printf("Pound --------- Kilos\\n\\n"); do{ j=1;
do{
printf("%5d\t",i*j);
printf("%.2f lbs = %.2f Kg\n", pound, kilogram);
j++;
}while(j<=100);
printf("\n");
i++;
}while(i<=300);
return 0;
}Start by indenting your code so it's readable:
#include
int main()
{
int i,j;
float pound, kilogram;printf("Pound --------- Kilos\\n\\n"); do { j=1; do { printf("%5d\\t",i\*j); printf("%.2f lbs = %.2f Kg\\n", pound, kilogram); j++; } while(j<=100); printf("\\n"); i++; } while(i<=300); return 0; }
It makes it so much more obvious what is going on. Then look at your code: where do you modify
pound
orkilogram
? Since they do not change, it will always print the same values. By the way, a better loop format for your application would be afor
loop:for (int i = 0; i <= 300; i++)
{
for (int j = 1; j <= 100; j++)
{
...
}
}Since you don't initialize
i
in your code at all, the value can be random depending on which compiler and / or compiler options you use."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Start by indenting your code so it's readable:
#include
int main()
{
int i,j;
float pound, kilogram;printf("Pound --------- Kilos\\n\\n"); do { j=1; do { printf("%5d\\t",i\*j); printf("%.2f lbs = %.2f Kg\\n", pound, kilogram); j++; } while(j<=100); printf("\\n"); i++; } while(i<=300); return 0; }
It makes it so much more obvious what is going on. Then look at your code: where do you modify
pound
orkilogram
? Since they do not change, it will always print the same values. By the way, a better loop format for your application would be afor
loop:for (int i = 0; i <= 300; i++)
{
for (int j = 1; j <= 100; j++)
{
...
}
}Since you don't initialize
i
in your code at all, the value can be random depending on which compiler and / or compiler options you use."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
i try for loop you suggest code:
#include
# define POUNDTOKG 0.453592;int main()
{
int i,j;
float pound, kilogram;printf("Pound --------- Kilos\\n\\n");
for (int i = 0; i <= 300; i++)
{
for (int j = 1; j <= 100; j++)
{
kilogram = pound * POUNDTOKG;} printf("%.2f pound = %.2f Kilogram\\n", pound, kilogram); } return 0;
}
but my expected output is like this,
======================
Pounds Kilos100.00 45.45
101.00 45.91
120.00 46.36
103.00 46.62
104.00 47.27
105.00 ........
.
.
.
300.00 136.08but thanks
-
i try for loop you suggest code:
#include
# define POUNDTOKG 0.453592;int main()
{
int i,j;
float pound, kilogram;printf("Pound --------- Kilos\\n\\n");
for (int i = 0; i <= 300; i++)
{
for (int j = 1; j <= 100; j++)
{
kilogram = pound * POUNDTOKG;} printf("%.2f pound = %.2f Kilogram\\n", pound, kilogram); } return 0;
}
but my expected output is like this,
======================
Pounds Kilos100.00 45.45
101.00 45.91
120.00 46.36
103.00 46.62
104.00 47.27
105.00 ........
.
.
.
300.00 136.08but thanks
You never initialize the
pound
variable, so it will always have the same (random) value.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
i try for loop you suggest code:
#include
# define POUNDTOKG 0.453592;int main()
{
int i,j;
float pound, kilogram;printf("Pound --------- Kilos\\n\\n");
for (int i = 0; i <= 300; i++)
{
for (int j = 1; j <= 100; j++)
{
kilogram = pound * POUNDTOKG;} printf("%.2f pound = %.2f Kilogram\\n", pound, kilogram); } return 0;
}
but my expected output is like this,
======================
Pounds Kilos100.00 45.45
101.00 45.91
120.00 46.36
103.00 46.62
104.00 47.27
105.00 ........
.
.
.
300.00 136.08but thanks
Can't you make simpler? Like this? Hm, this was not supposed to be a reply to Richard, but to the OP. Sorry.
#include #define POUNDTOKG 0.453592
int main() {
printf("Pound --------- Kilos\n\n");
for (int pound = 100; pound <= 300; pound++) {
printf("%.2f pound = %.2f Kilogram\n", pound, pound * POUNDTOKG);
}
return 0;
} -
Can't you make simpler? Like this? Hm, this was not supposed to be a reply to Richard, but to the OP. Sorry.
#include #define POUNDTOKG 0.453592
int main() {
printf("Pound --------- Kilos\n\n");
for (int pound = 100; pound <= 300; pound++) {
printf("%.2f pound = %.2f Kilogram\n", pound, pound * POUNDTOKG);
}
return 0;
}thanks, but i want like this output below, but when I run it in dev C++, pounds is not start 100 only kilos has a value I want to put 100 to 300 but how can I do that. but thanks a lot ====================== Pounds Kilos ====================== 100.00 45.45 101.00 45.91 120.00 46.36 103.00 46.62 104.00 47.27 105.00 ........
-
Can't you make simpler? Like this? Hm, this was not supposed to be a reply to Richard, but to the OP. Sorry.
#include #define POUNDTOKG 0.453592
int main() {
printf("Pound --------- Kilos\n\n");
for (int pound = 100; pound <= 300; pound++) {
printf("%.2f pound = %.2f Kilogram\n", pound, pound * POUNDTOKG);
}
return 0;
}your code is almost correct but in pound there is a no 100 to 300, how can I put it in my code, but thanks a lot
-
your code is almost correct but in pound there is a no 100 to 300, how can I put it in my code, but thanks a lot
Well it is, kindof. :) The format specfier to printf is wrong. Try this
#include
#define POUNDTOKG 0.453592int main() {
printf("Pound --------- Kilos\n\n");
for (int pound = 100; pound <= 300; pound++) {
printf("%d pound = %.2f Kilogram\n", pound, pound * POUNDTOKG);
}
return 0;
}
Pound --------- Kilos100 pound = 45.36 Kilogram
101 pound = 45.81 Kilogram
102 pound = 46.27 Kilogram
103 pound = 46.72 Kilogram
...
298 pound = 135.17 Kilogram
299 pound = 135.62 Kilogram
300 pound = 136.08 Kilogram...Program finished with exit code 0
Press ENTER to exit console. -
Well it is, kindof. :) The format specfier to printf is wrong. Try this
#include
#define POUNDTOKG 0.453592int main() {
printf("Pound --------- Kilos\n\n");
for (int pound = 100; pound <= 300; pound++) {
printf("%d pound = %.2f Kilogram\n", pound, pound * POUNDTOKG);
}
return 0;
}
Pound --------- Kilos100 pound = 45.36 Kilogram
101 pound = 45.81 Kilogram
102 pound = 46.27 Kilogram
103 pound = 46.72 Kilogram
...
298 pound = 135.17 Kilogram
299 pound = 135.62 Kilogram
300 pound = 136.08 Kilogram...Program finished with exit code 0
Press ENTER to exit console.That's great, thanks a lot for your help and finally I will pass my pre-final exam in c programming, thank you