Write a Java program that prints a sentence.
public class Hello_World {
public static void main (String[] args) {
System.out.println("Hello, world!");
}
}
Write a program which computes the factorial of an input integer that is >=0 and <=12.
// The Program computes the factorial of an input integer >=0 and <=12
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
int number, factorial = 1;
System.out.println("Input A Number >= 0 and <=12");
Scanner scan = new Scanner(System.in);
number = scan.nextInt(); // User Input Value
for (int i = 1; i <=number; i++) {
factorial = factorial * i; // OR factorial *= i;
}
System.out.println( "Factorial of " + number + " = " + factorial);
}
}
Input an integer that is >=0 and <=12
> 0
The factorial of 0 is 1
Input an integer that is >=0 and <=12
> 5
The factorial of 5 is 120
Input an integer that is >=0 and <=12
> 7
The factorial of 7 is 5040
Input an integer that is >=0 and <=12
> 10
The factorial of 10 is 3628800
In 1994, Mexico’s population was 58 million and growing at the annual rate of 7%. The United States’ population in the same year was 260 million and growing at the annual rate of 2%. If these two countries were to maintain the current rates of growth, in how many years will Mexico’s population exceed that of the United States. (Answer: 32 years)
// Population Growth – Mexico Vs USA
import java.text.DecimalFormat;
public class Population {
public static void main(String[] args) {
double usa = 260, mexico = 58;
int time = 0;
DecimalFormat df = new DecimalFormat("0.000");
while (mexico <= usa) {
usa *= 1.02;
mexico *=1.07;
time++;
System.out.println ("Year #" + time + "\tMexico = " +
df.format(mexico) + "\tUSA = " + df.format(usa));
}
System.out.println("Time in Years will be " + time);
}
}
Year# 1 Mexico = 62.060 USA = 265.200
Year# 2 Mexico = 66.404 USA = 270.504
Year# 3 Mexico = 71.052 USA = 275.914
Year# 4 Mexico = 76.026 USA = 281.432
Year# 5 Mexico = 81.348 USA = 287.061
Year# 6 Mexico = 87.042 USA = 292.802
Year# 7 Mexico = 93.135 USA = 298.658
Year# 8 Mexico = 99.655 USA = 304.631
Year# 9 Mexico = 106.631 USA = 310.724
Year# 10 Mexico = 114.095 USA = 316.939
Year# 11 Mexico = 122.081 USA = 323.277
Year# 12 Mexico = 130.627 USA = 329.743
Year# 13 Mexico = 139.771 USA = 336.338
Year# 14 Mexico = 149.555 USA = 343.064
Year# 15 Mexico = 160.024 USA = 349.926
Year# 16 Mexico = 171.225 USA = 356.924
Year# 17 Mexico = 183.211 USA = 364.063
Year# 18 Mexico = 196.036 USA = 371.344
Year# 19 Mexico = 209.759 USA = 378.771
Year# 20 Mexico = 224.442 USA = 386.346
Year# 21 Mexico = 240.153 USA = 394.073
Year# 22 Mexico = 256.963 USA = 401.955
Year# 23 Mexico = 274.951 USA = 409.994
Year# 24 Mexico = 294.197 USA = 418.194
Year# 25 Mexico = 314.791 USA = 426.558
Year# 26 Mexico = 336.826 USA = 435.089
Year# 27 Mexico = 360.404 USA = 443.790
Year# 28 Mexico = 385.633 USA = 452.666
Year# 29 Mexico = 412.627 USA = 461.720
Year# 30 Mexico = 441.511 USA = 470.954
Year# 31 Mexico = 472.417 USA = 480.373
Year# 32 Mexico = 505.486 USA = 489.981
Time in Years will be 32
Your program will take integer input values that are greater than or equal to 5. If the input value has 5 as its factor, print a line stating so (see example below). Further, compute the sum of input values which have 5 as their factor. You program will continue to process input values until you have encountered three input values which have 5 as their factor.
Finally, your program will print the sum of the input values which have 5 as their factor.
// Identifies three numbers in the input which have 5 as their factor and obtains their sum and terminates
import java.util.Scanner;
public class FactorsOf5Sum {
public static void main(String[] args) {
int sum = 0, count = 0, z;
System.out.println("Factors of 5 and their Sum");
Scanner m = new Scanner(System.in);
while (count < 3) {
System.out.println("Input Value");
z = m.nextInt();
if (z % 5 == 0) {
count++;
sum = sum + z;
System.out.println( z + " has 5 as its factor\n");
}
System.out.println();
}
System.out.println("Sum of all the numbers which had 5 as their factor is " +
sum);
}
}
Input Value
> 12
Input Value
> 44
Input Value
> 89
Input Value
> 90
90 has 5 as its factor
Input Value
> 10
10 has 5 as its factor
Input Value
> 66
Input Value
> 100
100 has 5 as its factor
Sum of all the numbers which had 5 as their factor is 200
Write a program which prints the digits of an input integer (> 0) in reverse.
// The Program prints an input integer in reverse.
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
int number, digit;
System.out.println("Input A Number Greater Than Zero:");
Scanner m = new Scanner(System.in);
number = m.nextInt(); // User Input Value
System.out.println("\nThe Reversed Number is:");
while (number != 0) {
digit = number % 10;
System.out.print(digit);
number = number / 10; // OR number /= 10;
}
}
}
Write a program to print the Fibonacci sequence as per the input.
import java.util.Scanner;
public class Fibonacci {
public static void main (String[] args) {
int a,b,c,count = 2;
System.out.println("How many Fibonacci Numbers do you need? ");
Scanner m = new Scanner(System.in);
int limit = m.nextInt();
a = 0;
b = 1;
if (limit == 1) {
System.out.println("0");
} else if (limit == 2) {
System.out.println("0" + " " + "1");
} else {
System.out.print(a + " " + b + " ");
while (count < limit) {
c = a + b;
a = b;
b = c;
count++;
System.out.print(c + " " );
}
}
}
}