Printing a pattern of numbers.

New to FreeBASIC? Post your questions here.
Post Reply
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Printing a pattern of numbers.

Post by ganache »

Hi, i am trying to translate this c code.

Code: Select all

#include<stdio.h>
main()
{
      int n, c, d, num = 1, space;
      printf ("Enter value of n");
      scanf("%d",&n);
 
      space = n - 1;
      for ( d = 1 ; d <= n ; d++ )
      {
          num = d;
          for ( c = 1 ; c <= space ; c++ )
              printf(" ");
          space--;
          for ( c = 1 ; c <= d ; c++ )
          {
              printf("%d", num);
              num++;
          }
          num--;
          num--;
          for ( c = 1 ; c < d ; c++)
          {
              printf("%d", num);
              num--;
          }
          printf("\n");
      }
 
      return 0;
}
My FB attempt is pasted here.

Code: Select all

 Dim As Integer n, c, d, num = 1, gap
 input "Enter value of n", n
 gap = n - 1
 for d = 1 to n
          num = d
 for c = 1 to gap
           print " ";
 gap-=1
 next c
 for  c =1 to d
           print num;
 num+=1
 num-=1
 num-=1
next c
for  c = 1 to d
          print  num;
 num-=1
          print " ";
next c
next d
Sleep
End
BasicCoder2
Posts: 3915
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Printing a pattern of numbers.

Post by BasicCoder2 »

A common type of problem type I have seen given to beginners learning C.

Code: Select all

      dim as integer n, c, d, num = 1, blank
      
      input "Enter value of n";n
 
      blank = n - 1
      
      for  d = 1 to n
          
          num = d
          
          for c = 1 to blank
              print "  ";
          next c
          
          blank = blank-1
          
          for  c = 1 to d
              print num;
              num = num + 1
          next c
          
          num = num - 2
          
          for  c = 1 to d-1
              print num;
              num = num - 1
          next c
          
          print
          
      next d
      
 sleep
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Re: Printing a pattern of numbers.

Post by ganache »

Thank you Basic Coder2.
The resulting triangle looks fine when the value of n is 5. If n is larger then the output is not aligned properly
(due to double digit numbers). So i removed the input line.
BasicCoder2
Posts: 3915
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Printing a pattern of numbers.

Post by BasicCoder2 »

Solution to that might be to check the input.
Validation of input is good programming practice anyway.
As for the limit of 5 you can increase that by adding spaces.
Programming comes down to problem solving.
It is also useful if you know how a program works, that is solve the problem yourself or at least understand the solution and then translating from one language to another becomes easier with the option to take advantage of each languages' strengths. It becomes more of a top down translation rather than sideways translation. Using code without understanding is also ok but then it needs to be in a library and in a form that can be called from any language.

The highest value will be n*2-1

Code: Select all

      dim as integer n, c, d, num = 1, blank
      
      input "Enter value between 1 and 12";n
      while n<1 or n>12
          input "Invalid input. Enter a value between 1 and 12";n
      wend
 
      blank = n - 1
      
      for  d = 1 to n
          
          num = d
          
          for c = 1 to blank
              print "   ";
          next c
          
          blank = blank-1
          
          for  c = 1 to d
              if num<10 then print " ";
              print num;
              num = num + 1
          next c
          
          num = num - 2
          
          for  c = 1 to d-1
              if num<10 then print " ";
              print num;
              num = num - 1
          next c
          
          print
          
      next d
      
 sleep
 
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Re: Printing a pattern of numbers.

Post by ganache »

Many thanks for the tips.
Post Reply