NPTEL Programming in Java Week 1 Assignment Solution

Programming in Java Week1

NPTEL Programming in Java Week 1 All Programming Assignment Solutions | Swayam. With the growth of Information and Communication Technology, there is a need to develop large and complex software.

Further, those software should be platform independent, Internet enabled, easy to modify, secure, and robust. To meet this requirement object-oriented paradigm has been developed and based on this paradigm the Java programming language emerges as the best programming environment.

Now, Java programming language is being used for mobile programming, Internet programming, and many other applications compatible to distributed systems.

This course aims to cover the essential topics of Java programming so that the participants can improve their skills to cope with the current demand of IT industries and solve many problems in their own filed of studies.

COURSE LAYOUT
    • Week 1 : Overview of Object-Oriented Programming and Java
    • Week 2 : Java Programming Elements
    • Week 3 : Input-Output Handling in Java
    • Week 4 : Encapsulation
    • Week 5 : Inheritance
    • Week 6 : Exception Handling
    • Week 7 : Multithreaded Programming
    • Week 8 : Java Applets and Servlets
    • Week 9 : Java Swing and Abstract Windowing Toolkit (AWT)
    • Week 10 : Networking with Java
    • Week 11: Java Object Database Connectivity (ODBC)
    • Week 12: Interface and Packages for Software Development

Course Name : “Programming in Java”

Question : 1 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;
if(radius<=0)
 {
  System.out.println("please enter non zero positive number ");
 }
else
{
 perimeter = 2 * Math.PI * radius;
 area = Math.PI * radius * radius;
 System.out.println(perimeter);
 System.out.println(area);
}
 }
}
				
			

Question : 2 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;
if(x >= y && x >= z)
        {
            result=x;
        }
        else if(y >= z)
        {
            result=y;
        }
        else
        {
            result=z;
        }
System.out.println(result);
 }
}
				
			

Question : 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.

				
					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;
int result=1;
int i=0;
while(result<=n)
	 {
	  if(i%2==0)
	  {
		 if(i%3==0)
		 {
			 sum=sum+i;
			
         }			 
		 result=result+1; 
	  }
	  i=i+1;
	 }
      System.out.println(sum);
 }
}
				
			

Question : 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;
int temp=n;
int c=0,t; 
//Use while loop to check the number is Armstrong or not.
    while(n>0)
	{
		t=n%10;
		n=n/10;
		c=c+(t*t*t);
	}		
	if(temp==c)
		result=1;
	else
		result=0;
    //Evaluation code 
    System.out.println(result);
 }
}
				
			

Question : 5 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 of integer type.
      int[] arr = new int[s];   
      
	 for(i=0;i<arr.length;i++)
	  {
        	arr[i]=input.nextInt();
	  } 
 //initialise maximum element as first element of array.  
	   int max=arr[0];
           double sum=arr[0];  
	  //traverse array elements to get the current max
      for(i=1;i<arr.length;i++)
	  { 
         sum=sum+arr[i]; 
         if(arr[i]>max)
            max =arr[i];
	  }
	 
    //Store the highest mark in the variable max
   //Store average mark in avgMarks
    result=max;	
    mark_avg=sum/(arr.length);    
 //Evaluation code 
    System.out.println(result);
    System.out.println(mark_avg);
 }
}
				
			

3 thoughts on “NPTEL Programming in Java Week 1 Assignment Solution

Leave a Reply

Your email address will not be published.