NPTEL JAVA WEEK 01 ASSIGNMENT SOLUTION
WEEK 01 SOLUTIONS WITH PROOF OF COMPILATION.
QUESTION 1= Complete the code segment to find the perimeter and area of a circle given a value of radius ?.
ANSWER
if(radius>0) {
area=Math.PI * radius * radius;
perimeter=2 * Math.PI * radius;
System.out.println(perimeter);
System.out.println(area);}
else{
System.out.print("please enter non zero positive number");
}
PROOF OF PASSING =
QUESTION NO 2 = TO find the largest among three numbers x,y, and z?
ANSWER if( x > y && x > z)
{
result = x;
}
else if( y > x && y > z)
{
result = y;
}
else if( z > y && z > x)
{
result = z;
}
else
{
result = x;
} System.out.print(result);
PROOF OF PASSING =
QUESTION NO 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?
ANSWER 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.print(sum);
PROOF OF PASSING =
QUESTION NO 4 = TO check whether the number is an Armstrong number or not.
Armstrong Number?
ANSWER
int remainder,temp,count=0,i;
temp=n;
while(temp!=0)
{
temp/=10;
count++;
}
i=count;
temp=n;
while(count>0)
{
remainder=temp%10;
result += Math.pow(remainder, i);
temp/=10;
count--;
}
if(n==result)
result=1;
else
result=0;
System.out.print(result);
PROOF OF PASSING=
QUESTION NO 5= Find the highest mark and average mark secured by him in "s" number of subjects.?ANSWER int temp=0,j;
PROOF OF PASSING=
for(i=0; i < s; i++)
{
for(j=1; j < (s-i); j++)
{
if(arr[j-1] > arr[j])
{
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
result=arr[s-1];
temp=0;
for(i=0;i<arr.length;i++)
{
temp +=arr[i];
}
mark_avg=temp/s;
System.out.println(result);
System.out.print(mark_avg);
THANKS AND STAY UPDATED FOR WEEK 02 OF THIS COURSE
FOLLOW FOR NEXT WEEK
ReplyDelete