9.05.2011

Star (*) Praymid

Q. Write a C program to print the following triangle:







*








* * *






* * * * *




* * * * * * *


* * * * * * * * *
* * * * * * * * * * *




Ans.

 /*c program for star pyramid*/
 #include<stdio.h>
 #include<conio.h>

 int main()
 {
  int num=6,r,c,sp;

  for(r=1; num>=r; r++)
  {
   for(sp=num-r; sp>=1; sp--)
       printf(" ");
   for(c=r; c>=1; c--)
        printf("*");
   for(c=r; c>1; c--)
        printf("*");
   printf("\n");
  }

  getch();
  return 0; 
 }


/***************OUTPUT***************








*








***






*****




*******


*********
***********



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

22 comments:

  1. Very helpful best site visited so far:-))

    ReplyDelete
  2. i need a c code to print the following triangle
    5
    5 5
    5 5 5
    5 5 5 5

    ReplyDelete
    Replies
    1. @Femi,
      Your required source code of above program as:

      #include "stdio.h"
      int main()
      {
      int num=5,r,c;
      for(r=1; r<num; r++)
      {
      for(c=1; c<=r; c++)
      printf("%d",num);
      printf("\n");
      }
      getch();
      return 0;
      }

      Delete
  3. I need a code for the following patter (Using recursive function).
    Help is appreciated.
    *
    * *
    * * *
    * * * *

    ReplyDelete
    Replies
    1. #include
      #include
      main()
      {
      int i,j,n;
      printf("Enter the number of rows:- ");
      scanf("%d", &n);
      for(i=1;i<=n;i++)
      {
      for(j=1;j<=i;j++)
      {
      printf("*");
      }
      printf("\n");
      }
      getch();
      }

      Delete
    2. if i put +i,++j instead of i++, j++ in this program.was the program works with same output.?

      Delete
  4. I need a code to print the following pyramid:
    1
    22
    333
    4444
    55555
    666666

    ReplyDelete
    Replies
    1. Console.Write("enter how many rows you want:");
      int number=int.Parse(Console.ReadLine());
      for(row=1;row<=number;row++)
      {
      for(col=1;col<=row;col++)
      {
      Console.Write(row);
      }
      Console.WriteLine();
      }

      Delete
  5. /*

    1
    22
    333
    4444
    55555




    for (int i=1;i<=5; i++) {

    for (int j=1; j<=i; j++) {

    NSLog(@"%i",i);
    }
    NSLog(@"\n");
    }


    */

    ReplyDelete
  6. Sir,
    I need a code.example input height 3,base 2,oreentation:
    orientation1: **
    ****
    ******
    orientation2: ********
    ****
    **

    orientation3:
    **
    ****
    ********

    orientation4:
    ********
    ****
    **

    orientation5:

    **
    ****
    ********

    orientation 6:
    ********
    ****
    **

    ReplyDelete
  7. hello
    i need a code to convert number to words

    ReplyDelete
    Replies
    1. #include
      #include < conio.h >
      void main()
      {
      long num,div,n1;
      int flag,digit,pos,tot_dig;
      clrscr();
      printf("\n Enter number: ");
      scanf("%ld",& num);
      if(num==0)
      {
      printf("Zero\n\n");
      exit(0);
      }
      if(num>99999)
      {
      printf("please enter number between 0 and 100000\n\n");
      exit(0);
      }
      tot_dig = 0;
      div = 1;
      n1 = num;
      while ( n1 > 9 )
      {
      n1 = n1 / 10;
      div = div * 10;
      tot_dig++;
      }
      tot_dig++;
      pos = tot_dig;
      while ( num != 0 )
      {
      digit= num / div;
      num = num % div;
      div = div / 10;
      switch(pos)
      {
      case 2:
      case 5: if ( digit == 1 )
      flag = 1;
      else
      {
      flag = 0;
      switch(digit)
      {
      case 2: printf("twenty ");break;
      case 3: printf("thirty ");break;
      case 4: printf("forty ");break;
      case 5: printf("fifty ");break;
      case 6: printf("sixty ");break;
      case 7: printf("seventy ");break;
      case 8: printf("eighty ");break;
      case 9: printf("ninty ");
      }
      }
      break;
      case 1:
      case 4: if ( flag == 1 )
      {
      flag = 0;
      switch(digit)
      {
      case 0 : printf("ten ");break;
      case 1 : printf("eleven ");break;
      case 2 : printf("twelve ");break;
      case 3 : printf("thirteen ");break;
      case 4 : printf("fourteen ");break;
      case 5 : printf("fifteen ");break;
      case 6 : printf("sixteen ");break;
      case 7 : printf("seventeen ");break;
      case 8 : printf("eighteen ");break;
      case 9 : printf("ninteen ");
      }
      }
      else
      {
      switch(digit)
      {
      case 1 : printf("one ");break;
      case 2 : printf("two ");break;
      case 3 : printf("three ");break;
      case 4 : printf("four ");break;
      case 5 : printf("five ");break;
      case 6 : printf("six ");break;
      case 7 : printf("seven ");break;
      case 8 : printf("eight ");break;
      case 9 : printf("nine ");
      }
      }
      if ( pos == 4 )
      printf("thousand ");
      break;
      case 3:
      if ( digit> 0 )
      {
      switch(digit)
      {
      case 1 : printf("one ");break;
      case 2 : printf("two ");break;
      case 3 : printf("three ");break;
      case 4 : printf("four ");break;
      case 5 : printf("five ");break;
      case 6 : printf("six ");break;
      case 7 : printf("seven ");break;
      case 8 : printf("eight ");break;
      case 9 : printf("nine ");
      }
      printf("hundred ");
      }
      break;
      }
      pos--;
      }
      if ( pos == 4 && flag == 0)
      printf("thousand");
      else
      if ( pos == 4 && flag == 1)
      printf("ten thousand");
      if ( pos == 1 && flag == 1 )
      printf("ten ");
      getch();
      }

      Delete
    2. here is a short code to convert 3 digit number to word :

      #include
      #include
      #include
      #include

      int main()
      {
      int i;
      int j[4];
      printf("\t\t enter a three digit number\n");
      scanf("%d",&i);
      printf("number u have entered is %d\n",i);
      if(i>=1000)
      {
      printf(" enter valid 3 digit number");
      }
      else
      {
      j[3]=i%100;
      j[2]=i%10;
      j[1]=(i/10)%10;
      j[0]=(i/100);
      char c[9][9]={"one","two","three","four","five","six","seven","eight","nine"};

      char d[10][10]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eigthteen","nineteen"};

      char e[10][10]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};

      if(j[0]!=0)
      {
      printf("%s hundred ",c[j[0]-1]);

      }
      if(j[1]==0 && j[2]!=0)
      {
      printf("and %s",c[j[2]-1]);
      }
      if(j[3]<20 && j[3]>9)
      {
      printf("and %s",d[j[3]-10]);
      }
      else if(j[1]!=0)
      {
      printf("and %s ",e[j[1]-2]);
      if(j[2]!=0)
      {
      printf("%s",c[j[2]-1]);

      }
      }
      }





      getch();
      return 0;

      }

      Delete
  8. if i put ++r,--sp,--c instead of r++, sp-- ,c--in this program.was the program works with same output.?

    ReplyDelete
  9. #include
    void main()
    {
    int x,i;
    for(x=1;x<=5;x++)
    {
    for(i=1;i<=x;i++)
    {
    printf("*");
    }
    printf("\n");
    }
    }

    ReplyDelete
  10. entify the logic behind the series

    6 28 66 120 190 276....

    The numbers in the series should be used to create a Pyramid. The base of the Pyramid will be the widest and will start converging towards the top where there will only be one element. Each successive layer will have one number less than that on the layer below it. The width of the Pyramid is specified by an input parameter N. In other words there will be N numbers on the bottom layer of the pyramid.

    The Pyramid construction rules are as follows
    First number in the series should be at the top of the Pyramid
    Last N number of the series should be on the bottom-most layer of the Pyramid, with Nth number being the right-most number of this layer.
    Numbers less than 5-digits must be padded with zeroes to maintain the sanctity of a Pyramid when printed. Have a look at the examples below to get a pictorial understanding of what this rule actually means.
    Example

    If input is 2, output will be

    00006
    00028 00066

    If input is 3, output will be

    00006
    00028 00066
    00120 00190 00276

    Formal input and output specifications are stated below

    Input Format:

    Firs

    ReplyDelete
  11. hello,I know the popularity of you thats why i am commenting
    Can you please help me in making these 2 programs as i am new to programming:
    I would be highly thankful to you
    Program 1
    Write a program which takes an integer input from the user (max value can be 8). The program then creates a matrix which displays the power of the input integer which is incremented in each column and subsequent row. For example, if a user enters the value 5, then the output should look like:

    1 1 1 1 1
    2 4 8 16 32
    3 9 27 81 243
    4 16 64 256 1024
    5 25 125 625 3125
    program 2:
    Write a program which moves the character ‘X’ in a 5x5 Matrix at the output. The input to this program is a character from the user: ‘w’, ‘a’, ‘s’ and ‘d’, in order to move ‘X’ up, left, down, and right. If user enters ‘0’, then the program should exit. Make sure that the character ‘X’ is not able to move outside the limits.

    The sample run of the program at the start is:
    . . . . .
    . . . . .
    . . X . .
    . . . . .
    . . . . .
    Enter the direction:

    If user enters ‘w’, then ‘X’ should move up:
    . . . . .
    . . X . .
    . . . . .
    . . . . .
    . . . . .
    Enter the direction:

    If user enters ‘a’, then ‘X’ should move left:
    . . . . .
    . X . . .
    . . . . .
    . . . . .
    . . . . .
    Enter the direction:

    And so on…
    Make Sure that ‘X’ does not go outside the 5x5 limit
    Waiting for your reply ,
    thank you.

    ReplyDelete
    Replies
    1. @Salman

      your Program 1 number triangle C source code as:

      #include "stdio.h"
      int main()
      {
      int num=5,r,c,p;
      for(r=1; r<=num; r++)
      {
      for(c=1,p=r; c<=num; c++)
      {
      printf(" %d ", p);
      p=p*r;
      }
      printf("\n");
      }
      return 0;
      }

      Delete
    2. thanks plz tell me solution of 2nd program

      Delete
    3. Bro kindly upload the solution of 2nd program.

      Delete
    4. not yet got my program kindly tell me if cannot solve it

      Delete