Template 2
Template 2
Template 2
class template2
{
public static void main(String[] args) {
for(int i=1; i<=10; i++){
System.out.println(i);
}
}
}
Output:
Question 2
Write a program to calculate the sum of first 10 natural number.
Code:
class template2
{
public static void main(String[] args) {
int sum = 0;
for(int i=1; i<=10; i++){
sum = sum+i;
}
System.out.println("Sum of first 10 numbers: "+sum);
}
}
Output:
Question 3
import java.util.Scanner;
public class template2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = sc.nextInt();
sc.close();
}
}
Output:
Question 4
Write a program to find the factorial value of any number entered through the
keyboard.
Code:
import java.util.Scanner;
class template2
{
public static void main(String[] args) {
Question 5
Two numbers are entered through the keyboard. Write a program to find the value of
one number raised to the power of another. (Do not use Java built-in method)
Code:
import java.util.Scanner;
class template
{
public static void main(String[] args) {
int result = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number1: ");
int base = sc.nextInt();
System.out.println("Enter a number2: ");
int exponent = sc.nextInt();
for (int i=1; i<=exponent;i++){
result *=base;
}
System.out.println(result);
sc.close();
}
}
Output:
Question 6
import java.util.Scanner;
class template2 {
public static void main(String[] args) {
int reversed = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = sc.nextInt();
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed number: " + reversed);
sc.close();
}
}
Output:
Question 7
Write a program that reads a set of integers, and then prints the sum of the even and
odd integers.
Code:
import java.util.Scanner;
class template2
{
public static void main(String[] args) {
int even = 0,odd = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter numbers of integers: ");
int count = sc.nextInt();
Question 8
Write a program that prompts the user to input a positive integer. It should then
output a message indicating whether the number is a prime number.
Code:
import java.util.Scanner;
class template2
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number to find number is prime or not: ");
int num = sc.nextInt();
if(isPrime) {
System.out.println(num + " is a prime Number");
}else {
System.out.println(num + " is not a prime Number");
}
sc.close();
}
}
Output:
Question 9
Write a program to calculate HCF of Two given number.
Code:
import java.util.Scanner;
class template2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.close();
}
}
Output:
Question 10
Write a do-while loop that asks the user to enter two numbers. The numbers should
be added and the sum displayed. The loop should ask the user whether he or she
wishes to perform the operation again. If so, the loop should repeat; otherwise it
should terminate.
Code:
import java.util.Scanner;
class template2
{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
do{
System.out.println("Enter the first number ");
int num1 = sc.nextInt();
if(choice !=1){
break;
}
}while(true);
sc.close();
}
}
Output:
class template2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int positive = 0;
int negative = 0;
int zero = 0;
char choice;
do{
System.out.println("Enter a number: ");
int num = sc.nextInt();
if(num > 0 ){
positive++;
} else if (num < 0) {
sc.close();
}
}
Output:
Question 12
import java.util.Scanner;
class template2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char choice;
do {
System.out.print("Enter a number: ");
int number = sc.nextInt();
sc.close();
}
}
Output:
class template2 {
public static void main(String[] args) {
int startRange = 1;
int endRange = 500;
int armstrongSum = 0;
temp = num;
while (temp > 0) {
int digit = temp % 10;
int power = 1;
for (int i = 0; i < numDigits; i++) {
power *= digit;
}
armstrongSum += power;
temp /= 10;
}
Question 14
Write a program to print Fibonacci series of n terms where n is input by user:
0112358 13 24.....
Code:
import java.util.Scanner;
class template2
{
public static void main(String[] args)
{
int n, firstTerm = 0, secondTerm = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your number: ");
n = sc.nextInt();
System.out.println("Fibonacci Series till " + n + " terms: ");
for (int i = 1; i <= n; ++i) {
System.out.print(firstTerm +",");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Output:
Question 15
Write a program to calculate the sum of following series where n is input by user.
1+1/2+1/3+ 1/4 + 1/5+.............1/n
Code:
import java.util.Scanner;
class template2
Question 16
Compute the natural logarithm of 2, by adding up to n terms in the series 1-1/2 + 1/3-
1/4+1/5-... 1/n
where n is a positive integer and input by user
Code:
import java.util.Scanner;
class template2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.close();
}
}