Programa para incrementar/decrementar patrón de triángulo

  • Patrón de triángulo incremental numérico 
    Escriba un programa para imprimir un patrón de triángulo incremental numérico comenzando con un número dado N.
    Ejemplos: 
     
Input : 3
Output : 3 
         4 5 
         6 7 8 
         4 5 
         3 

A continuación se muestra la implementación del problema mencionado anteriormente. 
 

C++

#include <iostream>
using namespace std;
int main()
{
    int i, j, r, N, count;
    N = 3; // initializing N
    N--;
    r = 5;
    for (i = 0; i < r; i++) {
        if (i <= r / 2) {
            count = N + 1;
            for (j = 0; j <= i; j++) {
                N++;
                cout << N << " ";
            }
            cout << endl;
        }
        else {
            N = count - (r - i);
            count = N;
            for (j = i; j < r; j++) {
                cout << N << " ";
                N++;
            }
            cout << endl;
        }
    }
}

Java

// Java Program for incrementing
// or decrementing triangle pattern
import java.io.*;
 
class GFG
{
public static void main(String args[])
{
    int i, j, r, N, count = 0;
    N = 3; // initializing N
    N--;
    r = 5;
    for (i = 0; i < r; i++)
    {
        if (i <= r / 2)
        {
            count = N + 1;
            for (j = 0; j <= i; j++)
            {
                N++;
                System.out.print(N + " ");
            }
            System.out.println();
        }
        else
        {
            N = count - (r - i);
            count = N;
            for (j = i; j < r; j++)
            {
                System.out.print(N + " ");
                N++;
            }
            System.out.println();
        }
    }
}
}
 
// This code is contributed
// by Subhadeep Gupta

Python 3

# python 3 program to print incrementing
# and decrementing triangle pattern
if __name__ == "__main__":
 
    N = 3 # initializing N
    N -= 1
    r = 5
    for i in range( r):
        if i <= r // 2:
            count = N + 1
            for j in range(i + 1):
                N += 1
                print(str(N), end = " ")
             
            print()
         
        else :
            N = count - (r - i)
            count = N
            for j in range(i, r):
                print(str(N), end = " ")
                N += 1
             
            print()
 
# This code is contributed
# by ChitraNayal

C#

// C# Program for incrementing
// or decrementing triangle pattern
using System;
 
class GFG
{
public static void Main()
{
    int i, j, r, N, count = 0;
    N = 3; // initializing N
    N--;
    r = 5;
    for (i = 0; i < r; i++)
    {
        if (i <= r / 2)
        {
            count = N + 1;
            for (j = 0; j <= i; j++)
            {
                N++;
                Console.Write(N + " ");
            }
            Console.Write("\n");
        }
        else
        {
            N = count - (r - i);
            count = N;
            for (j = i; j < r; j++)
            {
                Console.Write(N + " ");
                N++;
            }
            Console.Write("\n");
        }
    }
}
}
 
// This code is contributed
// by ChitraNayal

PHP

<?php
// PHP program for incrementing
// or decrementing triangle pattern
$N = 3; // initializing N
$N--;
$r = 5;
for ($i = 0; $i < $r; $i++)
{
if ($i <= $r / 2)
{
    $count = $N + 1;
    for ($j = 0; $j <= $i; $j++)
    {
        $N++;
        echo $N . " ";
    }
    echo "\n";
}
else
{
    $N = $count - ($r - $i);
    $count = $N;
    for ($j = $i; $j < $r; $j++)
    {
        echo $N . " ";
        $N++;
    }
    echo "\n";
}
}
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
      var i, j, r, N, count;
      N = 3; // initializing N
      N--;
      r = 5;
      for (i = 0; i < r; i++) {
        if (i <= r / 2) {
          count = N + 1;
          for (j = 0; j <= i; j++) {
            N++;
            document.write(N + "  ");
          }
          document.write("<br>");
        } else {
          N = count - (r - i);
          count = N;
          for (j = i; j < r; j++) {
            document.write(N + "  ");
            N++;
          }
          document.write("<br>");
        }
      }
    </script>
Producción: 

3 
4 5 
6 7 8 
4 5 
3

 

  • Patrón de triángulo numérico decreciente 
    Escriba un programa para imprimir un patrón de triángulo numérico decreciente que comience con un número dado N.
    Ejemplos: 
     
