Template 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Question 1

Write a program to print numbers from 1 to 10.


Code:

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

DHRUTI PATEL 22C21058 Page no.: 1


Write a program that prompts the user to input a positive integer. It should then print
the multiplication table of that number.
Code:

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();

System.out.println("Multiplication table of "+num+" is: ");


for(int i = 1;i <= 10;i++){
int result = num*i;
System.out.println(num + " x "+i+" = "+ result);
}

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) {

DHRUTI PATEL 22C21058 Page no.: 2


int i,fact = 1;
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number to find a factorial: ");
int num= sc.nextInt();
for(i=1;i<=num;i++){
fact = fact*i;
}
System.out.println("Factorial of "+num+ " is "+ fact);
sc.close();
}
}
Output:

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

DHRUTI PATEL 22C21058 Page no.: 3


Write a program that prompts the user to input an integer and then outputs the
number with the digits reversed. For example, if the input is 12345, the output should
be 54321.
Code:

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();

for(int i=0; i < count; i++){


System.out.println("Enter an integer: ");
int num = sc.nextInt();
if(num % 2 == 0){
even += num;
} else{
odd += num;
}

DHRUTI PATEL 22C21058 Page no.: 4


}

System.out.println("Sum of even numbers: " +even);


System.out.println("Sum of odd numbers: " +odd);
sc.close();
}
}
Output:

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();

boolean isPrime = true;

if(num <= 1){

DHRUTI PATEL 22C21058 Page no.: 5


isPrime = false;
}else {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}

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);

System.out.print("Enter first number: ");


int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();

int hcf = 1,smaller;


if(num1 < num2){
smaller = num1;
}else{
smaller = num2;
}

for(int i=1; i <= smaller; i++){


if (num1 % i == 0 && num2 % i == 0)
{
hcf = i;
}
}

DHRUTI PATEL 22C21058 Page no.: 6


System.out.println("The hcf of " + num1 + " and " + num2 + " is " + hcf);

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();

System.out.println("Enter the second number ");


int num2 = sc.nextInt();

int sum = num1+num2;


System.out.println("The sum of " + num1+ " and " + num2 + " is: "+ sum);

System.out.println("Do you want to continue?(1 for yes, 0 for no): ");


int choice = sc.nextInt();

if(choice !=1){
break;
}
}while(true);
sc.close();
}
}

Output:

DHRUTI PATEL 22C21058 Page no.: 7


Question 11
Write a program to enter the numbers till the user wants and at the end it should
display the count of positive, negative and zeros entered.
Code:

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) {

DHRUTI PATEL 22C21058 Page no.: 8


negative++;
}else {
zero++;
}

System.out.println("Do you want to enter another number? (y/n): ");


choice = sc.next().charAt(0);
}while (choice == 'y' || choice =='Y');

System.out.println("Count of positive numbers: " + positive);


System.out.println("Count of negative numbers: " + negative);
System.out.println("Count of zero numbers: " + zero);

sc.close();
}
}
Output:

Question 12

DHRUTI PATEL 22C21058 Page no.: 9


Write a program to enter the numbers till the user wants and at the end the program
should display the largest and smallest numbers entered.
Code:

import java.util.Scanner;

class template2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int largest = Integer.MIN_VALUE;


int smallest = Integer.MAX_VALUE;

char choice;

do {
System.out.print("Enter a number: ");
int number = sc.nextInt();

if (number > largest) {


largest = number;
}

if (number < smallest) {


smallest = number;
}

System.out.print("Do you want to enter another number? (y/n): ");


choice = sc.next().charAt(0);
} while (choice == 'y' || choice == 'Y');

System.out.println("Largest number entered: " + largest);


System.out.println("Smallest number entered: " + smallest);

sc.close();
}
}
Output:

DHRUTI PATEL 22C21058 Page no.: 10


Question 13
Write a program to print out all Armstrong numbers between 1 and 500. If sum of
cubes of each digit of the number is equal to the number itself, then the number is
called an Armstrong number.
For example, 153 = (1 1 1)+(555)+(3*3*3)
Code:

class template2 {
public static void main(String[] args) {
int startRange = 1;
int endRange = 500;

System.out.println("Armstrong numbers between " + startRange + " and " + endRange


+ " are:");
for (int num = startRange; num <= endRange; num++) {
int originalNumber = num;
int numDigits = 0;
int temp = num;

while (temp > 0) {


temp /= 10;
numDigits++;
}

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;
}

DHRUTI PATEL 22C21058 Page no.: 11


if (armstrongSum == originalNumber) {
System.out.print(num + " ");
}
}
}
}
Output:

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

DHRUTI PATEL 22C21058 Page no.: 12


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num;
double sum = 0;
System.out.print("Enter your number: ");
num = sc.nextInt();
for(int i = 1; i <= num; i++)
{
sum += 1.0/i;
}
System.out.println("sum of number are: " + sum);
}
}
Output:

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);

System.out.print("Enter the value of n (a positive integer): ");


int n = sc.nextInt();

double sum = 0.0;


for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
sum += 1.0 / i;
} else {
sum -= 1.0 / i;
}
}

System.out.println("The natural logarithm of 2 using " + n + " terms is approximately: " +


sum);

sc.close();
}
}

DHRUTI PATEL 22C21058 Page no.: 13


Output:

DHRUTI PATEL 22C21058 Page no.: 14

You might also like