0% found this document useful (0 votes)
5 views6 pages

Question Updated

ok

Uploaded by

bejjiprasad2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Question Updated

ok

Uploaded by

bejjiprasad2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Write a C program to find the factorial of a number using a for loop.

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() {

/* Put your declarations here */

int n;
unsigned long long factorial = 1;

/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

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() {

/* Put your declarations here */

long long int num, sum = 0, remainder;

/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

Test Cases:
Input Output
79810215 Sum = 33
12156 Sum = 15
257893 Sum = 34
-9785463 Sum = 42

3. Write a C program that generates a multiplication table for a user-defined


positive integer. The program should prompt the user to input a positive
integer for the number and the range. It should display the table in the
format number * i = result, where i ranges from 1 to the specified range.
The program must raise an error if the user inputs a negative number or
zero for either value.
Template.c
#include <stdio.h>
/* Include any headers here */

int main() {

/* Put your declarations here */

int number, range;


/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

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() {

/* Put your declarations here */

int start, rows;


int num;

/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

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

5. Write a C program that prints Pascal's Triangle for a user-defined number


of rows. The program should prompt the user to input a positive integer
representing the number of rows, and it should generate Pascal's Triangle
in a properly aligned triangular format.

Template.c
#include <stdio.h>
/* Include any headers here */

int main() {

/* Put your declarations here */

int rows;

// Prompt the user to input the number of rows for Pascal's Triangle

/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

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

You might also like