0% found this document useful (0 votes)
4 views

Java 40 First Mock Programs Preparation[1]

The document contains a series of Java programming tasks, each followed by a corresponding code solution. The tasks include checking for palindromes, counting digits, generating Fibonacci series, identifying Armstrong and perfect numbers, checking for prime numbers, and printing various star patterns. Each program is structured with a main method and relevant logic to achieve the specified output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java 40 First Mock Programs Preparation[1]

The document contains a series of Java programming tasks, each followed by a corresponding code solution. The tasks include checking for palindromes, counting digits, generating Fibonacci series, identifying Armstrong and perfect numbers, checking for prime numbers, and printing various star patterns. Each program is structured with a main method and relevant logic to achieve the specified output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Que 1 : Write a java program the given number is palindrom or not.

Given number : 121


Output = palindrom
program:
--------
public class PalindromeChecker {
public static void main(String[] args) {
int number = 121;
int originalNumber = number;
int reversedNumber = 0;

while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}

if (originalNumber == reversedNumber) {
System.out.println("Output = palindrome");
} else {
System.out.println("Output = not a palindrome");
}
}
}

-----------------------------

Que 2 : Write a java program to find count of digit of given number.


Given number : 6372
Output = 4
program:
--------
public class DigitCounter {
public static void main(String[] args) {
int number = 6372;
int count = 0;

while (number != 0) {
number /= 10;
count++;
}

System.out.println("Output = " + count);


}
}

-----------------------------

Que 3 : Write a program to print fibonacci serise for given number .


Given number : 7
Output = 0 1 1 2 3 5 8
program:
--------
public class FibonacciSeries {
public static void main(String[] args) {
int number = 7;
int firstTerm = 0;
int secondTerm = 1;
System.out.print("Output = " + firstTerm + " " + secondTerm);

for (int i = 2; i < number; i++) {


int nextTerm = firstTerm + secondTerm;
System.out.print(" " + nextTerm);
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}

-----------------------------

Que 4 : Write a java program to check given number is armstrong or not.


Given input : 153
Output : 153 is armstrong number.
program:
--------
public class ArmstrongNumber {
public static void main(String[] args) {
int number = 153; // Change this number to check other inputs
int originalNumber, remainder, result = 0;
int n = 0;

originalNumber = number;

// Finding the number of digits


while (originalNumber != 0) {
originalNumber /= 10;
++n;
}

originalNumber = number;

// Checking if the number is an Armstrong number


while (originalNumber != 0) {
remainder = originalNumber % 10;
result += Math.pow(remainder, n);
originalNumber /= 10;
}

if (result == number) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}
}

-----------------------------

Que 5 : Write a java program to print all armstrong numbers between given range.
(in this question iterviewer wants to check logic for multidigit armstrong
number like 4,5..n digit.)
given range : 100 to 10000
Output :
153
370
371
407
1634
8208
9474
program:
--------
public class ArmstrongNumbers {

public static void main(String[] args) {


int start = 100;
int end = 10000;

System.out.println("Armstrong numbers between " + start + " and " + end +


":");
for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
System.out.println(i);
}
}
}

private static boolean isArmstrong(int number) {


int sum = 0;
int temp = number;
int digits = String.valueOf(number).length();

while (temp != 0) {
int digit = temp % 10;
sum += Math.pow(digit, digits);
temp /= 10;
}

return sum == number;


}
}

-----------------------------

Que 6 : Write a java program to check given number is perfect number or not.
Explanation : sum of divisors is equal to given number then its known as
perfect number. for example 6 is perfect number because(1+2+3 = 6)
Given number : 6
Output : true
Given number : 28
Output : true
Given number : 7
Output : false
program:
--------
public class PerfectNumber {
public static void main(String[] args) {
int number1 = 6; // Example 1
int number2 = 28; // Example 2
int number3 = 7; // Example 3

System.out.println(number1 + " is a perfect number: " +


isPerfectNumber(number1));
System.out.println(number2 + " is a perfect number: " +
isPerfectNumber(number2));
System.out.println(number3 + " is a perfect number: " +
isPerfectNumber(number3));
}

public static boolean isPerfectNumber(int number) {


int sum = 0;
for (int i = 1; i <= number / 2; i++) {
if (number % i == 0) {
sum += i;
}
}
return sum == number;
}
}

-----------------------------

Que 7 : Write a java program to check given number number is prime or not.
Given input : 7
Output : true
Given input : 4
Output : false
program:
--------
public class PrimeCheck {

public static void main(String[] args) {


int number1 = 7;
System.out.println("Is " + number1 + " prime? " + isPrime(number1));

int number2 = 4;
System.out.println("Is " + number2 + " prime? " + isPrime(number2));
}

private static boolean isPrime(int number) {


if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}

-----------------------------

Que 8 : Write a java program to print given pattern

*
* *
* * *
* * * *
* * * * *
program:
--------
public class StarPattern {

public static void main(String[] args) {


int rows = 5; // You can change this to the number of rows you want

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


for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

-----------------------------

Que 9 : Write a java program to print given pattern

*
* *
* *
* *
* * * * *
program:
--------
public class StarPattern {

public static void main(String[] args) {


int rows = 5; // Number of rows to be printed

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


for (int j = 1; j <= i; j++) {
// Print star for the first and last column of each row
if (j == 1 || j == i || i == rows) {
System.out.print("* ");
} else {
// Print space for the columns in between
System.out.print(" ");
}
}
System.out.println();
}
}
}

-----------------------------

Que 10 : Write a java program to print given pattern

*
* *
* * *
* * * *
* * * * *
program:
--------
public class StarPattern {

public static void main(String[] args) {


int rows = 5; // Number of rows

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


// Print leading spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}

-----------------------------

Que 11 : Write a java program to print given pattern

*
* *
* *
* *
* * * * *
program:
--------
public class StarPattern {

public static void main(String[] args) {


int rows = 5; // Number of rows

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


// Print leading spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars and spaces between stars
for (int k = 1; k <= i; k++) {
if (k == 1 || k == i || i == rows) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

-----------------------------

Que 12 : Write a java program to print given pattern


*
* *
* * *
* * * *
* * * * *
program:
--------
public class StarPattern {

public static void main(String[] args) {


int rows = 5; // Number of rows

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


// Print leading spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars and spaces between stars
for (int k = 1; k <= i; k++) {
System.out.print("* ");
if (k < i) {
System.out.print(" ");
}
}
System.out.println();
}
}
}

-----------------------------

Que 13 : Write a java program to print given pattern

*
* * *
* *
* * *
* * *
* * * *
* * * *
* * * * *
program:
--------
public class StarPattern {

public static void main(String[] args) {


int rows = 5; // Number of rows

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


// Print leading spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("* ");
}
System.out.println();
}
}
}

You might also like