C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Write a program to print the following pattern on the console.
0 909 89098 7890987 678909876 56789098765 4567890987654 345678909876543 23456789098765432 1234567890987654321 Algorithm
Solution:C Program:
#include <stdio.h>
int main()
{
int lines=10;
int i,j;
int count;
for(i=1;i<=lines;i++){// this loop is used to print lines
count=0;
for(j=1;j<lines;j++){// this loop is used to print numbers in a line
if(count<i-1){
if(!(j<lines-i+1)){
printf("%d",j);
count++;
}
}
}
printf("0");
count=0;
for(--j;j>=1;j--){// this loop is used to print j and increment count
if(count<i-1){
printf("%d",j);
count++;
}
}
printf("\n");
}
return 0;
}
Output: 0 909 89098 7890987 678909876 56789098765 4567890987654 345678909876543 23456789098765432 1234567890987654321 Java Program:
public class patter{
public static void main(String []args){
int lines=10;
int i,j;
int count;
for(i=1;i<=lines;i++){// this loop is used to print lines
count=0;
for(j=1;j<lines;j++){// this loop is used to print numbers in a line
if(count<i-1){
if(!(j<lines-i+1)){
System.out.print(j);
count++;
}
}
}
System.out.print("0");
count=0;
for(--j;j>=1;j--){// this loop is used to print j and increment count
if(count<i-1){
System.out.print(j);
count++;
}
}
System.out.println("");
}
}
}
Output: 0 909 89098 7890987 678909876 56789098765 4567890987654 345678909876543 23456789098765432 1234567890987654321 C# Program:
using System;
public class Program
{
public static void Main()
{
int lines=10;
int i,j;
int count;
for(i=1;i<=lines;i++){// this loop is used to print lines
count=0;
for(j=1;j<lines;j++){// this loop is used to print numbers in a line
if(count<i-1){
if(!(j<lines-i+1)){
Console.Write(j);
count++;
}
}
}
Console.Write("0");
count=0;
for(--j;j>=1;j--){// this loop is used to print j and increment count
if(count<i-1){
Console.Write(j);
count++;
}
}
Console.WriteLine("");
}
}
}
Output: 0 909 89098 7890987 678909876 56789098765 4567890987654 345678909876543 23456789098765432 1234567890987654321 PHP Program:
$lines=10;
$i;
$j;
$count;
for($i=1;$i<=$lines;$i++){// this loop is used to print lines
$count=0;
for($j=1;$j<$lines;$j++){// this loop is used to print numbers in line
if($count<$i-1){
if(!($j<$lines-$i+1)){
echo $j;
$count++;
}
}
}
echo '0';
$count=0;
for(--$j;$j>=1;$j--){//this loop is used to print j and increment count
if($count<$i-1){
echo $j;
$count++;
}
}
echo "<br>";
}
Output: 0 909 89098 7890987 678909876 56789098765 4567890987654 345678909876543 23456789098765432 1234567890987654321 Python Program:
lines=10
i=1
j=1
count=0
while i<=lines: #this loop is used to print lines
count=0
j=1
while j<lines: # this loop is used to priont numbers in a line
if count<i-1:
if j<lines-i+1:
count=count
else :
print(j, end='', flush=True)
count=count+1
j=j+1
print("0", end='', flush=True)
count=0
j=j-1
while j>=1: #this loop is used to print j and increment count
if count<i-1:
print(j, end='', flush=True)
count=count+1
j=j-1
print("")
i=i+1
Output: 0 909 89098 7890987 678909876 56789098765 4567890987654 345678909876543 23456789098765432 1234567890987654321
Next TopicProgram to Print the Pattern 1
|