LAB 5(loops, pattern problem using loops)
1. Write a program which calculates n cr .
2. Write a program to find next prime palindrome. A user will enter a number ‘n’ and we have
to find a number greater than ‘n’ which is a prime as well as palindrome. For example, if
input is 7 than next number is 11, which is prime as well as palindrome.
Test data and expected output:
Enter a number : 12
Next prime palindrome : 101
Enter a number: 113
Next prime palindrome : 131
3. Write a C program to find HCF and LCM of two numbers.
4. Write a C program that accepts a non-negative integer from the keyboard and checks
whether the entered number is a Armstrong number. (An n-digit number that is the sum of
the n-th powers of its digits is called an Armstrong number)
Test data and expected output:
Enter a non-negative integer:-2 Input must be non-negative integer
Enter a non-negative integer:9 9 is an Armstrong number
Enter a non-negative integer:1634 1634 is an Armstrong number
Enter a non-negative integer:1636 1636 is NOT an Armstrong number
5. A perfect number is a positive number in which sum of all positive divisors excluding that
number is equal to that number. Write a C program that accepts a positive integer from the
keyboard and checks whether the entered number is a perfect number.
Test data and expected output:
Enter a positive integer:-2
Input must be positive
Enter a positive integer:8128
8128 is a perfect number
Enter a positive integer:28
28 is a perfect number
Enter a positive integer:64
64 is NOT a perfect number
6. Write a C program that prints out the prime numbers between 1 and 100. The output
should be such that each row contains a maximum of 7 prime numbers.
Expected output:
The prime numbers between 1 and 100 are:
2 3 5 7 11 13 17
19 23 29 31 37 41 43
47 53 59 61 67 71 73
79 83 89 97
7. Write a C program that reads a real number x from the keyboard and calculates the sum of
the exponential by adding terms as long as |tn| > 10−8. Also, print out the value from the C
math library function exp(x).
Test data and expected output:
Enter the value of x:1.5
Sum of the series at 1.50 is 4.48169e+00
Value from C math library=4.48169e+00
Enter the value of x:-20
Sum of the series at -20.00 is 2.06115e-09
Value from C math library=2.06115e-09
8. Write a C program to print the following pyramid patterns
* * 1 1 A
* * *** 2 2 2 3 B C
* * * ***** 3 3 3 4 5 6 D E F
* * * * *** 4 4 4 4 7 8
10 9 G H I J
* * * * * * 5 5 5 5 5 11 12 13 14 15 K L M N O
9. Write a C program to print following patterns
ABCDEDCBA **********
BCDEDCB **** ****
CDEDC *** ***
DED ** **
E * *