對於我的編程任務,我必須創建3個程序,根據用戶的輸入在c中打印出一個基於星號的三角形。 3個程序之間的區別是一個將用於循環,另一個將使用while循環,最後一個將使用goto。我有for循環程序以及goto程序,但對於while循環程序,我不確定如何將它合併到我的程序中。這是我用for循環的程序,第二個程序是我在while循環版本上的嘗試。如何改變我的程序使用while循環而不是for循環。星號三角形c
#include <stdio.h>
int main() {
int lines, a, b;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
/*create triangle based on inputed value */
for(a = 1; a <= lines; a++) {
for(b=1; b<= a; b++) {
printf("*");
}
printf("\n");
}
} while(1);
system("pause");
}
課程校#2:
#include <stdio.h>
int main() {
int lines, a = 1, b = 1;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
while(a <= lines) {
a++;
while (b <= a) {
b++;
printf("*");
}
printf("\n");
}
} while(1);
system("pause");
}