Assignment 4- Linear Loops
Assignment 4- Linear Loops
Linear Loops
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.
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.
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.
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.
10. Write a program in Java that asks the user for an integer input, and print the individual digits
forward (From left to right).
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
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 }