Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Programming Assignment 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

CSE 1001: Introduction to Computer Programming

Programming Assignment-V
(Iterative Statements-2)

Question-1:
Amicable numbers are pair of numbers each of whose divisors are added to give the other
number.
Example: The smallest pair of amicable numbers is (220, 284).
They are amicable because the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and
110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which
the sum is 220.
Note: 1 is included as a divisor but the numbers are not included as their own divisors.
Write a java program that tests whether a given pair of numbers is amicable numbers or not.
Sample run 1:
Enter first number: 220
Enter second number: 284
220 and 284 are amicable numbers.
Sample run 2:
Enter first number: 220
Enter second number: 230
220 and 230 are not amicable numbers.

Question-2:
WAP to check whether a number is twisted prime or not. Twisted prime is a number if the
number and its reverse both are prime then it is called twisted prime.

Sample run 1:
Enter a number: 97
97 is twisted prime.

Sample run 2: 43
43 is not a twisted prime
Question-3:
WAP to enter the first number and second number. Display the prime numbers between the
first and second number.

Sample run:
Enter the first number: 4
Enter the second number: 15
Prime numbers between 4 and 15 are: 5 7 11 13

Question-4:
WAP to calculate and display the factorial of all numbers between m and n (where m < n, m
> 0, n > 0)
Sample run:
Enter the value of m: 2
Enter the value of n: 5
Factorial of 2 is: 2
Factorial of 3 is: 6
Factorial of 4 is: 24
Factorial of 5 is: 120

Question-5:
WAP to display the multiplication table from 2 to 15.

Sample run:
Multiplication table of 2
2 × 1 = 2
2 × 2 = 4
:
:
2 × 10 = 20

Multiplication table of 3
3 × 1 = 3
3 × 2 = 6
:
:
3 × 10 = 30
:
:
:
Multiplication table of 15
15 × 1 = 15
15 × 2 = 30
:
:
15 × 10 = 150

Question-6:
Write a program to print the following outputs using for loops:

(a) *
* *
* * *
* * * *
* * * * *

(b) 1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

(c) 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(d) 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Question-7:
Write a program to print the following outputs using for loops:

(a) A
A B
A B C
A B C D
A B C D E

(b) $ $ $ $ $
$ $ $ $
$ $ $
$ $
$

(c) 1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Question-8:
WAP to enter the value of n and display find the following sum of the series:
1 + (1 + 2) + (1+ 2 +3) ..... + (1 + 2 + 3 + .... + n)

Question-9:
Write a program that will read the value of n from the user and calculate sum of the following
series:
1 1 1 1 1
2
+ 2 + 2 + 2 + ... + 2 .
1 2 3 4 𝑛

Question-10:
Given a=0, b=1 and c=1 are the first three numbers of some sequence. All other numbers in
the sequence are generated from the sum of their three most recent predecessors. Write a java
program to generate this sequence up to n terms where n > 3.

Home Assignment (Iterative Statement-2)

Question-1:
Write a program to print the following patterns:
5432*

543*1

54*21

5*321

*4321

Question -2:
Write a program to print the following patterns using loops:

*
***
*****
*******
Question-3:
Write a java program to evaluate the function sin(x) as defined by the infinite series expansion.
𝑥3 𝑥5 𝑥7
sin (x) = x- + - + +…
3! 5! 7!

The acceptable error for computation is 10-6.

Question-4:
Write a java program to evaluate the function sin(x) as defined by the infinite series expansion.
𝑥2 𝑥4 𝑥6
cos (x) = 1- + - + +…
2! 4! 6!

The acceptable error for computation is 10-6.

Question-5:
Write a java program to generate and print the first n terms of the Fibonacci sequence where
n>=1.
The first few terms are:
0, 1, 1, 2, 3, 5, 8, 13, ......
Each term beyond the first two is derived from the sum of its two nearest predecessors i.e. a
new term in the series (Except the first two) is found by the following formula.
new term=preceding term + term before the preceding term
Let us define:
c as new term
b as the preceding term
a as the term before the preceding term
So, c= b + a
Your program should handle for all positive values of n.
Example:
If n=1, it will display as: Fibonacci Series is: 0
If n=2, it will display as: Fibonacci Series is: 0, 1
If n=3, it will display as: Fibonacci Series is: 0, 1, 1 ….
If n=10, it will display as: Fibonacci Series is: 0, 1, 1, 2,
3, 5, 8, 13, 21, 34

*************

You might also like