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.
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;
}
}
}
Output
Related Questions
Write a program in Java to enter a natural number, where N>100 and N<1000, the natural number must not contain zeros. Print all the combinations of digits of the number including the number itself. Each new combination should appear on a new line. The program displays a message "Invalid Number", if N <100 or contains zeros.
Sample Input: Enter a number: 465
Sample Output:
456
465
546
564
645
654Sample Input: Enter a number: -712
Sample Output: Invalid NumberSample Input: Enter a number: 960
Sample Output: Invalid NumberA unique-digit integer is a positive integer (without leading zeros) with no duplicates digits. For example 7, 135, 214 are all unique-digit integers whereas 33, 3121, 300 are not. Given two positive integers m and n, where m < n, write a program to determine how many unique-digit integers are there in the range between m and n (both inclusive) and output them. The input contains two positive integers m and n. Assume m < 30000 and n < 30000. You are to output the number of unique-digit integers in the specified range along with their values in the format specified below:
Sample Input:
m = 100
n = 120
Sample Output:
The Unique-Digit integers are:
102, 103, 104, 105, 106, 107, 108, 109, 120.
Frequency of unique-digit integers is : 9Sample Input:
m = 2500
n = 2550
Sample Output:
The Unique-Digit integers are:
2501, 2503, 2504, 2506, 2507, 2508, 2509, 2510, 2513, 2514, 2516, 2517, 2518, 2517, 2530, 2519, 2530, 2531, 2534, 2536, 2537, 2538, 2539, 2540, 2541, 2543, 2546, 2547, 2548, 2549.
Frequency of unique-digit integers is: 28.A Smith number is a composite number, whose sum of the digits is equal to the sum of its prime factors. For example:
4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers.Write a program in Java to enter a number and check whether it is a Smith number or not.
Sample Input: 666
Sum of the digits: 6 + 6 + 6 = 18
Prime factors are: 2, 3, 3, 37
Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18
Thus, 666 is a Smith Number.Write a menu driven program using switch case statement to perform the given tasks as stated.
Task 1:
A Fibonacci series is given as:
0, 1, 1, 2, 3, 5, 8, 13, ………. and so on.
Display all the prime Fibonacci numbers in the range from 1 to 1000.
For example, 2, 3, 5, 13 are prime fibonacci numbers.
Task 2:
Prime factors are the factors of a number which are prime numbers. Display all the prime factors of a number.
Sample Input: 24
Sample Output: The prime factors of 24 are: 2, 2, 2, 3