Tanjima Tani
Tanjima Tani

Reputation: 580

Half pyramid of numbers

I need to generate the following output of odd numbers in pyramid pattern. The output will be like

1
3 3
5 5 5
7 7 7 7

I have written the following code. What portion i should modify?

#include<stdio.h>
int main()
{
  int num,r,c;
  printf("Enter structure number : ");
  scanf("%d", &num);

      for(r=1; r<=num; r++)
      {
          if(r%2 != 0){
              m=1;
             for(c=1; c<=m; c++)
                printf("%d",r);
             printf("\n");
      }
  }
  return 0;
}

Current Output:

Current output is like-

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Upvotes: 1

Views: 1679

Answers (8)

anon
anon

Reputation:

You could write:

  for(r=1; r <= num; r+=2) //we only need odd numbers
  {
      times = r/2 + 1; //how many times to print odd number
      for(c=1; c <= times; c++)
          printf("%d",r); //print one character at a time
      printf("\n");
  }

You'll probably understand it better if you only iterate through odd numbers. I'm currently doing that and calculating how many times I need to print that number, then I'm just printing it as many times as times is.

Upvotes: 4

Himanshu
Himanshu

Reputation: 4395

Please take a look at this:

for(r=1; r<=num; r+=2) // increment by 2, work for r= 1,3,5,7...
{
    for(c=1; c<=r; c+=2)// increment by 2
        printf("%d",r);
     printf("\n");
}

If you want alternate numbers like 1,3,5,7... just increment value by 2.

Upvotes: 0

Mukit09
Mukit09

Reputation: 3399

In row 1, you should print 1.
In row 2, you should print 3.
In row 3, you should print 5.
In row 4, you should print 7.
............................
............................
In row n, you should print 2*n-1.

You can check this:

 #include<stdio.h>


int main()
{
    int num,r,c;
    printf("Enter structure number : ");
    scanf("%d", &num);
    for(r=1; r<=num; r++)
    {
        for(c=1; c<=r; c++)
            printf("%d",2*r-1);
        printf("\n");
    }
    return 0;
}

Upvotes: 0

Camille G.
Camille G.

Reputation: 3236

#include<stdio.h>
int main()
{
    int num,r,c;
    printf("Enter structure number : ");
    scanf("%d", &num);

    for(r=1; r<=num; r++)
    {
        if(r%2 != 0){
            int m=r;
            for(c=1; c<=m; c++)
            {
                if(c%2 != 0){
                    printf("%d ",r);
                }
            }
            printf("\n");
        }
    }
  return 0;
}

and test

sh-4.3# main                                                                                                                                                                          
Enter structure number : 9                                                                                                                                                            
1                                                                                                                                                                                     
3 3                                                                                                                                                                                   
5 5 5                                                                                                                                                                                 
7 7 7 7                                                                                                                                                                               
9 9 9 9 9   

Upvotes: 1

Waqar ul islam
Waqar ul islam

Reputation: 498

you have 2 errors in this code . m is not declared anywhere . you are running a infinite loop

try this .

#include<stdio.h>
int main()
{
  int num,r,c,m;
  printf("Enter structure number : ");
  scanf("%d", &num);

      for(r=1; r<=num; r++)
      {
          if(r%2 != 0){
              m=r;
             for(c=1; c<=m; c++)
                printf("%d",r);
             printf("\n");
      }
  }
  return 0;
}

Upvotes: 3

Mateusz Piotrowski
Mateusz Piotrowski

Reputation: 9117

the inner for loop should look like this:

for(c=1; c <= r/2; c++)
            printf("%d ",r);

just think about it for a second. you want to print a rounded r/2 of numbers in every line, right?

like:
3/2 -> 1.5 -rounded-> 1 -> prints: 3
5/2 -> 2.5 -rounded-> 2 -> prints: 5 5
and so on.

you can run the code here on ideone.com

Upvotes: 1

54l3d
54l3d

Reputation: 3973

Some little modifications and it works:

#include<stdio.h>
 int main()
 {
 int num,r,c,m=0;
 printf("Enter structure number : \n");
 scanf("%d", &num);

  for(r=1; r<=num; r++)
  {
      if(r%2 != 0){
          m++;
         for(c=1; c<=m; c++)
            printf("%d ",r);
         printf("\n");
  }
 }
 return 0;
}
  • m is undeclared
  • line feed at the end of the printf message
  • m incremented each odd iteration
  • space between printed unmbers

Here is a demo.

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

In your code, instead of

  m=1;

you should write

 m= ( (r/2) + 1);

Oterwise, all the time, you'll be iterating in the for loop only once.

Upvotes: 1

Related Questions