Midsem Ans
Midsem Ans
a. -13 -26
-13 -26 -1
b. a=72 b=-97
a=72 b=-98
c. 10^6=12 because 10 XOR(^) 6 =12
2
a. Case 1: put bracket while multiplying r*r as the system
dividing mass2 by r then the result is multiplied with r. The
correct formula: F = G* mass1*mass2/(r*r);
Case 2:If still the result is unexpected, that means you have
taken the inputs as int, so int / int results in int is the
cause of unexpected result. So, either takes the inputs as
double or typecast the operands used in the division to
double.
b. public class B2 {
public static void main(String[] args) {
int r =Integer.parseInt(args[0]);
int h =Integer.parseInt(args[1]);
double area=Math.PI*r*r+2*Math.PI*r*h;
System.out.println("Area ="+ area);
}
}
c. import java.util.Scanner;
public class Q2C {
public static void main(String[] args) {
Scanner ob=new Scanner(System.in);
System.out.println("Enter the no. of days");
int day=ob.nextInt();
int year=day/360;
int month=(day%360)/30;
int rd=(day%360)%30;
System.out.println(day+" is "+ year+" year, "+month+"
month and "+rd+" days");
}
}
3
a. public class Q3a {
public static void main(String[] args) {
int a =Integer.parseInt(args[0]);
int b =Integer.parseInt(args[1]);
int c =Integer.parseInt(args[2]);
if((a==b)&&(b==c))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
b. int largest= (a>b & a>c)?a:(b>c?b:c);
System.out.println(largest);
c. import java.util.Scanner;
public class Q3c {
public static void main(String[] args) {
Scanner ob=new Scanner(System.in);
System.out.println("Enter a no.");
int n=ob.nextInt();
System.out.println((n%10 ==7)||(n%7 ==0));
}
}
4
a. import java.util.Scanner;
public class Q4a {
public static void main(String[] args) {
Scanner ob=new Scanner(System.in);
System.out.println("Enter a no.");
int n=ob.nextInt();
int sumd=0,cd;
while(n!=0)
{
cd=n%10;
sumd=sumd+cd;
n=n/10;
}
if(sumd%9==0)
System.out.println("Divisible by 9");
else
System.out.println("Not Divisible by 9");
}
}
b.
c. Assume n=1543
sumd =0
1543!=0 (True) cd=1543%10=3 sumd=0+3=3 n=1543/10=154
154!=0 (True) cd=154%10=4 sumd=3+4=7 n=154/10=15
15!=0 (True) cd=15%10=5 sumd=7+5=12 n=15/10=1
1!=0 (True) cd=1%10=1 sumd=12+1=13 n=1/10=0
0!=0 (False) STOP
13%9 == 0 is False
OUTPUT : Not Divisible by 9
5
a. import java.util.Scanner;
public class Q5a {
public static void main(String[] args) {
Scanner ob=new Scanner(System.in);
System.out.println("Enter the x-axis");
int x=ob.nextInt();
System.out.println("Enter the Y-axis");
int y=ob.nextInt();
if (x > 0 && y > 0)
System.out.println("lies in First quadrant");
else if (x == 0 && y != 0)
System.out.println("lies at y axis");
else if (y == 0 && x != 0)
System.out.println("lies at x axis");
else
System.out.println("lies at origin");
}
}
b. public class Q5b {
public static void main(String[] args) {
int n=Integer.parseInt(args[0]);
for(int a = 0; a <= n; a++)
{
for(int b = 0; b <= n; b++)
{
for(int c = 0; c <= n; c++)
{
for(int d = 0; d <= n; d++)
{
if ((a != b) && (a != c) && (a != d) &&
(b != c) && (b != d) && (c != d))
{
int x = (int)Math.pow(a, 3) +
(int) Math.pow(b, 3);
int y = (int)Math.pow(c, 3) +
(int) Math.pow(d, 3);
if ((x) == (y))
{
System.out.println(a+"^3+"+b+"^3="+c+"^3+"+d+"^3");
}
}
}
}
}
}
}
}
c. for(int i=5;i>=1;i-- )
{
for(int j=1;j<=5;j++ )
{
if(j<i)
System.out.print(" ");
else
System.out.print(" $");
}
System.out.println();
}