Monday, September 14, 2020

Programming in Java WEEK 1 NPTEL(JULY - DEC 2020) ASSIGNMNET SOL.

Question 1: 

 
if(radius>0)
       {
perimeter = 2*Math.PI*radius;
    area = (Math.PI)*radius*radius;
          System.out.println(perimeter);
          System.out.print(area);
       }

else 
      System.out.print("please enter non zero positive number");


Question 2:


//Use if...else ladder to find the largest among 3 numbers and store the largest number in a variable called result.

if(x>y)
        {
          if(x>z)
            System.out.print(x);
          else 
            System.out.print(z);
        }

else 
        {
          if(y>z)
            System.out.print(y);
          else
            System.out.print(z);
        }

Question 3:


//Use for or while loop do the operation

  for(int i = 0, j = 0; i<n; i++, j=j+2)
      {
        if(j%3==0)
          sum = sum + j;
      }
  System.out.print(sum);
        

Question 4:


//Use while loop check the number is Armstrong or not.
//store the output(1 or 0) in result variable.
int sum = 0;
int n1 = n;
int counter = 0;
int[] arr = new int[100];

while(n>0)
        {
          int rem = n%10;
          arr[counter] = rem;
          n = n/10;
          counter++;
        }

for(int i=0; i<counter; i++)
        {
          int l = 1;
          for(int j=1; j<=counter; j++)
          {
            l=l*arr[i];
          }
          sum = sum + l;
        }
            

if(sum == n1)
          result = 1;
else 
          result = 0;

System.out.print(result);

          

Question 5:


//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 largest;
int sum = 0;
float avg;
for(i=0;i<arr.length;i++)
  {
        for(int j = i; j<arr.length; j++)
                  if(arr[i]<arr[j])
                {
                  int temp = arr[i];
                  arr[i] = arr[j];
                  arr[j] = temp;
                }
    } 

largest = arr[0];

for(i=0;i<arr.length;i++)
  {
        sum = sum + arr[i];
  } 
avg = sum/arr.length;
System.out.println(largest);
System.out.print(avg);

0 Comments:

Post a Comment