0

I have created the program and generate code below

#include <stdio.h>
int main(){    
int a,i;    
scanf("%d",&a);
while(a!=-1){
if(a>=0 && a<=80){
for(i=a;i<=a;i++)
printf("|");
printf("%d\n");
scanf("%d",&a);
}           
}

with input 1 3 4 4 5 5 -1, it should be display a bar chart like

|

| | |

| | | |

| | | |

| | | | |

| | | | |

but in that codes ,display |

|

|

|

|

|

can anybody explain why its not working?

1 Answers1

2
for(i=a;i<=a;i++)
      ^ maybe you want 0 here?

printf("%d\n"); also looks wrong, %d is a formate string uses to print value of int. To print simply newline char it should be just printf("\n");

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Charlie Burns
  • 6,994
  • 20
  • 29
  • you're right,it works.but can i put any number in there? why it necessarry? – Gusti Bimo Marlawanto Oct 10 '13 at 04:21
  • Given what you want it to look like above, I think you just want printf("\n"); – Charlie Burns Oct 10 '13 at 04:23
  • @user160247 Suppose you have a viable like `int i = 10;` and you wants to prints its value then you need `%d` as `printf("i value is = %d", i);`. But What we can understand from your code you don't wanted to print any int value that is why `%d` is redundant. I suggest you to pick some good book from [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Grijesh Chauhan Oct 10 '13 at 04:27