Computer Assignment Term-3
Computer Assignment Term-3
COMPUTER APPLICATIONS
ASSIGNMENT
2022-23 TERM 3
1
INDEX
Q.No. Question Page Number
1 Write Java expression for the following. 3
2 Purpose and return data type of the given Math 4
functions
3 Evaluation of the given expressions 5,7
4 Give the output of the following statements 6
5 Explain the following terms 8
6 Program to check print whether a number is a 9,10,11
Dudeney number or not.
7 Program to input number of hours parked and 14,15
print the charges as per given criteria.
8 Program to print pattern and sum of series 16,17,18
2
Question 1
Write Java expression for the following using Math function as required.
i) a^3 + b^3 + 3ab(a-b)
Ans: Math.pow(a,3)+Math.pow(b,3)+3*a*b*(a-b)
3
Question 2
State the purpose and return data type of the following functions.
i) Math.floor()
Ans: Return the lower interger value i:e < or = the argument
Return datatype:- double
ii) Math.random()
4
Question 3
Evaluate the following expressions.
i) c%= a/b + (++b + a--) - c++; when a=4, b=7 and c=3
Ans: c=c%(a/b+(++b+a--)-c++)
c=3%(4/7+(5+7)-3)
c=3%(4/7+12)-3)
c=3%(0+12)-3)
c=3%12-3
c=3-3
c=0
5
Question 4
Give the output of the following
i) nt i = -10;
do
{
if(i%2==0 && i>=0)
break;
else
System.out.println(i);
i+=2;
}
while(i<=10);
ii) System.out.println(Math.abs(Math.max(-5,-7)) +
Math.ceil(9.3)+Math.floor(-3.2));
Ans: Math.abs(-5)+(10.0)+(-4.0)
Math.abs(-5+10-4)
Math.abs(1)
1
6
int i,j;
1
0
2
1
0
3
2
1
0
7
Question 5
Explain the following
i) Encapsulation
Ans: Encapsulation in Java refers to integrating data (variables) and code
(methods) into a single unit.
ii) Literals
Ans: Literals in Java are a synthetic representation of boolean, character,
numeric, or string data.
8
Question 6
Do the following conversions:
i) Convert the following if-else construct to switch case.
if(m==0){
x= x+2;
System.out.println("X=" +x);
}
else if(m==1){
x= x+4;
System.out.println("X=" +x);
}
else { x= x+6;
System.out.println("X=" +x);
}
Ans: Switch(x)
case:1
{
x= x+2;
System.out.println("X=" +x);
}
case:2
{
x= x+4;
System.out.println("X=" +x);
9
}
case:3
{
x= x+6;
System.out.println("X=" +x);
}
default:
{
System.out.println(“ERROR”);
}
10
Ans: for(;number!=2;) {
System.out.println(number);
number = 2;
System.out.println(number);
number = 3; }
11
Question 6
A Dudeney number is a positive integer that is a perfect cube such that the sum
of its digits is equal to the cube root of the number. Write a program to input a
number and check and print whether it is a Dudeney number or not.
Example:
Consider the number 512.
Sum of digits = 5 + 1 + 2 = 8
Cube root of 512 = 8
As Sum of digits = Cube root of Number hence 512 is a Dudeney number.
Ans: import java.util.Scanner;
12
int d = t % 10;
s += d;
t /= 10;
}
if (s == cubeRoot) {
System.out.println(n + " is a Dudeney number");
}
else {
System.out.println(n + " is not a Dudeney number");
}
}
else {
System.out.println(n + " is not a Dudeney number");
}
}
}
Question 7
13
Indian Railway Stand decided to take charge for the parking of 2 wheeler / 3
wheeler / 4 wheeler as per the following criteria:
No. of Hrs. Parking Charges
upto 8 hrs Rs 10
next 8 hrs Rs. 6
above 16 hours Rs. 5 for each additional 8 hr.
Input number of hours parked and print the charges as per given criteria.
Ans:
import java.util.Scanner
public class abc
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println(“Enter the no of hours for parking”);
int n=sc.nextInt();
int c=0;
if(n>0 && n<=8)
{
c=10;
System.out.println(c);
}
else if(n>8 && n<=16)
{
c=10+6;
else if(n>16)
14
{
int x=(n-16)/8;
int y=(n-16)%8;
if(y>0)
{
c=10+6+x*5+5;
System.out.println(c);
}
else
{
c=10+6+x*5;
System.out.println(c);
}
}
}
}
Question 8
15
Write a program for each of the following.
1)
1
26
3 7 10
4 8 11 13
5 9 12 14 15
Ans:
public class abc2
{
public static void main()
{
int k=0; int p=4:
for(int i=1;i<=5;i++)
{
k=i; p=4;
for(int j=1;j<=i;j++)
System.out.print(k+ “”);
k=k+p; p--;
}
System.out.println();
}
16
Ans:
import java.io.*;
class MathSeries {
// Function to get the series
static double Series(double x, int n)
{
double sum = 1, term = 1, fct, j, y = 2, m;
17
public static void main(String[] args)
{
double x = 3;
int n = 4;
System.out.println(Math.round(Series(x, n) *
10000.0) / 10000.0);
}
}
********************************************************************************
18
19