My program wont compile, I am trying to ask it to print a pyramid and then to pr
ID: 3873241 • Letter: M
Question
My program wont compile, I am trying to ask it to print a pyramid and then to print a diamond by asking it if it wants a diamond
/**
* Assignment4
*
* @author Sam
* @version 4.0
*/
import java.util.*;
public class Assignment4
{
int main() {
int i, space, rows, star=0;
printf("Please type in the height of the tree");
printf("It must be at least 1 high and no more than 25");
scanf("%d",&rows);
/* printing one row in every iteration */
for(i = 1; i <= rows; i++) {
/* Printing spaces */
for(space = 1; space <= rows-i; space++) {
printf(" ");
}
/* Printing stars */
while(star != (2*i - 1)) {
printf("*");
star++;;
}
star=0;
/* move to next row */
printf(" ");
}
return 0;
}
int main() {
int i, space, rows=7, star=0;
printf("Would you like a diamond instead of a pyramid?");
/* Printing upper triangle */
for(i = 1; i <= rows; i++) {
/* Printing spaces */
for(space = 1; space <= rows-i; space++) {
printf(" ");
}
/* Printing stars */
while(star != (2*i - 1)) {
printf("*");
star++;;
}
star=0;
/* move to next row */
printf(" ");
}
rows--;
/* Printing lower triangle */
for(i = rows;i >= 1; i--) {
/* Printing spaces */
for(space = 0; space <= rows-i; space++) {
printf(" ");
}
/* Printing stars */
star = 0;
while(star != (2*i - 1)) {
printf("*");
star++;
}
printf(" ");
}
return 0;
}
}
Explanation / Answer
#include<stdio.h>
void pyramid() {
int i, space, rows, star=0;
printf("Please type in the height of the pyramid");
printf("It must be at least 1 high and no more than 25 : ");
scanf("%d",&rows);
/* printing one row in every iteration */
for(i = 1; i <= rows; i++) {
/* Printing spaces */
for(space = 1; space <= rows-i; space++) {
printf(" ");
}
/* Printing stars */
while(star != (2*i - 1)) {
printf("*");
star++;;
}
star=0;
/* move to next row */
printf(" ");
}
}
void diamond()
{
int i, space, rows=7, star=0;
/* Printing upper triangle */
for(i = 1; i <= rows; i++) {
/* Printing spaces */
for(space = 1; space <= rows-i; space++) {
printf(" ");
}
/* Printing stars */
while(star != (2*i - 1)) {
printf("*");
star++;;
}
star=0;
/* move to next row */
printf(" ");
}
rows--;
/* Printing lower triangle */
for(i = rows;i >= 1; i--) {
/* Printing spaces */
for(space = 0; space <= rows-i; space++) {
printf(" ");
}
/* Printing stars */
star = 0;
while(star != (2*i - 1)) {
printf("*");
star++;
}
printf(" ");
}
}
int main() {
int a=0;
printf("Enter 1 to print diamond Enter 2 to print Pyramid Enter your choice :");
scanf("%d",&a);
if(a==1)
diamond();
else if(a==2)
pyramid();
else
printf(" Wrong Input");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.