0% found this document useful (0 votes)
38 views

Computer Programming-I (Cs 1301) Tutorial - #02

This document contains a tutorial on input/output statements in computer programming. It provides examples of using printf formatting in Java to output variables of different data types. It also contains practice problems for students to write Java programs that perform basic operations like calculating averages, swapping variables, and displaying outputs. Students are asked to identify errors in code samples and write code to meet given specifications for various tasks like calculating sums and products of input numbers.

Uploaded by

Samia Elsayed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Computer Programming-I (Cs 1301) Tutorial - #02

This document contains a tutorial on input/output statements in computer programming. It provides examples of using printf formatting in Java to output variables of different data types. It also contains practice problems for students to write Java programs that perform basic operations like calculating averages, swapping variables, and displaying outputs. Students are asked to identify errors in code samples and write code to meet given specifications for various tasks like calculating sums and products of input numbers.

Uploaded by

Samia Elsayed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer programming- I (CS 1301)

Tutorial - #02

Tutorial - #02

Input / Output
Statements

Evaluation:

Student Name &


Number
Date: 23/ 1/ 1438
Instructor: Maha

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

Computer programming- I (CS 1301)

Tutorial - #02

Q3. Find errors in the following programs ( 5 errors):


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

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

Computer programming- I (CS 1301)

Tutorial - #02

Q4. What is output by the following program ?


Program
public class Q4
{
public static void main(String[] args) {
int x=10; int y=20;
String str="Hello Every body";
boolean bool=true;
char c='h';
System.out.print("The first line is:"+str+"\n");
System.out.println("x" + " /1.0 = " + (++x/1.0));
System.out.println("Is it true?"+((x<y)&&(!bool)));
System.out.println("y is "+ y + ((int)y==(int)(y++))+ " y is"+y);
}
}
Output

Computer programming- I (CS 1301)

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

Computer programming- I (CS 1301)

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

Computer programming- I (CS 1301)

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

Note: use nextDouble() to read floating-point numbers.

Program

You might also like