This article will help you with the solutions of the Programming in Java NPTEL 2022 Week 1 Assignment.
Programming in Java NPTEL Week 1 Assignment 1 Solutions
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 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
//Calculate the area
perimeter = 2 * Math.PI * radius;
area = Math.PI * radius * radius;
System.out.println(perimeter);
System.out.print(area);
}
}
Note: If you use System.out.println(area) then your code will run but a small error will be faced i.e.,
Passed after ignoring Presentation Error
In order to run the code 100% correct you have to use print instead of println.
Week 1: Programming Assignment 2 Solution
Programming in Java NPTEL Week 1 assignment solutions
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.print(result);
}
}
Week 1: Programming Assignment 3 Solution
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;
//Use for or while loop do the operation.
for(int i = 0; i < n; i += 2) {
if(i % 3 == 0) {
sum = sum + i;
}
}
System.out.print(sum);
}
}
Week 1: Programming Assignment 4 Solution
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.
int actual_number = n;
int number = 0;
while(n!=0) {
int digit = n%10;
number += (digit * digit * digit);
n = n/10;
}
// if number is palindrome
if(number==actual_number){
result = 1;
}
System.out.print(result);
}
}
Week 1: Programming Assignment 5 Solution
Complete the code segment to help Ragav , find the highest mark and average mark secured by him 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.
int[] arr = new int[s];
for(i=0;i<arr.length;i++)
{
arr[i]=input.nextInt();
}
//Initialize maximum element as first element of the array.
//Traverse array elements to get the current max.
//Store the highest mark in the variable result.
//Store average mark in avgMarks.
int max = arr[0];
int sum = 0;
for(i=0; i<arr.length; i++){
sum += arr[i];
if(arr[i]>max){
max = arr[i];
}
}
double average_marks = sum/arr.length;
System.out.println(max);
System.out.print(average_marks);
}
}
The above question set contains all the correct answers. But in any case, you find any typographical, grammatical or any other error in our site then kindly inform us. Don’t forget to provide the appropriate URL along with error description. So that we can easily correct it.
Thanks in advance.
For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.