Due
Date: Nov 25th 2011
Course:
Programming Fundamentals
Total Marks: 20
Problem 1: (5 points)
Implement
a function that takes a number as input from console and returns whether the
number is prime or not (it returns 1 if the number is prime and 0 if the number
is not prime). Following is function prototype:
int
isprime(int);
Write
a program that prompts the user to input two numbers. The program calculates the
average of prime numbers between these numbers and displays it in the format
shown below. The program uses the function isprime included as a header file. Use only what we
have learned so far in the class.
Hint: 2 is the first
prime number. Please make sure that you take care of this in your program.
Sample Output
Enter first number: 1
Enter second number: 20
The average of prime numbers
between 1 and 20 is 9.62
|
Problem 2: (5 points)
Write
a program that prompts the user to input a number. The program determines and
prints twin primes till that number in the format shown below. The program uses
the function isprime included as a header
file. Use only what we have learned so far in the class.
Hint: A pair of prime
numbers that differ by 2 (successive odd numbers that are both Prime numbers)
is called twin primes. e.g., (3, 5), (5, 7), (11, 13),…
Sample Output
Enter the upper limit: 100
The twin primes from 1 till 100
are:
3, 5
5, 7
11, 13
17, 19
29, 31
41, 43
59, 61
71, 73
|
Problem 3: (10 points)
Implement
another function which takes a number as an argument and prints it as a product
of prime numbers. The prototype of the function is:
void productofprimes(int);
The
function productofprimes calls the function isprime to determine the
next prime numbers.
Write
a program that reads a number from console and prints it as the product of
prime numbers. The program uses isprime and productofprimes functions included as a header file. Use
only what we have learned so far in the class.
e.g., 90 = 2 x3 x 3
x5
100 = 2 x 2 x 2 x 5 x 5
Sample Output
Enter a number: 200
200 expressed as product of prime numbers is:
2 x 2 x 2 x 5 x 5
|
Sample Output
Enter a number: 19
19 expressed as product of prime numbers is:
19
|
Submission
Guidelines:
-
No Late
Submissions will be entertained
-
Copied Assignments will be marked Zero.
No comments:
Post a Comment