Input : 3
Output : 3 
         5 4 
         8 7 6 
         5 4 
         3 

A continuación se muestra la implementación del problema mencionado anteriormente. 
 

C++

#include <iostream>
using namespace std;
int main()
{
    int i, j, r, N, N1;
    N1 = 3;
    N = 0;
    r = 5;
    for (i = 0; i < r; i++) {
        if (i <= r / 2) {
            N = N1;
            for (j = 0; j <= i; j++) {
                N++;
            }
            N1 = N;
            for (j = 0; j <= i; j++) {
                N--;
                cout << N << " ";
            }
 
            cout << endl;
        }
        else {
            for (j = i; j < r; j++) {
                N--;
                cout << N << " ";
            }
            cout << endl;
        }
    }
}

Java

// Java program for incrementing
// and decrementing triangle pattern
class GFG
{
public static void main(String[] args)
{
    int i, j, r, N, N1;
    N1 = 3;
    N = 0;
    r = 5;
    for (i = 0; i < r; i++)
    {
        if (i <= r / 2)
        {
            N = N1;
            for (j = 0; j <= i; j++)
            {
                N++;
            }
            N1 = N;
            for (j = 0; j <= i; j++)
            {
                N--;
                System.out.print(N + " ");
            }
 
            System.out.println();
        }
        else
        {
            for (j = i; j < r; j++)
            {
                N--;
                System.out.print(N + " ");
            }
            System.out.println();
        }
    }
}};
 
// This code is contributed
// by ChitraNayal

Python 3

# python 3 program for incrementing
# and decrementing triangle pattern
if __name__ == "__main__":
 
    N1 = 3
    N = 0;
    r = 5;
    for i in range( r):
        if i <= r // 2:
            N = N1
            for j in range(i + 1):
                N += 1
             
            N1 = N
            for j in range(i + 1):
                N -= 1
                print(N, end = " ")
 
            print()
         
        else :
            for j in range(i, r):
                N -= 1
                print(N, end = " ")
             
            print()
 
# This code is contributed
# by ChitraNayal

C#

// C# program for incrementing
// and decrementing triangle pattern
using System;
 
class GFG
{
public static void Main()
{
    int i, j, r, N, N1;
    N1 = 3;
    N = 0;
    r = 5;
    for (i = 0; i < r; i++)
    {
        if (i <= r / 2)
        {
            N = N1;
            for (j = 0; j <= i; j++)
            {
                N++;
            }
            N1 = N;
            for (j = 0; j <= i; j++)
            {
                N--;
                Console.Write(N + " ");
            }
 
            Console.Write("\n");
        }
        else
        {
            for (j = i; j < r; j++)
            {
                N--;
                Console.Write(N + " ");
            }
            Console.Write("\n");
        }
    }
}};
 
// This code is contributed
// by ChitraNayal

PHP

<?php
// PHP program for incrementing
// and decrementing triangle pattern
$N1 = 3;
$N = 0;
$r = 5;
for ($i = 0; $i < $r; $i++)
{
    if ($i <= $r / 2)
    {
        $N = $N1;
        for ($j = 0; $j <= $i; $j++)
        {
            $N++;
        }
        $N1 = $N;
        for ($j = 0; $j <= $i; $j++)
        {
            $N--;
            echo $N ." ";
        }
 
        echo "\n";
    }
    else
    {
    for ($j = $i; $j < $r; $j++)
    {
        $N--;
        echo $N ." ";
    }
    echo "\n";
    }
}
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
// Javascript program for incrementing
// and decrementing triangle pattern
 
let i, j, r, N, N1;
 N1 = 3;
    N = 0;
    r = 5;
    for (i = 0; i < r; i++)
    {
        if (i <= r / 2)
        {
            N = N1;
            for (j = 0; j <= i; j++)
            {
                N++;
            }
            N1 = N;
            for (j = 0; j <= i; j++)
            {
                N--;
                document.write(N + " ");
            }
  
            document.write("<br>");
        }
        else
        {
            for (j = i; j < r; j++)
            {
                N--;
                document.write(N + " ");
            }
            document.write("<br>");
        }
    }
 
 
// This code is contributed by avanitrachhadiya2155
</script>
  •  
Producción: 

3 
5 4 
8 7 6 
5 4 
3

 

Publicación traducida automáticamente

Artículo escrito por Smitha Dinesh Semwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *