Week 1: Programming Assignment 1
public class Exercise1_1
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
double radius= s.nextDouble();
double perimeter;
double area;
//Calculate the perimeter
perimeter = 2 * Math.PI * radius;
System.out.println (perimeter);
//Calculate the area
area = Math.PI * radius * radius;
System.out.print(area);
}
}
Week 1 : Programming Assignment 2
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;
if (x > y && x > z)
{
result = x;
}
else if (y > z)
{
result = y;
}
else
{
result= z;
}
System.out.print(result);
}
}
Week 1 : Programming Assignment 3
Consider First n even numbers starting from zero(0).Complete the code segment to calculate sum of all the numbers divisible by 3 from 0 to n. Print the sum.
Example:
Input: n = 5
-------
0 2 4 6 8
Even number divisible by 3:0 6
sum:6
import java.util.Scanner;
public class Exercise1_3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
for (int i = 0; i < (n*2)-1; i++)
{
if (i%2==0)
{
sum = sum + i;
}
}
sum = sum/3;
System.out.print(sum);
}
}
Week 1 : Programming Assignment 4
Complete the code segment to check whether the number is an Armstrong number or not.
Armstrong Number:
A positive number is called an Armstrong number if it is equal to the sum of cubes of its digits for example 153 = 13+53+33, 370, 371, 407, etc.
import java.util.Scanner;
public class Exercise1_4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int result=0;
//Use while loop check the number is Armstrong or not.
//store the output(1 or 0) in result variable.
No comments