Number based Java Programs
Number based Java Programs
25.Program to find the sum of first N natural numbers using the formula: Sum=(N×(N+1))/2
import java.util.Scanner;
26.Program to find the number of digits in the factorial of a number without calculating the
factorial:
import java.util.Scanner;
28.Program to find the sum of digits of a given number until it becomes a single-digit number:
import java.util.Scanner;
33.Program to find the digital root of a number (repeatedly sum digits until single digit):
import java.util.Scanner;
38.Program to find the largest palindrome made from the product of two n-digit numbers:
import java.util.Scanner;
39.Program to find the number of steps required to reach 1 in the Collatz sequence:
import java.util.Scanner;
59.Program to find the sum of the series: 1 + 1/2 + 1/3 + ... + 1/N:
import java.util.Scanner;
60.Program to find the sum of the series: 1 - 1/2 + 1/3 - ... + (-1)^(N+1) * 1/N:
import java.util.Scanner;
61.Program to find the sum of the series: 1^2 + 2^2 + 3^2 + ... + N^2:
import java.util.Scanner;
62. Program to find the sum of digits of a number raised to the power of itself:
import java.util.Scanner;
63.Program to find the sum of even digits and odd digits of a number separately:
import java.util.Scanner;
70.Program to find the nth term of the Fibonacci series using recursion:
import java.util.Scanner;
71.Program to find the sum of the series: 1 + 1/3 + 1/5 + ... + 1/(2n-1):
import java.util.Scanner;
80.Program to find the sum of the series: 1/1! + 2/2! + 3/3! + ... + N/N!:
import java.util.Scanner;
public class SeriesSumFactorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
double sum = seriesSumFactorial(n);
System.out.println("Sum of the series is: " + sum);
}
}
}
}
static boolean prime(int n)
{
// TODO code application logic here
int i = 2;
boolean flag = true;
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
return flag;
}
}
}
}
.concat(processor.getName(decimalValue)).concat(SEPARATOR).concat(SCALE.getName(-dec
imalValue.length()));
}
return name;
}
}
static public AbstractProcessor processor;
public static void main(String...args)
{
processor = new DefaultProcessor();
long[] values = new long[]
{
0,4,10,12,100,108,299,1000,1003,2040,45213,100000,100005,100010,202020,202022,999999,
1000000,1000001,10000000,10000007,99999999,
Long.MAX_VALUE,
Long.MIN_VALUE
};
String[] strValues = new String[]
{
"0001.2","3.141592"
};
for (long val : values)
{
System.out.println(val + " = " + processor.getName(val) );
}
for (String strVal : strValues)
{
System.out.println(strVal + " = " + processor.getName(strVal) );
}
//generates a very large number
StringBuilder bigNumber = new StringBuilder();
for (int d=0; d<66; d++)
{
bigNumber.append( (char) ((Math.random() * 10) + '0'));
}
bigNumber.append(".");
for (int d=0; d<26; d++)
{
bigNumber.append( (char) ((Math.random() * 10) + '0'));
}
System.out.println(bigNumber.toString() + " = " + processor.getName(bigNumber.toString()));
}
}
while(true)
{
System.out.println("Automated Teller Machine");
System.out.println("Choose 1 for Withdraw");
System.out.println("Choose 2 for Deposit");
System.out.println("Choose 3 for Check Balance");
System.out.println("Choose 4 for EXIT");
System.out.print("Choose the operation you want to perform:");
//check whether the balance is greater than or equal to the withdrawal amount
if(balance >= withdraw)
{
//remove the withdrawl amount from the total balance
balance = balance - withdraw;
System.out.println("Please collect your money");
}
else
{
//show custom error message
System.out.println("Insufficient Balance");
}
System.out.println("");
break;
case 2:
case 3:
//displaying the total balance of the user
System.out.println("Balance : "+balance);
System.out.println("");
break;
case 4:
//exit from the menu
System.exit(0);
}
}
}
}
//create ISBNNumberExample class to check whether the given number is a valid ISBN or not
class ISBNNumberExample {
strNumber = ""+number;
if (strNumber.length() != 10) {
return false;
}
if ((sum % 11) == 0) {
return true;
}
return false;
try {
if (checkISBNNumber(n1))
System.out.println(n1 + " is a valid ISBN number");
else
System.out.println(n1 + " is not a valid ISBN number");
if (checkISBNNumber(n2))
System.out.println(n2 + " is a a valid ISBN number");
else
System.out.println(n2 + " is not a valid ISBN number");
}catch(java.lang.Exception e) {
System.out.println("Error while reading the data.");
}
}
}
// create checkNumber() method that returns true when it founds number krishnamurthy
static boolean checkNumber(int number)
{
int sum = 0; //initialize sum to 0
// Check whether the number is equal to the sum or not. If both are equal, number is
krishnamurthy number
if(sum == number)
return true;
else
return false;
}
//custom message
System.out.println("Enter any number:");
if (checkNumber(n))
System.out.println(n + " is a krishnamurthy number");
else
System.out.println(n + "is not a krishnamurthy number");
}
}
for(char c : str1.toCharArray()){
sum_num += Math.pow(Character.digit(c, 10), x);
}
return sum_num == n;
}
126.Write a Java program to print out the first 10 Catalan numbers by extracting them from
Pascal's triangle.
import java.util.Scanner;
public class Example8 {
public static void main(String[] args) {
int num = 10;
int[] t = new int[num + 2];
t[1] = 1;
System.out.printf("\nList 10 Catalan numbers:-\n");
for (int i = 1; i <= num; i++) {
t[i + 1] = t[i];