Unit #1 and Unit #2

 

Problem #1: A perfect number is a positive integer (more than 0) whose factors except the number add up to the number. For example – The factors of 6 except itself are 1, 2 and 3.1+2+3 =6.  Hence, 6 is a perfect number. Write a program which takes an integer input and prints if the input is a, “perfect number” or, “not a perfect number”.

 

Example:

Run #1:

Input number

28

Program Output:

28 is a perfect number

 

Run #2:

Input number

50

Program Output:

50 is not a perfect number

 

Problem #2: Write a program to print all the terms in the Fibonacci sequence that are less than 100.

 

Program Output:

 0 1 1 2 3 5 8 13 21 34 55 89

 

 

 

 

Unit #3

 

Problem #3:

A happy number is defined by the following process. Starting with any positive integer (>0), replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1(happy) or 4(unhappy). Write a program which taken a non-zero, positive integer input, N and outputs if it’s happy or unhappy.

Example Input/output:

·        7 is a happy number since 7 -> 49 -> 97 -> 130 -> 10 -> 1. 

·        4 is an unhappy number since 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4.

·        45 is an unhappy number since   45 -> 41->17->50->25->29->85->89->145->42->20->4

 

Problem #4:

A humble number is a positive integer (> 0) whose only prime factors are 2 or 3 or 5. Your program will take a positive integer input (> 0) and output if it’s humble or not.

Example Input/output:

·        Input: 35; Output: Not a Humble number;

·        Input: 15; Output: Humble number;

·        Input: 100; Output: Humble number;

 

 

 

 

Problem #5:

Almost prime numbers are the non-prime numbers, which are divisible by only a single prime number. In this problem, your job is to write a program, which finds out the number of almost prime numbers within a certain range.  The input to you program will be two integers (x and y), which specify the range, [x, y]. The output will be a count (n) of the non-prime numbers in that range. You can test your program with the following:

 

Interval [x,y]           Count(n)

[1, 10]                    3

           [1 , 20]                   4

 

Problem #6: In 1994, Mexico’s population was 58 million and growing at the annual rate of 7%. The United States’ population in the same year was 260 million and growing at the annual rate of 2%. If these two countries were to maintain the current rates of growth, in how many years will Mexico’s population exceed that of the United States. (Answer: 32 years)

NOTE:  Use a double variable type to store the real numbers that you will encounter in this problem.

Program Output:

Year#1   Mexico = 62.060     USA = 265.200

Year#2   Mexico = 66.404     USA = 270.504

Year#3   Mexico = 71.052     USA = 275.914

Year#4   Mexico = 76.026     USA = 281.432

Year#5   Mexico = 81.348     USA = 287.061

Year#6   Mexico = 87.042     USA = 292.802

Year#7   Mexico = 93.135     USA = 298.658

Year#8   Mexico = 99.655     USA = 304.631

Year#9   Mexico = 106.631     USA = 310.724

Year#10   Mexico = 114.095     USA = 316.939

Year#11   Mexico = 122.081     USA = 323.277

Year#12   Mexico = 130.627     USA = 329.743

Year#13   Mexico = 139.771     USA = 336.338

Year#14   Mexico = 149.555     USA = 343.064

Year#15   Mexico = 160.024     USA = 349.926

Year#16   Mexico = 171.225     USA = 356.924

Year#17   Mexico = 183.211     USA = 364.063

Year#18   Mexico = 196.036     USA = 371.344

Year#19   Mexico = 209.759     USA = 378.771

Year#20   Mexico = 224.442     USA = 386.346

Year#21   Mexico = 240.153     USA = 394.073

Year#22   Mexico = 256.963     USA = 401.955

Year#23   Mexico = 274.951     USA = 409.994

Year#24   Mexico = 294.197     USA = 418.194

Year#25   Mexico = 314.791     USA = 426.558

Year#26   Mexico = 336.826     USA = 435.089

Year#27   Mexico = 360.404     USA = 443.790

Year#28   Mexico = 385.633     USA = 452.666

Year#29   Mexico = 412.627     USA = 461.720

Year#30   Mexico = 441.511     USA = 470.954

Year#31   Mexico = 472.417     USA = 480.373

Year#32   Mexico = 505.486     USA = 489.981

 

Time in Years will be 32


 

Problem #7:

NOTE: Use the double variable type for this problem.

Calculate the value of π (up to four decimal places) from the following series:

 

               π = 4 – 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +…..n terms

 

The program will take a positive integer, n (>0) as the input, which represents the number of terms in the series.

 

               Example input/output:

·        If n =10, π=3.0418

·        If n = 100, π=3.1316

·        If n = 1000, π=3.1406