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

Java Week1 Case Studies

This document contains the answers to 5 Java programming questions from Week 1. 1) The first question provides sample code to calculate the perimeter and area of a circle given the radius. It checks if the radius is less than or equal to 0 before performing calculations. 2) The second question contains code to find the largest of three numbers using if-else statements. 3) The third question involves writing a code segment to calculate the sum of numbers divisible by both 2 and 3 from 0 to a given number. 4) The fourth question asks to check if a given number is an Armstrong number using concepts like remainder, powers and summation. 5) The last question provides code to find the highest

Uploaded by

urvashi satra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views

Java Week1 Case Studies

This document contains the answers to 5 Java programming questions from Week 1. 1) The first question provides sample code to calculate the perimeter and area of a circle given the radius. It checks if the radius is less than or equal to 0 before performing calculations. 2) The second question contains code to find the largest of three numbers using if-else statements. 3) The third question involves writing a code segment to calculate the sum of numbers divisible by both 2 and 3 from 0 to a given number. 4) The fourth question asks to check if a given number is an Armstrong number using concepts like remainder, powers and summation. 5) The last question provides code to find the highest

Uploaded by

urvashi satra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Quiz1 Answers

1) c
2) c
3) d
4) d
5) b
6) a
7) c
8) b
9) b,d
10) c

JAVA WEEK1 :Q1


/*Complete the code segment to find the perimeter and area of a circle given a value of radius. You should use Math.PI constant in
your program. If radius is zero or less than zero then print " please enter non zero positive number ". */

import java.util.Scanner;
public class Question3 {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
double radius= s.nextDouble();
double perimeter;
double area;

if(radius<=0)
System.out.println(" please enter non zero positive number");
else
{
perimeter =Math.PI*(radius*radius);
area=2*Math.PI*radius;
System.out.println(perimeter);
System.out.println(area);
}
}
}
Java Week 1:Q2
Complete the code segment to find the largest among three numbers x, y, and z. You should use if-then-else construct in Java.

import java.util.Scanner;
public class Exercise1_2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int x = s.nextInt();
int y = s.nextInt();
int z = s.nextInt();
int result = 0;
//Use if...else ladder to find the largest among 3 numbers and store the largest number in a variable called result.
if(x>y && x>z)
result=x;
else if(y>x && y>z)
result=y;
else
result=z;
System.out.println(result);
}
}

Java Week 1:Q3


import java.util.Scanner;
public class Question3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
int i=0;
int count=0;
while(count<=n)
{
if(i%2==0 && i%3==0)
sum=sum+i;

i=i+2;
count++;
}
System.out.println(sum);
}
}

Java Week 1:Q4


Complete the code segment to check whether the number is an Armstrong number or not.

import java.util.Scanner;
public class Question3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int result=0;
int sum=0;
int temp=n;
while(n!=0)
{
sum=sum+(int)Math.pow((n%10),3);
n=n/10;
}

if(sum==temp)
result=1;
else
result=0;
System.out.println(result);
}
}

Java Week 1:Q5


Complete the code segment to find the highest mark and average mark secured by Hari in "s" number of subjects.

import java.util.Scanner;
public class Exercise1_5{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double mark_avg;
int result;
int i;
int s;
//define size of array
s = input.nextInt();
//The array is defined "arr" and inserted marks into it of integer type.
int[] arr = new int[s];

for(i=0;i<arr.length;i++)
{
arr[i]=input.nextInt();
}

result=arr[0];
int sum=arr[0];
for(i=1;i!=s;i++)
{
sum=sum+arr[i];
if(arr[i]>result)
result=arr[i];
}
mark_avg=sum/s;

System.out.println(result);
System.out.println(mark_avg);
}}

You might also like