Dudley triangle
Here given code implementation process.
// C Program for
// Dudley triangle
#include <stdio.h>
void dudleyTriangle(int n)
{
if (n <= 0)
{
return;
}
int ans = 0;
for (int r = 1; r <= n; ++r)
{
for (int j = 1; j <= r; ++j)
{
// Calculate dudley triangle
ans = j *(j + 1) % (r + 2);
// Display calculated result
printf(" %d", ans);
}
// include new line
printf("\n");
}
}
int main()
{
/*
n = 10 [row]
----------
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
*/
dudleyTriangle(10);
return 0;
}
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
// Java program for
// Dudley Triangle
public class Triangle
{
public void dudleyTriangle(int n)
{
if (n <= 0)
{
return;
}
int ans = 0;
for (int r = 1; r <= n; ++r)
{
for (int j = 1; j <= r; ++j)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
System.out.print(" " + ans);
}
// include new line
System.out.print("\n");
}
}
public static void main(String[] args)
{
Triangle task = new Triangle();
/*
n = 10 [row]
----------
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
*/
task.dudleyTriangle(10);
}
}
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Dudley Triangle
class Triangle
{
public: void dudleyTriangle(int n)
{
if (n <= 0)
{
return;
}
int ans = 0;
for (int r = 1; r <= n; ++r)
{
for (int j = 1; j <= r; ++j)
{
// Calculate dudley triangle
ans = j *(j + 1) % (r + 2);
// Display calculated result
cout << " " << ans;
}
// include new line
cout << "\n";
}
}
};
int main()
{
Triangle *task = new Triangle();
/*
n = 10 [row]
----------
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
*/
task->dudleyTriangle(10);
return 0;
}
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
// Include namespace system
using System;
// Csharp program for
// Dudley Triangle
public class Triangle
{
public void dudleyTriangle(int n)
{
if (n <= 0)
{
return;
}
int ans = 0;
for (int r = 1; r <= n; ++r)
{
for (int j = 1; j <= r; ++j)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
Console.Write(" " + ans);
}
// include new line
Console.Write("\n");
}
}
public static void Main(String[] args)
{
Triangle task = new Triangle();
/*
n = 10 [row]
----------
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
*/
task.dudleyTriangle(10);
}
}
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
package main
import "fmt"
// Go program for
// Dudley Triangle
func dudleyTriangle(n int) {
if n <= 0 {
return
}
var ans int = 0
for r := 1 ; r <= n ; r++ {
for j := 1 ; j <= r ; j++ {
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2)
// Display calculated result
fmt.Print(" ", ans)
}
// include new line
fmt.Print("\n")
}
}
func main() {
/*
n = 10 [row]
----------
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
*/
dudleyTriangle(10)
}
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
<?php
// Php program for
// Dudley Triangle
class Triangle
{
public function dudleyTriangle($n)
{
if ($n <= 0)
{
return;
}
$ans = 0;
for ($r = 1; $r <= $n; ++$r)
{
for ($j = 1; $j <= $r; ++$j)
{
// Calculate dudley triangle
$ans = $j * ($j + 1) % ($r + 2);
// Display calculated result
echo(" ".$ans);
}
// include new line
echo("\n");
}
}
}
function main()
{
$task = new Triangle();
/*
n = 10 [row]
----------
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
*/
$task->dudleyTriangle(10);
}
main();
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
// Node JS program for
// Dudley Triangle
class Triangle
{
dudleyTriangle(n)
{
if (n <= 0)
{
return;
}
var ans = 0;
for (var r = 1; r <= n; ++r)
{
for (var j = 1; j <= r; ++j)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
process.stdout.write(" " + ans);
}
// include new line
process.stdout.write("\n");
}
}
}
function main()
{
var task = new Triangle();
/*
n = 10 [row]
----------
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
*/
task.dudleyTriangle(10);
}
main();
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
# Python 3 program for
# Dudley Triangle
class Triangle :
def dudleyTriangle(self, n) :
if (n <= 0) :
return
ans = 0
r = 1
while (r <= n) :
j = 1
while (j <= r) :
# Calculate dudley triangle
ans = j * (j + 1) % (r + 2)
# Display calculated result
print(" ", ans, end = "")
j += 1
# include new line
print(end = "\n")
r += 1
def main() :
task = Triangle()
# n = 10 [row]
# ----------
# 2
# 2 2
# 2 1 2
# 2 0 0 2
# 2 6 5 6 2
# 2 6 4 4 6 2
# 2 6 3 2 3 6 2
# 2 6 2 0 0 2 6 2
# 2 6 1 9 8 9 1 6 2
# 2 6 0 8 6 6 8 0 6 2
task.dudleyTriangle(10)
if __name__ == "__main__": main()
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
# Ruby program for
# Dudley Triangle
class Triangle
def dudleyTriangle(n)
if (n <= 0)
return
end
ans = 0
r = 1
while (r <= n)
j = 1
while (j <= r)
# Calculate dudley triangle
ans = j * (j + 1) % (r + 2)
# Display calculated result
print(" ", ans)
j += 1
end
# include new line
print("\n")
r += 1
end
end
end
def main()
task = Triangle.new()
# n = 10 [row]
# ----------
# 2
# 2 2
# 2 1 2
# 2 0 0 2
# 2 6 5 6 2
# 2 6 4 4 6 2
# 2 6 3 2 3 6 2
# 2 6 2 0 0 2 6 2
# 2 6 1 9 8 9 1 6 2
# 2 6 0 8 6 6 8 0 6 2
task.dudleyTriangle(10)
end
main()
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
// Scala program for
// Dudley Triangle
class Triangle()
{
def dudleyTriangle(n: Int): Unit = {
if (n <= 0)
{
return;
}
var ans: Int = 0;
var r: Int = 1;
while (r <= n)
{
var j: Int = 1;
while (j <= r)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
print(" " + ans);
j += 1;
}
// include new line
print("\n");
r += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Triangle = new Triangle();
/*
n = 10 [row]
----------
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
*/
task.dudleyTriangle(10);
}
}
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
// Swift 4 program for
// Dudley Triangle
class Triangle
{
func dudleyTriangle(_ n: Int)
{
if (n <= 0)
{
return;
}
var ans: Int = 0;
var r: Int = 1;
while (r <= n)
{
var j: Int = 1;
while (j <= r)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
print(" ", ans, terminator: "");
j += 1;
}
// include new line
print(terminator: "\n");
r += 1;
}
}
}
func main()
{
let task: Triangle = Triangle();
/*
n = 10 [row]
----------
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
*/
task.dudleyTriangle(10);
}
main();
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
// Kotlin program for
// Dudley Triangle
class Triangle
{
fun dudleyTriangle(n: Int): Unit
{
if (n <= 0)
{
return;
}
var ans: Int ;
var r: Int = 1;
while (r <= n)
{
var j: Int = 1;
while (j <= r)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
print(" " + ans);
j += 1;
}
// include new line
print("\n");
r += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Triangle = Triangle();
/*
n = 10 [row]
----------
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
*/
task.dudleyTriangle(10);
}
Output
2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment