Computer Programming-I (Cs 1301) Tutorial - #02
Computer Programming-I (Cs 1301) Tutorial - #02
Tutorial - #02
Tutorial - #02
Input / Output
Statements
Evaluation:
Q1. Show the output produced by each of the following. Align the output
carefully so the exact position of each output character is obvious.
int num = 375;
double per = 3.926;
Boolean flag= true;
String str = "Csc111 Java";
num = 10 + 4 + (int) 6.78;
per *= 2 * 0.6 + (int) 1.9;
System.out.printf("%-17s%-5d%-2.2f%n",str,num,per);
System.out.printf("num = %7d%n",num);
System.out.printf("per = %6.3f%n flag = %2b%n",per,flag);
System.out.printf("%12s%5d%n", "Question No:",1);
Line
1
Line
2
Line
3
Line
4
Line
5
Q2. Write one statement to produce the following outputs:
float grade = 15.75f;
string name="Saad";
Output:
s i
e d a r g
d a a S
% 0 5 > =
0 3
/ 8
. 5 1
Line
1
Line
2
Tutorial - #02
import java.util.Scanner
public class Q3
{
public static void main(String[] args) {
double radius;
final double PI = 3.14;
String str= 'circule';
System.out.println("start program " , str);
System.out.print("Enter radius:");
radius =<int>input.next();
area = PI * radius * radius;
System.out.printf("%5s area is calculated%",str);
System.out.printf("radius=%.2f area=%.2f%n",radius);
}
}
Correction/reason
error
lin
e
Tutorial - #02
Tutorial - #02
Q1. Write a program that asks the user to enter two numbers, obtains the two numbers
from the user and prints the sum, product, difference, and quotient of the two numbers.
Example:
if you entered
num1=10,
num2=2
then
the output will be
10 + 2 = 12
10 2 = 8
10 * 2 = 20
10 / 2 = 5
Program
Tutorial - #02
Q2. Write a java program that reads two numbers a and b. Swap these two numbers and
print their values.
Example:
if you entered
a=3, b=2 then
the output will be
a=2, b=3
Program
Tutorial - #02
Q3. Write a java program that reads five marks of a student. Compute the average of
these marks, then print the marks and their average, each in a separate line.
Example:
if you entered the marks: 72, 94, 60, 81, 73, then the program should output the following:
Marks:
72.0
94.0
60.0
81.0
73.0
Average:
76.0
Program