9.01.2011

3.Design triangle pyramid

Q. Write a program to generate a following @'s triangle?    
               

@ @ @ @ @
@ @ @ @
@ @ @

@ @


@
Ans.

/* c program for symbol triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(; num>=1; num--)
 {
  for(c=1; c<=num; c++)
     printf("@");
  printf("\n");
 }
 getch();
 return 0;
}

/*************OUTPUT**************
Enter loop repeat number(rows): 5

@@@@@
@@@@
@@@

@@


@

***********************************/

9 comments:

  1. #include
    int main()
    {
    int i,j;
    for(i=0;i<=5;i++)
    {
    for(j=0;j<5-i;j++)
    printf("@");
    printf("\n");
    }
    }

    ReplyDelete
    Replies
    1. there are use 5-i in inner loop(int j), and according to program, each line must be decreasing one by one so in starting point the values are as:

      i=0 : j<5-0=5 (condition True here inner loop repeat 5 times)
      i=1 : j<5-1=4 (condition True here inner loop repeat 4 times)
      i=2 : j<5-2=3 (condition True here inner loop repeat 3 times)
      i=3 : j<5-3=2 (condition True here inner loop repeat 2 times)
      i=4 : j<5-4=1 (condition True here inner loop repeat 1 times)
      i=5 : j<5-5=0 (condition False here inner loop break)

      So you can see that why there are use 5-i, here you can use num-i for more flexibility in C program.

      Delete
  2. 7
    14 15
    28 29 30 31
    56 57 58 59 60 61 62 63 please will u provide the code for this pattern

    ReplyDelete
  3. Try this

    #include

    int main() {
    int rows;int start_num;
    int i,j,k,col;

    printf("Enter rows");
    scanf("%d",&rows);

    printf("Enter starting index");
    scanf("%d",&start_num);
    printf("\n");

    for(i=0;i=1;j--)
    {
    col=col*2;
    }

    int final_num=1;
    final_num=col*start_num;

    printf("%d ",final_num);
    for(k=(col-1);k>0;k--)
    {
    final_num=final_num+1;
    printf("%d ",final_num);
    }
    printf("\n");


    }

    return 0;
    }


    ReplyDelete
  4. logic ko kaise undestand kare....plz tel me......

    ReplyDelete