Question Updated
Question Updated
If the
input is negative, display an error message. For valid input, calculate and
display the factorial using unsigned long long.
Template.c
#include <stdio.h>
/* Include any headers here */
int main() {
int n;
unsigned long long factorial = 1;
Test Cases:
Input Output
5 Factorial = 120
10 Factorial = 3628800
-9 Error: Factorial is not defined for negative numbers.
25 Factorial = 7034535277573963776
2. Write a C program to find the sum of the digits of a given number using a
while loop. The program should handle both positive and negative integers.
If a negative number is entered, treat it as positive and compute the sum of
its digits.
Template.c
#include <stdio.h>
/* Include any headers here */
int main() {
Test Cases:
Input Output
79810215 Sum = 33
12156 Sum = 15
257893 Sum = 34
-9785463 Sum = 42
int main() {
Test Cases:
Input Output
2 2*1=2
10 2*2=4
2*3=6
2*4=8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
-9 Error: The number must be positive.
10
17 Error: The range must be positive.
-10
25 25 * 1 = 25
15 25 * 2 = 50
25 * 3 = 75
25 * 4 = 100
25 * 5 = 125
25 * 6 = 150
25 * 7 = 175
25 * 8 = 200
25 * 9 = 225
25 * 10 = 250
25 * 11 = 275
25 * 12 = 300
25 * 13 = 325
25 * 14 = 350
25 * 15 = 375
4. Write a C program that generates a number pyramid starting from a user-
defined number. Prompt the user for the starting number and the number
of rows. If the number of rows is zero or negative, display "Error: Number
of rows should be greater than zero". Otherwise, print a pyramid where the
first row has one number, the second row has two, and so on.
Template.c
#include <stdio.h>
/* Include any headers here */
int main() {
Test Cases:
Input Output
1 1
9 23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
-99 -99
7 -98 -97
-96 -95 -94
-93 -92 -91 -90
-89 -88 -87 -86 -85
-84 -83 -82 -81 -80 -79
-78 -77 -76 -75 -74 -73 -72
4 Error: Number of rows should be
-9 greater than zero.
0 0
5 12
345
6789
10 11 12 13 14
1452 1452
7 1453 1454
1455 1456 1457
1458 1459 1460 1461
1462 1463 1464 1465 1466
1467 1468 1469 1470 1471 1472
1473 1474 1475 1476 1477 1478
1479
Template.c
#include <stdio.h>
/* Include any headers here */
int main() {
int rows;
// Prompt the user to input the number of rows for Pascal's Triangle
Test Cases:
Input Output
5 1
11
121
1331
14641
1 1
3 1
11
121
7 1
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1