Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Assignment 4- Linear Loops

This document outlines Lab Assignment 4 for CSE110, which consists of 12 programming tasks focused on Java loops. Each task requires students to write Java code to perform specific operations, such as printing sequences, calculating sums and averages, finding divisors, and identifying prime and perfect numbers. The total points available for the assignment is 120.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 4- Linear Loops

This document outlines Lab Assignment 4 for CSE110, which consists of 12 programming tasks focused on Java loops. Each task requires students to write Java code to perform specific operations, such as printing sequences, calculating sums and averages, finding divisors, and identifying prime and perfect numbers. The total points available for the assignment is 120.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Assignment 4

Linear Loops

CSE110: Programming Language I

No of Tasks Points to Score

12 120

1. Write a Java code that would print the following sequences using while loop:
a) 24, 18, 12, 6, 0, -6
b) -10, -5, 0, 5, 10, 15, 20

2. Write a Java program that will take N numbers from the user and find their sum and average
using a for loop.

Sample Input:
N=5
Input the 5 numbers:
1
2
3
4
5
Expected Output:
The sum of 5 no is: 15
The Average is: 3.0

3. Write a Java program that will keep taking integer numbers as inputs from the user and print
the square of those numbers until it gets a negative number and then stop.

Sample Input/Output: (The purple numbers are input.)


Enter Number: 2
2^2=4
Enter Number: 6
6 ^ 2 = 36
Enter Number: 1
1^2=1
Enter Number: 4
4 ^ 2 = 16
Enter Number: -5

4. Write a Java program that will take an integer as input and print all the divisors of that
number.

Sample Input
6
Sample Output
Divisors of 6:
1
2
3
6

5. Write a Java code that asks an integer as input from the user and takes that many integer
inputs. Your task is to count how many numbers are non-negative and negative.

Sample Input: (The purple numbers are input.)


Enter an integer: 9
Enter number 1: -8
Enter number 2: 33
Enter number 3: -100
Enter number 4: 10
Enter number 5: 0
Enter number 6: 5
Enter number 7: 10
Enter number 8: -4
Enter number 9: 4
Sample Output:
6 Non-negative Numbers
3 Negative Numbers

6. Write a Java program that displays the sum of first n odd natural numbers.

Sample Input:
Input number of terms: 5
Expected Output:
The odd numbers are:
1
3
5
7
9
The Sum of odd Natural Numbers up to 5 terms is: 25

7. Write a Java program that will read 10 numbers from the user, and then print the first number,
the sum of the first 2 numbers, the first 3 numbers, and so on up to the sum of 10 numbers.

Sample Input/Output: (The purple numbers are input.)


Enter Number: 6
Sum = 6
Enter Number: 7
Sum = 13
Enter Number: 2
Sum = 15
Enter Number: 8
Sum = 23
Enter Number: 7
Sum = 30
Enter Number: 5
Sum = 35
Enter Number: 1
Sum = 36
Enter Number: -12
Sum = 24
Enter Number: 0
Sum = 24
Enter Number: 1
Sum = 25

8. Write a Java program that will take a positive integer n as input and print all the numbers from
0 to n which are divisible by 5 but not divisible by 3.

Sample Input
40
Sample Output
5
10
20
25
35
40

9. Write a program in Java that asks the user for an integer input and counts the number of digits
in the number.
Hint: You may keep dividing the number by ten and count how many times this can be done
until the number becomes 0.

Sample Input Output

7546 Total digits = 4

10. Write a program in Java that asks the user for an integer input, and print the individual digits
forward (From left to right).

Sample Input Output

32768 3, 2, 7, 6, 8

[Hint: First, count how many digits. Then calculate 10 to the power that (number of digits)
minus 1.Say, 32768 has 5 digits, so you calculate 10 to the power 4 which is 10,000.
Then divide 32,768 by 10,000 and thus you get 3
Take remainder of 32,768 by 10,000 and thus you get 2,768
Then divide 10,000 by 10 to get 1,000

Then divide 2,768 by 1,000 and thus you get 2.


take remainder of 2,768 by 1,000 and thus you get 768
keep going on until there are no more digits left (zero!).

In short:
Part 1: First count digits, say 5 in this case for 32,768
Part 2: Then calculate 10 to the power 4 (5-1), that is 10,000.
Part 3: Then repeat the following three steps:
32,768 / 10,000 = 3
32,768 % 10,000 = 2,768
10,000/10 = 1,000

2,768 / 1,000 = 2
2,768 % 1,000 = 768
1,000/10 = 100

768 / 100 = 7
768 % 100 = 68
100/10 = 10

68 / 10 = 6
68 % 10 = 8
10/10 = 1

8/1=8
8%1=0
1/10 = 0

11. Write a Java program that will take an integer as input and -
a) Find out if the number is a prime number or not.
b) Find out if the number is a perfect number or not.
[Prime Number: If a number has only two divisors, (1 and itself), then it is a prime number.
Else, then it is not a prime number.
Perfect Number: A number is said to be a perfect number if the sum of its divisors, including 1
but not the number itself is equal to that number.]
Sample Input
6
Sample Output
6 is not a prime number
6 is a perfect number

12. Trace the following code, create a tracing table and write the outputs

1 public class T4
2 {
3 public static void main(String args[])
4 {
5 int x = 0, p = 0, sum = 0;
6 p = 1;
7 x = 2;
8 double q;
9 sum = 0;
10 while (p < 12){
11 q = x + p-(sum+7/3)/3.0%2 ;
12 sum = sum + (++x) + (int)q;
13 System.out.println(sum++);
14 if (x > 5){
15 p += 4/2;
16 }
17 else {
18 p += 3%1;
19 }
20 }
21 sum = sum + p;
22 System.out.println(sum);
23 }
24 }

You might also like