C Program for Pyramid

this is my code for pyramid of asterisks but my output is different from the given output by our instructor. It has spaces inside and underscores both left and right. Someone please help

_ _ _ _ * _ _ _ _

_ _ _ * * * _ _ _

_ _ * * * * * _ _

_ * * * * * * * _

* * * * * * * * *
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,s,co,tt=1;
 printf("Enter loop repeat number(rows): ");
 scanf("%d", &num);
 printf("\n");
 for(; num>=1; num--,tt++)
 {
  for(s=1; s<=num; s++)
    printf("_");
  for(co=tt; co>1; co--)
    printf("*");
  for(co=tt; co>1; co--)
    printf("*");
  for(co=num; co>=1; co--)
    printf("_");
  printf("\n");
 }
 return 0;
}

Upvotes: 0

Views: 578

Answers (2)

kiran Biradar
kiran Biradar

Reputation: 12732

If you see the pyramid pattern, number of asterisks(*) should print 2 more than previous loop and number of underscore(_) should print 2 less than previous loop.

Since you had 2 for loops for printing asterisks, your pyramid was starting from 2.

Consider the below code as an example.

int main()
{
   int num,co;
   printf("Enter loop repeat number(rows): ");
   scanf("%d", &num);
   printf("\n");
  int asterisk = 1;
  for(; num>=1; num--)
  {
    for(co=0; co<num; co++)
      printf("_");
    for(co=0; co<asterisk ; co++)
      printf("*");
    asterisk +=2; //Increase the number of asterisk by 2. 
    for(co=num; co>=1; co--)
      printf("_");
    printf("\n");
  }
   return 0;
 }

output:

Enter loop repeat number(rows): 10

__________*__________
_________***_________
________*****________
_______*******_______
______*********______
_____***********_____
____*************____
___***************___
__*****************__
_*******************_

Upvotes: 2

4386427
4386427

Reputation: 44274

Apparently you want 2*num + 1 chars on each line and a total of num lines. For that use a nested loop like:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int num;
    printf("Enter loop repeat number(rows): ");
    if (scanf("%d", &num) != 1) exit(1);
    int linelen = 2*num+1;  // Number of chars per line
    printf("\n");

    for(int i=0; i < num; ++i)  // Number of lines
    {
        for(int j=0; j < linelen; ++j) // Number of chars per line
        {
            if (j < num-i)
                printf("_");
            else if (j >= num-i && j <= num+i)
                printf("*");
            else 
                printf("_");
        }
        printf("\n");   // Line finished - go to a newline
    }
    return 0;
}

Input

42

Output

__________________________________________*__________________________________________
_________________________________________***_________________________________________
________________________________________*****________________________________________
_______________________________________*******_______________________________________
______________________________________*********______________________________________
_____________________________________***********_____________________________________
____________________________________*************____________________________________
___________________________________***************___________________________________
__________________________________*****************__________________________________
_________________________________*******************_________________________________
________________________________*********************________________________________
_______________________________***********************_______________________________
______________________________*************************______________________________
_____________________________***************************_____________________________
____________________________*****************************____________________________
___________________________*******************************___________________________
__________________________*********************************__________________________
_________________________***********************************_________________________
________________________*************************************________________________
_______________________***************************************_______________________
______________________*****************************************______________________
_____________________*******************************************_____________________
____________________*********************************************____________________
___________________***********************************************___________________
__________________*************************************************__________________
_________________***************************************************_________________
________________*****************************************************________________
_______________*******************************************************_______________
______________*********************************************************______________
_____________***********************************************************_____________
____________*************************************************************____________
___________***************************************************************___________
__________*****************************************************************__________
_________*******************************************************************_________
________*********************************************************************________
_______***********************************************************************_______
______*************************************************************************______
_____***************************************************************************_____
____*****************************************************************************____
___*******************************************************************************___
__*********************************************************************************__
_***********************************************************************************_

Upvotes: 1

Related Questions