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

Java Exercise 1: QUES1

This document contains 10 questions related to Java exercises completed by Khushi Agrawal. The questions cover topics such as checking if a number is even or odd, calculating the area of a circle, determining if a year is a leap year, using switch statements, for loops to calculate sums, nested for loops to print multiplication tables, do-while loops, parsing digits from a number, searching arrays, and checking if the sum of the first and last digits of a number is odd.

Uploaded by

koko
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)
61 views

Java Exercise 1: QUES1

This document contains 10 questions related to Java exercises completed by Khushi Agrawal. The questions cover topics such as checking if a number is even or odd, calculating the area of a circle, determining if a year is a leap year, using switch statements, for loops to calculate sums, nested for loops to print multiplication tables, do-while loops, parsing digits from a number, searching arrays, and checking if the sum of the first and last digits of a number is odd.

Uploaded by

koko
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
You are on page 1/ 11

JAVA EXERCISE 1

KHUSHI AGRAWAL
19BIT0371

QUES1.
package kkkjbjk;

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n= sc.nextInt();

if(n%2==0) {
System.out.println("EVEN NUMBER");
}
else {
System.out.println("ODD NUMBER");
}

}}
QUES2.
package kkkjbjk;

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("enter radius");
int r= sc.nextInt();

double area = 3.14*r*r;


System.out.println("The area of circle is " + area);

}}

QUES3.
package kkkjbjk;

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("enter year");
int year= sc.nextInt();
boolean leap = false;

// if the year is divided by 4


if (year % 4 == 0) {

// if the year is century


if (year % 100 == 0) {

// if year is divided by 400


// then it is a leap year
if (year % 400 == 0)
leap = true;
else
leap = false;
}

// if the year is not century


else
leap = true;
}

else
leap = false;

if (leap)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");

}}
QUES4.
package kkkjbjk;

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("enter roomNo");
int roomNo = sc.nextInt();

switch(roomNo) {

case 823:
System.out.println("Java Programming");
break;
case 824:
System.out.println("Python Programming");
break;
default:
System.out.println("Invalid input");

}
sc.close();
}}
QUES5.
package kkkjbjk;

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("enter number");
int n = sc.nextInt();
int sum=0;

for(int i=1;i<=n;i++) {
sum+=i;
}

System.out.println("sum of first"+ n + "numbers is " + sum);

sc.close();
}}
QUES6.

package kkkjbjk;

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("enter number");
int n = sc.nextInt();
int sum=0;

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

sc.close();
}}
QUES7.
package kkkjbjk;

import java.util.Scanner;

public class Main {


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

int k=1, a=0, b=0, s=0;


do{
System.out.print("Enter the first number-->");
a=sc.nextInt();
System.out.print("Enter the second number-->");
b=sc.nextInt();
s=a+b;
System.out.println("Sum is "+s);
System.out.print("Enter 1 if you want to try again-->");
k=sc.nextInt();}

while(k==1);
System.out.print("Thank you!");

}}
QUES8.
package kkkjbjk;

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("enter number");
int n = sc.nextInt();
int len = Integer.toString(n).length();
int[] digits = new int[len];
for (int i = 0; i < len; i++) {
digits[i] = n % 10;
n /= 10;
}

int even = 0, odd = 0, prime=0;

for(int i = 0; i < digits.length; i++) {


if(digits[i] % 2 == 0)
{
even++;
}
else
odd++;
}
System.out.printf("\nNumber of even elements in the array: %d",even);
System.out.printf("\nNumber of odd elements in the array: %d",odd);

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


int j = 2;
int p = 1;
while (j < digits[i]) {
if (digits[i] % j == 0) {
p = 0;
break;
}
j++;
}
if (p == 1) {
prime++;
}
}
System.out.printf("\nNumber of prime elements in the array:
%d",prime);
sc.close();
}}
QUES9.
package kkkjbjk;

import java.util.Scanner;

public class Main {


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

String names[]={"Kumar","Dinesh","Ganesh","Rajesh","Rakesh"};
String
number[]={"9012345621","8143567890","7114567213","9098456743","8159056784"};
int exp[]={17,7,13,2,9};
System.out.println("Enter mobile number:-");
String n=sc.nextLine();
int i=0;
boolean flag=false;
for(i=0;i<number.length;i++){
if(n.equals(number[i])){
flag=true;
if(exp[i]<3){
System.out.println("Not eligible for bonus !");
}
else{
System.out.println("Amount--
>"+500*(exp[i]-3));
System.out.println("Mobile number is
"+number[i]);
}}}
if(flag==false)System.out.println("Invalid Mobile number !!");

}}
QUES10.
package kkkjbjk;

import java.util.Scanner;

public class Main {


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

System.out.println("Enter the number:-");


int n=sc.nextInt();
int a=n;
int x=0;
while(a>0){
x=a%10;a/=10;
}
if((n%10+x)%2==1)
System.out.println("ODD SUM!");
else
System.out.println("No Odd sum!");

}}
Q.

You might also like