0% found this document useful (0 votes)
9 views7 pages

Java Programs

The document contains various Java programming tasks and examples that involve basic algorithms and data structures. It includes problems such as cooking instant noodles, checking if a number is perfect, determining if a number is an Armstrong number, and checking for palindromes. Additionally, it covers finding maximum and minimum elements in an array, calculating factorials, and identifying prime and happy numbers.

Uploaded by

Gavs Sastry
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Java Programs

The document contains various Java programming tasks and examples that involve basic algorithms and data structures. It includes problems such as cooking instant noodles, checking if a number is perfect, determining if a number is an Armstrong number, and checking for palindromes. Additionally, it covers finding maximum and minimum elements in an array, calculating factorials, and identifying prime and happy numbers.

Uploaded by

Gavs Sastry
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Chef has invented 1-minute Instant Noodles.

As the name suggests, each packet takes


exactly 1 minute to cook. Chef's restaurant has X stoves and only 1 packet can be cooked in
a single stove at any minute. How many customers can Chef serve in Y minutes if each
customer orders exactly 1 packet of noodles? Input Format The first and only line of input
contains two space-separated integers X and Y — the number of stoves and the number of
minutes, respectively. Output Format Print a single integer, the maximum number of
customers Chef can serve in Y minutes

import [Link].*;
class Main{
public static void main(String args[]){
int x,y,z;
Scanner sc=new Scanner([Link]);
x=[Link]();
y=[Link]();
z=x*y;
[Link](z);
}
}
Alice has scored X marks in her test and Bob has scored Y marks in the same test. Alice is
happy if she scored at least twice the marks of Bob’s score. Determine whether she is happy
or not. Input Format The first and only line of input contains two space-separated integers
X,Y — the marks of Alice and Bob respectively. Output Format For each testcase, print Yes
if Alice is happy and No if she is not, according to the problem statement. Sample 1: Input
Output 2 1 Yes

import [Link].*;
class Main{
public static void main(String args[]){
int x,y;
Scanner sc=new Scanner([Link]);
x=[Link]();
y=[Link]();
if(x>y)
{
[Link]("Yes");
}
else
{
[Link]("No");
}

}
}
perfect [Link] a number n. Print number is Perfect Number or Not Perfect Number.
Perfect Number: A perfect number is a positive integer that is equal to the sum of its positive
divisors, excluding the number itself. Example Test Case1: Input 6 Output Perfect Number

// Your First JAVA Program


