Sum Pattern In JAVA
Write a program to print triangle of user defined integers sum.
if input 4
output should :
1=1
1+2=3
1+2+3=6
1+2+3+4=10
Answer The Program
Answers
Answer:
KnowledgeBoat Logo
Computer Science
A triangular number is formed by the addition of consecutive integers starting with 1. For example,
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15
Thus, 3, 6, 10, 15, are triangular numbers.
Write a program in Java to display all the triangular numbers from 3 to n, taking the value of n as an input.
Java
Java Iterative Stmts
ICSE
8 Likes
ANSWER
import java.util.Scanner;
public class KboatTriangularNumbers
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();
if (n < 3) {
System.out.println("Value of n should be greater than or equal to 3");
return;
}
System.out.println("Triangular Numbers from 3 to "
+ n + ":");
int sum = 3; //First Triangular Number
for (int i = 3; sum <= n; i++) {
System.out.println(sum);
sum += i;
}
}
}
Explanation:
i hope you helpful
Answer:
It's Pretty Simple you will understand.
just used if,else and while loop concept.
Explanation:
import java.util.Scanner;
public class SumPattern {
public static void main(String[] args){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int i=1;
int sum=i;
while(i<=n){
int t=1; //starting number
int j=1;
while(j<=i){
if(t<i){
System.out.print(t+"+");
t++;
j++;
}
else{
System.out.print(t+"="+sum);
j++;
}
}
System.out.println();
i++;
sum=sum+i;
}
}
}
Mark Me as Brainliest. Thank you!!