Question

Exercise Write a program which asks the user to enter an integer n. It will print a diamond shape if n is negative, AA AAAA A
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.


#include <stdio.h>

int main()
{
int n,i,j,k;
printf("Enter N value: ");
scanf("%d",&n);
  
if(n>0)
{
//Triangle printed
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
printf("A");
printf("\n");
}
}
else
{
//DIAMOND printed
n=-n;
  
//PRINT UPPER HALF
int space=n/2;
int count=1;
  
for(i=0;i<n/2;i++)
{
for(j=0;j<space;j++)
printf(" ");
for(k=0;k<count;k++)
printf("AA");
printf("\n");
count++;
space--;
}
  
//PRINT MIDDLE LINE
for(i=0;i<n/2+1;i++)
printf("AA");
printf("\n");
  
//PRINT LOWER HALF
space=1;
count=n/2;
for(i=0;i<n/2;i++)
{
for(j=0;j<space;j++)
printf(" ");
for(k=0;k<count;k++)
printf("AA");
printf("\n");
count--;
space++;
}
}
   return 0;
}

============

SCREENSHOT

1 include <stdio.h> 2 3 int nain) 4- tnt n,1,,k printf(Enter N value: ) ; scant(Nd,8n); 7 tr(n-e) 10 11 //Triangle printedEnter N value: 5 А А AAA AAAА AAAА ...Program finished with exit code o Press ENTER to exit console.|Enter N value: -5 А AAAA AAАAAА AAAA А . .Program finished with exit code 0 Press ENTER to exit console .Enter N value: 7 А AA AAA AAAA AAAAА AAAAAА AAAAAAА ..Program finished with exit code 0 Press ENTER to exit console. |Enter N value: -7 АА AAAA AAAAAА AAAAAAAА ААAАА АA А ..Program finished with exit code 0 Press ENTER to exit console. |

If there are any doubts, comment down here. Нарру Lear ning..!! We are right here to help you. PLEASE give PLEASE give an UPV

Add a comment
Know the answer?
Add Answer to:
Exercise Write a program which asks the user to enter an integer n. It will print...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT