Problem 1

Write a program to print Pascal's triangle , take no of rows as input from the user .
Eg
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Solution
Problem 2

Take n as input.
Then, take n distinct integers as input. Find the second largest of these n integers.
Do not use arrays.

Solution
Problem 3

Write a C program to accept a stream of integers from the user through keyboard, and print the mean of the numbers after each entry.
The program should terminate when the input number is -1. ( No array to be used)

For eg: Input : 1 2 3 4 5 -1
Output : 1 1.5 2 2.5 3

Solution
Problem 4

Find the first 5 numbers such that the sum of cubes of its digits is the number itself.

Find the nth armstrong number.

Solution
Problem 5

Write a C program to calculate the sum of 10 positive integers entered by the user through the keyboard.
If the user enters a negative value, the program should exit and output (display) the sum upto that step.
If the input number is greater than 100, it should be ignored in computation of sum.
Note: Use infinite while loop, Break and Continue statements to solve this problem.

Solution
Problem 6

Write a program to print Pascal's triangle , take no of rows as input from the user .

Eg
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Solution
Problem 7

Write a C-program that reads an integer N, and prints the sum of prime numbers, which are less than N.
Run your program to provide results for the following values of N.
2, 4, 8, 16, 32, 64 and 128

Solution
Problem 8

Write a C-program that computes the following sum of series within an accuracy of m decimal places given an input x if it converges within the number of iterations K.
If it does not converge, it will print a message: “The sum not converging.”.
x + (1/2)x2+(1/3)x3+…

If it converges print the number of iterations required for convergence and also the error between the above sum and ln(1/(1-x)).

Run your program to provide results for the following values of x by
taking values of (m,K) as (5,100), (3, 50), and (7, 1000).
.5, 2, .04, -2, 1, 100

Solution
Problem 9

Cosine function can be expressed as the following series:

cosine(x) = 1 - (1/6!)x6
Write a C program which shall take a floating point variable x and evaluate the above cosine series to the 10th term and print the approximate value of cosine(x).
Your program should compute each successive term based on the previously computed terms
You cannot use any mathematical function from the math library

Solution
Problem 10

Write a C program which reads an integer n and then prints the sum of digits of n.
For example, if n is 2020 it should print 4.

Solution
Problem 11

Suppose we want to evaluate the series


f(x) = 1 - x/1! + x2/2! - x3/3! + x4/4! - …
for 0 < x ≤ 1 (x is a floating point number)
upto a given number of terms.

Write a C program that will do the following:

Read in the values of x and an integer n

 If either the values entered for x does not satisfy 0 < x ≤ 1, or n is ≤ 0, print a message asking the user to enter BOTH the values again, and read the values again (must use a while loop)
 Continue the above two steps until values entered satisfy both 0 < x ≤ 1 and n is positive
 Now compute the value of f(x) upto n terms and print it (must use a for loop)
You cannot use any mathematical function like pow etc. (50% penalty if you use it)

Hint:

 If you try to compute each term independently, the factorial will soon get very large even for small n
 So think how you can generate the next term from the previous term (that you already computed)
 Note that the first term is 1 = x0/0!
 The second term is (x/1)(x0/0!) = x/1!
 The third term is (x/2)(x/1!) = x2/2!
 You should get a hint now
 Decide on the sign of the term
 Remember the term number (1st, 2nd, 3rd,…) with a variable i (initialize it properly)
 If i is even, multiply the term with -1 before adding

Solution
Problem 12

In this assignment, you will read a line of alphanumeric characters (alphabets or digits), and replace them with the following code:

 A lowercase alphabet should be replaced with the lowercase alphabet just before it (in cyclic order).
 Example: ‘a’ should be replaced with ‘z’ (as ‘z’ is just before ‘a’ in the cyclic order), ‘b’ should be replaced with ‘a’, ‘c’ should be replaced with ‘b’, and so on
 An uppercase alphabet should be replaced with the lowercase equivalent of the alphabet just before it (in cyclic order).
 Example: ‘A’ should be replaced with ‘z’ (as ‘Z’ is just before ‘A’ in the cyclic order), ‘B’ should be replaced with ‘a’, ‘C’ should be replaced with ‘b’, and so on
 A digit should be replaced with the digit just after it (in cyclic order)
 So ‘0’ should be replaced by ‘1’, ‘1’ should be replaced by ‘2’, ‘2’ should be replaced by ‘3’, …, ‘9’ should be replaced by ‘0’
 Enter the line of characters from the keyboard with no space or anything in between, press Enter at the end once

Approach:
 Read in the characters one by one from the keyboard until you get the character ’\n’ (the special character corresponding to Enter keypress)
 Use a while loop
 For each character read, take action as described and print the changed character
 Do NOT use an array even if you know it (will get 0 if array is sued)

Solution
Problem 13

Write a program to display all the prime numbers between two integers n1 and n2 (n1 < n2), which are taken as user input.

Solution
Problem 14

