Thursday, 15 April 2010

visual c++ - C Program triangle -



visual c++ - C Program triangle -

i need prompt user come in number 'n' print print stars in separate lines, exanple if user enters 5 next should printed (using while loop)

* ** *** **** ***** **** *** ** *

the code have doesn't output that

int n, x = 1, y = 1; printf("enter number : "); scanf_s("%d", &n); while (x <= n){ x++; while (y <= x){ while (y >= n){ y--; printf("*"); } printf("\n"); } system("pause"); }

}

this should work you:

#include <stdio.h> int main() { int number, count, charactercount; printf("please come in number:\n>"); scanf("%d", &number); for(count = 1; count <= number; count++) { for(charactercount = 1; charactercount <= count; charactercount++) printf("*"); printf("\n"); } for(count = 1; count < number; count++) { for(charactercount = number-1; charactercount >= count; charactercount--) printf("*"); printf("\n"); } homecoming 0; }

edit after comment:

solution while loop:

#include <stdio.h> int main() { int number, count = 1, charactercount; printf("please come in number:\n>"); scanf("%d", &number); while(count <= number) { charactercount = 1; while(charactercount <= count) { printf("*"); charactercount++; } printf("\n"); count++; } count = 1; while(count < number) { charactercount = number-1; while( charactercount >= count) { printf("*"); charactercount--; } printf("\n"); count++; } homecoming 0; }

c visual-c++

No comments:

Post a Comment