import [Link].*;
class Main{
public static void main(String args[]){
Scanner sc =new Scanner([Link]);
int n=[Link]();
int sum=0;
for(int i=1;i
For a given 3 digit number, find whether it is armstrong number or not. An Armstrong number
of three digits is an integer such that the sum of the cubes of its digits is equal to the number
itself. Print "Yes" if it is a armstrong number else Print "No". NOTE: 371 is an Armstrong
number since 33 + 73 + 13 = 371 Example 1: Input: 153 Output: "Yes" Explanation: 153 is
an Armstrong number since 13 + 53 + 33 = 153. Hence answer is "Yes". Example 2: Input:
370 Output: "Yes" Explanation: 370 is an Armstrong number since 33 + 73 + 03 = 370.
Hence answer is "Yes". Expected Time Complexity: O(1) Expected Auxiliary Space: O(1)
import [Link].*;
class Main{
public static void main(String args[]){
int n,num,sum=0,rem;
Scanner sc=new Scanner([Link]);
n=[Link]();
num=n;
while(n!=0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(sum==num)
{
[Link]("Yes");
}
else
{
[Link]("N0");
}
}
}

Given a number N, check if a number is perfect or not. A number is said to be perfect if sum
of all its factors excluding the number itself is equal to the number. Return 1 if the number is
Perfect otherwise return 0. Example 1: Input: 6 Output: 1 Explanation: Factors of 6 are 1, 2,
3 and 6. Excluding 6 their sum is 6 which is equal to N itself. So, it's a Perfect Number.
Example 2: Input: 10 Output: 0 Explanation: Factors of 10 are 1, 2, 5 and 10. Excluding 10
their sum is 8 which is not equal to N itself. So, it's not a Perfect Number.

import [Link].*;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
int n,i,sum=0;
n=[Link]();
for(i=1;i

Given an integer, check whether it is a palindrome or not. if the given number is palindrome
print "Yes" otherwise "No" Example 1: Input: n = 555 Output: Yes Example 2: Input: n = 123
Output: No

// Your First JAVA Program


import [Link].*;
class Main{
public static void main(String args[]){
int n,num,rev=0,rem;
Scanner sc=new Scanner([Link]);
n=[Link]();
num=n;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(num==rev)
[Link]("Yes");
else
[Link]("No");
}
}
Given N, reverse the digits of N. Example 1: Input: 200 Output: 2 Explanation: By reversing
the digts of number, number will change into 2. Example 2: Input : 122 Output: 221
Explanation: By reversing the digits of number, number will change into 22

// Your First JAVA Program


import [Link].*;
class Main{
public static void main(String args[]){
int n,rev=0,rem;
Scanner sc=new Scanner([Link]);
n=[Link]();
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
[Link](rev);
}
}
Given a positive integer, N. Find the factorial of N. Example 1: Input: N = 5 Output: 120
Explanation: 5*4*3*2*1 = 120 Example 2: Input: N = 4 Output: 24 Explanation: 4*3*2*1 = 24
Your Task: You don't need to read input or print anything. Your task is to complete the
function factorial() which takes an integer N as input parameters and returns an integer, the
factorial of N.

// Your First JAVA Program


import [Link].*;
class Main{
static int fact(int n)
{
if(n==0 || n==1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int n=[Link]();
int res=fact(n);
[Link](res);
}
}
Ravi is an enthusiastic student. He knows that in computer terminology what a palindrome
means. We can have palindromes with numbers as well as strings. For example, madam is a
string palindrome and 121 is a number palindrome. Ravi has been assigned a task by his
teacher. In this task, he has been given a numbers. His duty is to tell the given number is
palindrome or [Link] Palindrome print 'YES' otherwise Print 'NO' Sample Test Case Test case 1:
Input 121 output YES
class Main{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
int i,n,rev=0,num,rem;
n=[Link]();
num=n;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(rev==num)
[Link]("YES");
else
[Link]("NO");
}
}
Write a program to print less than n even numbers. Input: 10 Output: 2 4 6 8
import [Link].*;
class Main{
public static void main(String args[]){
int n,i;
Scanner sc=new Scanner([Link]);
n=[Link]();
for(i=2;i<n;i+=2)
{
[Link](i+” “);
}
}
}
Given an integer n, return true if it is a power of four. Otherwise, return false. An integer n is
a power of four, if there exists an integer x such that n == 4x. Example 1: Input: n = 16
Output: true Example 2: Input: n = 5 Output: false Example 3: Input: n = 1 Output: true
// Your First JAVA Program
import [Link].*;
public class Main{
public static boolean ipf(int n)
{
if(n<=0)
{
return false;
}
while(n%4==0)
{
n=n/4;
}
return n==1;
}

public static void main(String args[]){


Scanner sc=new Scanner([Link]);
int n;
n=[Link]();
[Link](ipf(n));
}
}

For a given number N check if it is prime or not. A prime number is a number which is only
divisible by 1 and itself. Example 1: Input: N = 5 Output: 1 Explanation: 5 has 2 factors 1 and
5 only. Your Task: You don't need to read input or print anything. Your task is to complete
the function isPrime() which takes an integer N as input parameters and returns an integer, 1
if N is a prime number or 0 otherwise.
// Your First JAVA Program
import [Link].*;
class Main{
static int isprime(int n)
{
int i;
if(n<=1)
{
return 0;
}
for(i=2;i*i<=n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
int n;
n=[Link]();
[Link](isprime(n));
}
}
Write an algorithm to determine if a number n is happy. A happy number is a number defined
by the following process: Starting with any positive integer, replace the number by the sum
of the squares of its digits. Repeat the process until the number equals 1 (where it will stay),
or it loops endlessly in a cycle which does not include 1. Those numbers for which this
process ends in 1 are happy. Return true if n is a happy number, and false if not. Example 1:
Input: n = 19 Output: true Explanation: 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 +
02 = 1
import [Link];
import [Link];

public class HappyNumberChecker {

/**
* Calculates the sum of the squares of the digits of a number.
* @param n The number to process.
* @return The sum of the squares of its digits.
*/
private static int sumOfSquareDigits(int n) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
return sum;
}

/**
* Checks if a number is a happy number.
* @param n The number to check.
* @return true if the number is happy, false otherwise.
*/
public static boolean isHappy(int n) {
// Use a Set to detect cycles. If we encounter a number that is
already
// in the set, it means we are in a cycle and the number is not
happy.
Set<Integer> seenNumbers = new HashSet<>();

while (n != 1 && ![Link](n)) {


[Link](n);
n = sumOfSquareDigits(n);
}

// If n is 1, the number is happy. Otherwise, it's in a cycle.


return n == 1;
}

public static void main(String[] args) {


Example of an unhappy number
Scanner sc=new Scanner([Link]);
int n;
n=[Link]();

[Link](number1 + " is a happy number: " +


isHappy(number1));
[Link](number2 + " is a happy number: " +
isHappy(number2));
}
}
Read “n” elements into array. Print Maximum element in an array. Example: Input: 5 1 2 4 5
8 Output: 8 Explanation: Here we take 5 elements. Maximum element among 5 elements is
8
import [Link].*;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
int n=[Link]();
int[] a=new int[n];
for(int i=0;i<n;i++)
{
A[i]=[Link]();
}
Max=a[0];
for(int i=1;i<n;i++)
{
max=a[i];
}
}
[Link](max);

}
}
Read “n” elements into array. Print Minimum element in an array. Example: Input: 5 1 2 4 5 8
Output: 1 Explanation: Here we take 5 elements. MINIMUM element among 5 elements is 1

You might also like