Write a program to print Floyd’s triangle upto N. For example, for N = 10, the corresponding

Floyd’s triangle is –

1
2 3
4 5 6
7 8 9 10

For N = 11, the corresponding Floyd’s triangle is –

1
2 3
4 5 6
7 8 9 10
11

Solution
Problem 15

Write a program to take successive positive integers (in the range of 0 - 1000) as input continuously until ‚ -1 is pressed by the user. Hence, print the maximum and minimum of all the numbers that have been taken as input.
Hint- You can only use do-while and are not allowed to use arrays.

Solution
Problem 16

Write a program to take an integer N as input from the user and check if it is Prime.
If N is prime, print all Narcissistic numbers between 0 and N.
Note - A Narcissistic number is a M-digit number which is equal to the sum of the Mth power
of each of its digits. For example, 153 is a Narcissistic number since 153 = 1^3+5^3+3^3.
Similarly,
1634 is a Narcissistic number since 1634 = 1^4 + 6^4 + 3^4 + 4^4.

Solution
Problem 17

A number is called a perfect number, if the number is equal to the sum of all its positive divisors except the number itself.
For example, (6 = 1 + 2 + 3, 28 = 1 + 2 + 4 + 7 + 14).
Find and print all the perfect numbers less than or equal to 1000.

Solution
Problem 18

Declare an integer array a[10].
Let the user fill up this array when the program runs.
After filling up, find the largest element.


Enter 10 integers: 4 2 7 8 1 2 2 4 7 4
Largest = 8.

Solution
Problem 19

User gives a positive integer a (>1) as input.
Compute and print its prime factorization.
You should not use array or math library.
You cannot use nested loops but can use only one for loop / while loop / do-while loop.

Enter a positive integer: 2
Prime factorization = 2.

Enter a positive integer: 4
Prime factorization = 2 x 2.

Enter a positive integer: 6
Prime factorization = 2 x 3.

Enter a positive integer: 72
Prime factorization = 2 x 2 x 2 x 3 x 3.

Enter a positive integer: 78781211
Prime factorization = 107 x 736273.

Enter a positive integer: 87654321
Prime factorization = 3 x 3 x 1997 x 4877.

Solution
Problem 20

Write a program that takes as input a name and a surname as strings, and creates a string named fullname that will contain the name and the surname separated by a space.

Enter the name: Isaac
Enter the surname: Newton
Full name = Isaac Newton

Enter the name: I.
Enter the surname: Newton
Full name = I. Newton

Solution
Problem 21

Write a program that takes as input an integer array a[10] that stores n (= at most 10) elements (scanned during execution) and finds how many elements are out of order.

An element a[i] is said to be in order if it is not smaller than a[0], a[1], ..., a[i-1] and not larger than a[i+1], a[i+2], ..., a[n-1].

Enter number of elements: 1
Enter 1 integers: 5
Not in order = 0.

Enter number of elements: 2
Enter 2 integers: 5 7
Not in order = 0.

Enter number of elements: 2
Enter 2 integers: 7 5
Not in order = 2.

Enter number of elements: 2
Enter 2 integers: 5 5
Not in order = 0.

Enter number of elements: 4
Enter 4 integers: 5 6 7 9
Not in order = 0.

Enter number of elements: 4
Enter 4 integers: 6 5 7 9
Not in order = 2.

Enter number of elements: 4
Enter 4 integers: 5 7 5 5
Not in order = 3.

Enter number of elements: 4
Enter 4 integers: 5 5 7 5
Not in order = 2.

Enter number of elements: 7
Enter 7 integers: 2 4 2 2 4 2 2
Not in order = 6.

Solution
Problem 22

Write a program where you will take a positive integer from an user and output it in reverse order.
Hint: You might have to use modulus operator. sample output of the program can be:

======================================

Please enter a positive integer to be reversed: 12345

The reversed number is: 54321

======================================

Solution
Problem 23

The following series is called Fibonacci Series:
1, 1, 2, 3, 5, 8, 13, 21, 34,...

Observe that its n-th term is simply the sum of its (n-1)th and (n-2)th terms.

Print this series up to its n-th term, where n (3 to 40) is given as input.
Your program should not use any array or function.

Examples
Enter the number of terms (3 to 40): 3
Fibonacci Series of order 3: l, l, 2.
Enter the number of terms (3 to 40): 20
Fibonacci Series of order 20: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765.

Solution
Problem 24

Write a program that takes two positive integers a and b as input, computes their GCD, and prints the result on the terminal.

Example
Enter two positive integers: 36 210
GCD 6
S Enter two positive integers: 210 36
GCD 6
S Enter two positive integers: 123456 789024
GCD 96

Solution
Problem 25

Given an integer interval [a, b] and n elements (integers), your program should determine how many numbers belong to that interval.

Examples
Enter the interval limits: 5 9
Enter the number of elements (n): 6
Enter the elements: 8-3 9-4 7 1
Result: 3

Solution