Question Bank › Technical › C › Coding Questions
Write a C program that reads two integers from the user and prints the bigger of the two.
#include <stdio.h>
int main()
{
int a, b;
printf("Enter first number : ");
scanf("%d", &a);
printf("Enter second number : ");
scanf("%d", &b);
if (a > b)
printf("Bigger number : %d\n", a);
else
printf("Bigger number : %d\n", b);
return 0;
}What is the output of the following C program?
```c
int x = 5;
if (x == 5)
printf("x is five");
else
printf("x is not five");
```Write a C program that asks the user to enter an integer and prints whether it is a positive number or a negative number. (Treat 0 as positive, as in the original.)
#include <stdio.h>
int main()
{
int value;
printf("Enter an integer: ");
scanf("%d", &value);
if (value >= 0)
{
printf("%d is a positive number\n", value);
}
else
{
printf("%d is a negative number\n", value);
}
return 0;
}Write a C program that reads two integers from the user and prints the smaller of the two. For example, if the user enters 18 and 7, the program should print the smallest number as 7.
#include <stdio.h>
int main()
{
int a, b;
printf("Enter first number : ");
scanf("%d", &a);
printf("Enter second number : ");
scanf("%d", &b);
if (a < b)
printf("Smallest number : %d\n", a);
else
printf("Smallest number : %d\n", b);
return 0;
}Write a C program that prints the word "Delhi" and then, using the \n escape sequence, prints the word "Mumbai" on the next line.
#include <stdio.h>
int main()
{
printf("Delhi\nMumbai");
return 0;
}Write a C program that prints the word "Namaste" followed by a horizontal tab (using the \t escape sequence) and then the word "Bharat", all on a single line.
#include <stdio.h>
int main()
{
printf("Namaste\tBharat");
return 0;
}Write a C program that demonstrates the \b (backspace) escape sequence. The program should print "Hello\b Programmer " using a single printf statement and return 0 from main.
#include <stdio.h>
int main()
{
printf("Hello\b Programmer ");
return 0;
}Write a C program that prints 'This is a beep' followed by a bell sound using the \a escape sequence. The output should be: This is a beep (and then the bell sound).
#include <stdio.h>
int main() {
printf("This is a beep\a");
return 0;
}Write a C program that prints the text 'Hello Coder' enclosed in single quotes using escape sequences. The output should be exactly: 'Hello Coder' (including the single quotes).
#include <stdio.h>
int main() {
printf("\'Hello Coder\'");
return 0;
}Write a C program that prints the following line exactly, including the double quotes around the greeting: "Namaste Coder" Note: the escape sequence \" prints a double quote character.
#include <stdio.h>
int main()
{
printf("\"Namaste Coder\"\n");
return 0;
}Write a C program that prints the following line exactly, using escape sequences for the backslash characters: C\practice\code\daily.in Note: the escape sequence \\ prints a single backslash.
#include <stdio.h>
int main()
{
printf("C\\practice\\code\\daily.in\n");
return 0;
}Write a C program that prints the text: \f - form feed sign. (i.e. it should output a backslash character followed by the letter f, then the rest of the sentence).
#include <stdio.h>
int main()
{
printf("\\f - form feed sign. \n");
return 0;
}Write a C program that uses the vertical tab escape sequence (\v) inside a printf statement to print the text "\v - vertical tab demo. " and then returns 0 from main.
#include <stdio.h>
int main()
{
printf("\v - vertical tab demo. ");
return 0;
}Write a C program that prints the text "Namaste" followed by a null character escape sequence (\0) and then the word "Bharat", all using a single printf statement. The program should return 0 from main.
#include <stdio.h>
int main()
{
printf("Namaste \0 Bharat");
return 0;
}Write a C program that demonstrates the \r (carriage return) escape sequence. The program should print "\nab" first, then print "\bsi", and finally print "\rha". Use printf statements and return 0 from main.
#include <stdio.h>
int main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
return 0;
}Write a C program that opens a text file named "nums.txt" containing three integers separated by spaces, reads the three integers from it, and prints them on the screen. If the file cannot be opened, print an error message.
#include <stdio.h>
int main(void) {
int a, b, c;
FILE *fp;
fp = fopen("nums.txt", "r");
if (fp == NULL) {
printf("Error: could not open nums.txt\n");
return 1;
}
if (fscanf(fp, "%d %d %d", &a, &b, &c) != 3) {
printf("Error: file does not contain three integers\n");
fclose(fp);
return 1;
}
printf("The three integers are: %d, %d, %d\n", a, b, c);
fclose(fp);
return 0;
}
Write a C program that asks the user for a number and writes its multiplication table (from 1 to 10) in the text format "n x i = result", one line per entry, into a file named "table.txt". The file must be created/overwritten and properly closed so it is readable and well formatted.
#include <stdio.h>
int main(void) {
int n, i;
FILE *fp;
printf("Enter a number: ");
if (scanf("%d", &n) != 1) {
printf("Invalid input\n");
return 1;
}
fp = fopen("table.txt", "w");
if (fp == NULL) {
printf("Could not open file\n");
return 1;
}
for (i = 1; i <= 10; i++) {
fprintf(fp, "%d x %d = %d\n", n, i, n * i);
}
fclose(fp);
printf("Table written to table.txt\n");
return 0;
}
Write a C program that attempts to open a file named "data.txt" for reading. Before opening, check if the file exists. If it does not exist, print "File not found". If it exists, open it and print "File opened successfully". Finally, close the file if it was opened.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
if ((fp = fopen("data.txt", "r")) == NULL) {
printf("File not found\n");
} else {
printf("File opened successfully\n");
fclose(fp);
}
return 0;
}Write a C program that prints the following number pattern using nested loops: the first line shows 1 2 3 4, the second line shows 1 2 3, the third line shows 1 2, and the last line shows 1. Each number should be printed without spaces between them (i.e. 1234, 123, 12, 1).
#include <stdio.h>
int main()
{
int row, col;
for(row = 4; row >= 1; row--)
{
for(col = 1; col <= row; col++)
{
printf("%d", col);
}
printf("\n");
}
return 0;
}Write a C program that prints the following pattern using nested for loops. The first (outer) loop decides how many lines to print; the second (inner) loop prints consecutive natural numbers starting from 1, continuing across lines. Print each number followed by a space and start a new line after each row. The output should be: 1 2 3 4 5 6 7 8 9 10
#include <stdio.h>
int main()
{
int a, i, k = 1;
for(a = 1; a < 5; a++)
{
for(i = 1; i <= a; i++)
{
printf("%d ", k++);
}
printf("\n");
}
return 0;
}Write a C program that prints the following pattern using nested for loops. The first (outer) loop decides how many lines to print; the second (inner) loop prints the digits on each line. For line number a (starting from 1), print the digits 1 to a, but print 1 when the digit is odd and 0 when the digit is even, each followed by a space. So the output should be: 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1
#include <stdio.h>
int main()
{
int a, i;
for(a = 1; a <= 5; a++)
{
for(i = 1; i <= a; i++)
{
printf("%d ", (a - i + 1) % 2);
}
printf("\n");
}
return 0;
}Write a C program that prints the following alphabet pattern using nested loops: A A B A B C A B C D A B C D E Each row starts from 'A' and continues up to the letter matching the row number (row 1 ends at 'A', row 2 ends at 'B', and so on).
#include <stdio.h>
int main()
{
int row, ch;
for(row = 65; row <= 69; row++)
{
for(ch = 65; ch <= row; ch++)
{
printf("%c ", ch);
}
printf("\n");
}
return 0;
}Write a C program to print the following number pattern: 54321 4321 321 21 1
#include <stdio.h>
int main() {
for (int i = 5; i >= 1; i--) {
for (int j = i; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
return 0;
}Write a C program to print a square pattern of stars. The program should read an integer n from the user and print an n x n square of asterisks (*). For example, if n = 4, the output should be: **** **** **** ****
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter the size of the square: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints a mirror half diamond star pattern (Part 1) with 5 rows. The first row has 4 leading spaces followed by 1 star, and each following row has one fewer leading space and one more star, so the stars are right-aligned and increasing. The expected output is:
*
**
***
****
*****#include<stdio.h>
int main()
{
int i, j, k;
for(i = 1;i <= 5;i++)
{
for(j = 5;j > i;j--)
{
printf(" ");
}
for(k = 1;k <= i;k++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints a mirror half diamond star pattern (Part 2) with 4 rows. The first row has 4 stars, and each following row has one fewer star, with the stars right-aligned (leading spaces increase by one each row). The expected output is: **** *** ** *
#include<stdio.h>
int main()
{
int i, j, k;
for(i = 1;i <= 4;i++)
{
for(j = 1;j < i;j++)
{
printf(" ");
}
for(k = 4;k >= i;k--)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program to print a half diamond star pattern with 6 rows in the upper half and 5 rows in the lower half, like: * ** *** **** ***** ****** ***** **** *** ** *
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 6; i++) {
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
for (i = 1; i <= 5; i++) {
for (j = i; j <= 5; j++) {
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints a right-angle triangle star pattern with 5 rows: row 1 has one '*', row 2 has two '*', row 3 has three '*', row 4 has four '*', and row 5 has five '*'. Each row must be printed on its own line.
#include<stdio.h>
int main()
{
int i, j;
for(i = 1; i <= 5; i++)
{
for(j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints a mirrored right-angle triangle of 5 rows. In row 1 print four '0' characters followed by one '*'; in row 2 print three '0' characters followed by two '*'; and so on, until row 5 prints five '*' characters with no '0'. Each row ends with a newline.
#include<stdio.h>
int main()
{
int i, j, k;
for(i = 1; i <= 5; i++)
{
for(j = 5; j > i; j--)
{
printf("0");
}
for(k = 1; k <= i; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints the upper (top) half of a diamond star pattern using the digit 0 in place of spaces. For 5 rows, the output must be: 0000* 000*** 00***** 0******* ********* (Each row i from 1 to 5 has (5-i) zeros followed by (2*i-1) stars.)
#include<stdio.h>
int main()
{
int i, j, k;
for(i = 1; i <= 5; i++)
{
for(j = 5; j > i; j--)
{
printf("0");
}
for(k = 1; k <= (2*i - 1); k++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints the lower half of a diamond pattern with 4 rows using '0' for leading zeros and '*' for the rest. Row 1: 0 followed by 7 stars; Row 2: 00 followed by 5 stars; Row 3: 000 followed by 3 stars; Row 4: 0000 followed by 1 star. So the output is: 0******* 00***** 000*** 0000*
#include <stdio.h>
int main()
{
int i, j, k, m = 8;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= i; j++)
{
printf("0");
}
for (k = i; k < m; k++)
{
printf("*");
}
m -= 1;
printf("\n");
}
return 0;
}Write a C program that reads an integer n from the user and prints a rhombus (parallelogram) star pattern of n rows, where each row has n stars and the rows are shifted right by one space each time (first row has n-1 leading spaces, last row has 0). For example, if the user enters 4, the output should be: **** **** **** ****
#include <stdio.h>
int main()
{
int i, j, k, n;
printf("Enter a number: ");
scanf("%d", &n);
printf("\n");
for (i = 0; i < n; i++)
{
for (k = 0; k < n - 1 - i; k++)
{
printf(" ");
}
for (j = 0; j < n; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints an alphabet pyramid of 5 rows. Row i (1-based) has (5-i) leading spaces, then the letters A, B, C, ... in increasing order up to i letters, then the same letters in decreasing order back to A. For example, row 3 should be " ABCBA" (two spaces then ABCBA).
#include <stdio.h>
int main() {
int i, j, k;
char ch;
for (i = 1; i <= 5; i++) {
for (j = 5; j > i; j--)
printf(" ");
ch = 'A';
for (k = 1; k <= i; k++)
printf("%c", ch++);
ch -= 2;
for (k = 1; k < i; k++)
printf("%c", ch--);
printf("\n");
}
return 0;
}Write a C program that reads an integer n from the user and prints a number pyramid of n rows. In row i (1-based), there are (n-i) leading spaces, then the numbers 1 to i in increasing order, then the numbers i-1 down to 1 in decreasing order. For example, if n = 4 the output should be: 1 121 12321 1234321
#include <stdio.h>
int main() {
int n, i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++)
printf(" ");
for (k = 1; k <= i; k++)
printf("%d", k);
for (k = i - 1; k >= 1; k--)
printf("%d", k);
printf("\n");
}
return 0;
}Write a C program that reads an integer n from the user and prints a solid square of '*' characters with n rows and n columns. For example, if n = 4 the output should be: **** **** **** ****
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program to print a pyramid pattern of stars. The program should read the number of rows from the user and then print a pyramid where the first row has 1 star, the second row has 3 stars, and so on, with each row centered using spaces. For example, if the user enters 4, the output should be: * *** ***** *******
#include <stdio.h>
int main() {
int rows, spaces;
printf("Enter the number of rows: ");
scanf("%d", &rows);
spaces = rows - 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= spaces; j++) {
printf(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
printf("*");
}
spaces--;
printf("\n");
}
return 0;
}Write a C program that prints an inverted pyramid star pattern. Read the number of rows n from the user. Row 1 has (2n-1) stars with no leading spaces; each next row has one more leading space and two fewer stars, so the last row has exactly 1 star centred under the first. For example, if n = 4 the output should be: ******* ***** *** *
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
int spaces = 0;
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= spaces; j++)
{
printf(" ");
}
for (int k = 1; k <= 2 * i - 1; k++)
{
printf("*");
}
spaces++;
printf("\n");
}
return 0;
}Write a C program that prints a half diamond star pattern. Read the number of rows n from the user, then print an increasing triangle of stars with rows of 1, 2, ..., n stars, followed by a decreasing triangle with rows of n-1, n-2, ..., 1 stars. For example, if n = 4 the output should be: * ** *** **** *** ** *
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
for (int i = n - 1; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program to print the following number pattern. Replace each 0 with a space. The pattern for 5 lines is:
5
45
345
2345
12345
(Note: The spaces before the numbers are shown as blanks; the numbers are printed without spaces between them.)#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) {
printf(" ");
}
for (int j = n - i + 1; j <= n; j++) {
printf("%d", j);
}
printf("\n");
}
return 0;
}Write a C program that prints a centred pyramid star pattern with 5 rows. Row 1 has 1 star, row 2 has 3 stars, row 3 has 5 stars, and so on (row i has 2i-1 stars), with leading spaces so the pyramid is centred. Use nested loops.
#include<stdio.h>
int main()
{
int i, j, k;
for(i = 1;i <= 5;i++)
{
for(j = 5;j > i;j--)
{
printf(" ");
}
for(k = 1;k <= ((i*2)-1);k++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints an inverted pyramid star pattern with 5 rows. The first row should have 9 stars, and each subsequent row should have 2 fewer stars, with the stars centred (leading spaces increase by 1 each row). Use nested loops.
#include<stdio.h>
int main()
{
int i, j, k, m = 9;
for(i = 1;i <= 5;i++)
{
for(j = 1;j < i;j++)
{
printf(" ");
}
for(k = 1;k <= m;k++)
{
printf("*");
}
m = m - 2;
printf("\n");
}
return 0;
}Write a C program to print a mirror half diamond star pattern with 6 rows in the upper half and 5 rows in the lower half, where each row has leading spaces so that the stars are right-aligned, like:
*
**
***
****
*****
******
*****
****
***
**
*#include <stdio.h>
int main() {
int i, j, k;
for (i = 1; i <= 6; i++) {
for (j = 6; j > i; j--) {
printf(" ");
}
for (k = 1; k <= i; k++) {
printf("*");
}
printf("\n");
}
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf(" ");
}
for (k = 5; k >= i; k--) {
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints a hollow diamond enclosed in a square of stars. For size 5 the output must be: ********** **** **** *** *** ** ** * * ** ** *** *** **** **** ********** (The top half shrinks the inner gap as rows go down; the bottom half grows it back.)
#include<stdio.h>
int main()
{
int i, j, k, m, a = 1, b = 0;
for(i = 5; i > 0; i--)
{
for(j = i; j > 0; j--)
printf("*");
for(k = 1; k < a; k++)
printf(" ");
a = a + 2;
for(m = i; m > 0; m--)
printf("*");
printf("\n");
}
for(i = 1; i <= 4; i++)
{
for(j = 0; j <= i; j++)
printf("*");
for(k = 6; k > b; k--)
printf(" ");
b = b + 2;
for(m = 0; m <= i; m++)
printf("*");
printf("\n");
}
return 0;
}Write a C program that prints a full diamond star pattern using '0' as the filler character for the left padding and '*' for the diamond. The top half has 5 rows: row i (1 to 5) prints (5-i) zeros, then (2*i-1) stars. The bottom half has 4 rows: row i (1 to 4) prints i zeros, then (9-2*i) stars. Each row ends with a newline.
#include<stdio.h>
int main()
{
int i, j, k;
for(i = 1; i <= 5; i++)
{
for(j = 5; j > i; j--)
printf("0");
for(k = 1; k <= 2*i-1; k++)
printf("*");
printf("\n");
}
for(i = 1; i <= 4; i++)
{
for(j = 1; j <= i; j++)
printf("0");
for(k = 1; k <= 9-2*i; k++)
printf("*");
printf("\n");
}
return 0;
}Write a C program that prints the first part of a hollow diamond star pattern: a triangle of 5 rows where each row has leading spaces followed by two stars separated by increasing spaces (the top half of a hollow diamond). Specifically, for rows 1 to 5, print (5-row) spaces, then a star, then (2*row-3) spaces (none for row 1), then another star, then a newline. For row 1 print just a single star. Use a loop-based approach.
#include<stdio.h>
int main()
{
int row, s, gap;
for(row = 1; row <= 5; row++)
{
for(s = row; s < 5; s++)
printf(" ");
printf("*");
if(row > 1)
{
for(gap = 1; gap < 2*row-2; gap++)
printf(" ");
printf("*");
}
printf("\n");
}
return 0;
}Write a C program to print a hollow pyramid star pattern. The program should read an integer n (the number of rows) from the user and print a pyramid where the first row has one star, the last row is a solid line of stars, and the intermediate rows have stars at the edges with spaces inside. For example, if n = 5, the output should be:
*
* *
* *
* *
*********#include <stdio.h>
int main() {
int n, i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
// Print leading spaces
for (k = 1; k <= n - i; k++)
printf(" ");
// Print stars and spaces for the pyramid
for (j = 1; j <= 2 * i - 1; j++) {
if (i == n || j == 1 || j == 2 * i - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Write a C program to print a hollow right-angled triangle star pattern. The program should read an integer n (the number of rows) from the user and print the pattern as described: the first row has one star, the last row has n stars, and for intermediate rows, there is a star at the beginning and at the end (with spaces in between). For example, if n = 5, the output should be: * ** * * * * *****
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
if (j == 1 || j == i || i == n)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Write a C program to print a mirrored hollow right angle triangle star pattern. The program should read an integer num from the user and print num lines. The pattern is right-aligned: each line starts with (num - i) spaces, followed by stars and spaces such that the first character after the spaces is a star, the last character is a star, and the interior is hollow except for the last line which is completely filled with stars. For example, if num = 5, the output should be:
*
**
* *
* *
*****#include <stdio.h>
int main() {
int i, j, k, num;
printf("Enter a number: ");
scanf("%d", &num);
printf("\n");
for (i = 0; i < num; i++) {
for (k = 0; k < num - i - 1; k++) {
printf(" ");
}
for (j = 0; j <= i; j++) {
if (j == 0 || j == i || i == num - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Write a C program to print a pyramid pattern of stars with 5 rows, where each row i (1-indexed) has (2*i - 1) stars, and the stars are preceded by (5 - i) zeros. For example, the first row has 4 zeros followed by 1 star, the second row has 3 zeros followed by 3 stars, and so on.
#include <stdio.h>
int main() {
int i, j, k;
for (i = 1; i <= 5; i++) {
for (j = 5; j > i; j--) {
printf("0");
}
for (k = 1; k <= (2 * i - 1); k++) {
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that reads an integer n from the user and prints a hollow square of side n using '*' characters, with both diagonals also drawn with '*'. For example, if n = 5 the output should be: ***** ** ** * * * ** ** ***** (Every border cell, every main-diagonal cell and every anti-diagonal cell is a '*'; all other cells are spaces.)
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == 1 || i == n || j == 1 || j == n || i == j || j == n - i + 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Write a C program to print a right arrow star pattern. Read the number of columns n from the user. For n = 5 the output should be:
*****
****
***
**
*
**
***
****
*****#include <stdio.h>
int main(void) {
int n;
printf("Enter the number of columns");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
printf(" ");
for (int k = 1; k <= n - i; k++)
printf("*");
printf("\n");
}
for (int i = 1; i < n; i++)
{
for (int j = 1; j < n - i; j++)
printf(" ");
for (int k = 1; k <= i + 1; k++)
printf("*");
printf("\n");
}
return 0;
}Write a C program that prints a left-pointing arrow made of '*' characters. Read an integer n (number of columns) from the user. The upper part has n rows: row i (1 to n) prints (n-i) spaces followed by (n-i+1) stars. The lower part has n-1 rows: row i (1 to n-1) prints i spaces followed by (i+1) stars. For example, if n = 4 the output should be: **** *** ** * ** *** ****
#include <stdio.h>
int main(void) {
int n;
printf("Enter the number of columns: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int k = 0; k <= n - i; k++) {
printf("*");
}
printf("\n");
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= i; j++) {
printf(" ");
}
for (int k = 1; k <= i + 1; k++) {
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints a plus (+) pattern using the character '+'. The program should read an odd number n (the size of the pattern) from the user and print an n×n grid where the middle row is completely filled with '+' and every other row has a single '+' in the middle column. For example, if n = 5 the output should be: + + +++++ + +
#include <stdio.h>
int main(void) {
int n;
printf("Enter an odd number: ");
scanf("%d", &n);
int mid = n / 2 + 1;
for (int i = 1; i <= n; i++) {
if (i == mid) {
for (int j = 1; j <= n; j++) {
printf("+");
}
} else {
for (int j = 1; j <= n / 2; j++) {
printf(" ");
}
printf("+");
}
printf("\n");
}
return 0;
}Write a C program that reads the order of two matrices (up to 10 x 10), reads the elements of matrix A and matrix B, and prints their sum matrix (element-wise addition).
#include <stdio.h>
int main()
{
int i, j, rows, cols, a[10][10], b[10][10];
printf("Enter the order of the matrices (rows cols): \n");
scanf("%d %d", &rows, &cols);
printf("Enter the elements of matrix A:\n");
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
scanf("%d", &a[i][j]);
printf("Enter the elements of matrix B:\n");
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
scanf("%d", &b[i][j]);
printf("Matrix Addition:\n");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
printf("%4d", a[i][j] + b[i][j]);
printf("\n");
}
return 0;
}Write a C program that reads an integer from the user and prints its multiplication table from 1 to 10, in the format shown. For example, if the user enters 7, the output should be: 1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49 8 * 7 = 56 9 * 7 = 63 10 * 7 = 70
#include <stdio.h>
int main()
{
int n, i;
printf("Enter a number : ");
scanf("%d", &n);
for(i = 1; i <= 10; i++)
{
printf("\n%d * %d = %d", i, n, i * n);
}
return 0;
}Write a C program that acts as a simple arithmetic calculator. Read two integers from the user, then display a menu: 1 for Addition, 2 for Subtraction, 3 for Multiplication, 4 for Division, and any other key to exit. Read the user's choice and use a switch statement to perform the selected operation, printing the result in the form 'a op b = result'. For example, if the user enters 15 and 4 and then chooses 1, the program should print '15 + 4 = 19'.
#include <stdio.h>
int main()
{
int num1, num2, choice, answer;
printf("Enter any two integers : ");
scanf("%d %d", &num1, &num2);
printf("\nPress 1 for Addition");
printf("\nPress 2 for Subtraction");
printf("\nPress 3 for Multiplication");
printf("\nPress 4 for Division");
printf("\nPress other key to exit");
printf("\n\nEnter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
answer = num1 + num2;
printf("\n%d + %d = %d", num1, num2, answer);
break;
case 2:
answer = num1 - num2;
printf("\n%d - %d = %d", num1, num2, answer);
break;
case 3:
answer = num1 * num2;
printf("\n%d * %d = %d", num1, num2, answer);
break;
case 4:
answer = num1 / num2;
printf("\n%d / %d = %d", num1, num2, answer);
break;
default:
printf("\nInvalid Choice");
}
return 0;
}Write a C program that asks the user for a limit n, then reads n integers in a loop, and prints the biggest of them. Use two temporary variables: store the first number in temp1, and for each subsequent number stored in temp2, update temp1 if temp2 is greater.
#include <stdio.h>
int main()
{
int num, limit, i, temp1 = 0, temp2 = 0;
printf("\nEnter the limit : ");
scanf("%d", &limit);
for(i = 0; i < limit; i++)
{
printf("\nEnter number %d : ", i + 1);
scanf("%d", &num);
if(i == 0)
temp1 = num;
else
{
temp2 = num;
if(temp2 > temp1)
temp1 = temp2;
}
}
printf("The biggest number is %d", temp1);
return 0;
}Write a C program to find the largest among n numbers entered by the user. First, read the count n, then read n numbers one by one, and finally display the largest number. Use a temporary variable to keep track of the maximum.
#include <stdio.h>
int main() {
int n, i, num, max = 0;
printf("Enter how many numbers: ");
scanf("%d", &n);
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &num);
if (i == 0 || num > max) {
max = num;
}
}
printf("The largest number is %d\n", max);
return 0;
}Write a C program to subtract two matrices of the same order. The program should first read the number of rows and columns (up to 10 each), then read the elements of the first matrix, then the elements of the second matrix, and finally print the resulting matrix after subtraction.
#include <stdio.h>
int main() {
int rows, cols;
int A[10][10], B[10][10], C[10][10];
int i, j;
printf("Enter the number of rows and columns (max 10): ");
scanf("%d %d", &rows, &cols);
printf("Enter elements of matrix A:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Enter elements of matrix B:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &B[i][j]);
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
C[i][j] = A[i][j] - B[i][j];
}
}
printf("Result of matrix subtraction:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%4d", C[i][j]);
}
printf("\n");
}
return 0;
}Write a C program that prints the message "Welcome to C programming!" to the screen.
#include <stdio.h>
int main() {
printf("Welcome to C programming!\n");
return 0;
}Write a C program that declares an int variable named TOTAL (all uppercase letters) and initializes it to 7, then prints its value in the format: The value of TOTAL = 7
#include <stdio.h>
int main()
{
int TOTAL = 7;
printf("The value of TOTAL = %d\n", TOTAL);
return 0;
}Write a C program that declares a constant integer named LIMIT with value 50 using the const keyword, prints its value, and includes a commented-out line showing that modifying it would cause a compile error.
#include <stdio.h>
int main()
{
const int LIMIT = 50;
printf("The value of LIMIT = %d\n", LIMIT);
/* LIMIT = LIMIT + 1; // ERROR: assignment to read-only variable */
return 0;
}Write a C program that demonstrates a constant value defined WITHOUT using the const keyword. Define a constant named MAX_MARKS with value 75 using a preprocessor directive, print its value, and then show (in a comment) that trying to change it would cause a compile error.
#include <stdio.h>
#define MAX_MARKS 75
int main()
{
printf("The value of MAX_MARKS = %d\n", MAX_MARKS);
/* MAX_MARKS = MAX_MARKS + 1; // ERROR: cannot assign to a macro constant */
return 0;
}Write a C program to demonstrate the purpose of a volatile variable. Declare a volatile integer variable named 'total' and initialize it to 10. Then increment its value by 5 and print the result.
#include <stdio.h>
int main() {
volatile int total = 10;
total = total + 5;
printf("The value of total = %d\n", total);
return 0;
}Write a C program to demonstrate the purpose of a volatile variable. Declare an integer variable named 'count' and initialize it to 5. Then increment its value by 3 and print the result. Do not use the volatile keyword.
#include <stdio.h>
int main() {
int count = 5;
count = count + 3;
printf("The value of count = %d\n", count);
return 0;
}Write a C program that declares a char variable named 'grade' and assigns it the character 'A'. Then print the character using printf in the format: "The character of grade = A" (without quotes).
#include <stdio.h>
int main()
{
char grade = 'A';
printf("The character of grade = %c", grade);
return 0;
}Write a C program that declares an integer variable named 'marks' and assigns it the value 75. Then print the value using printf in the format: "The value of marks = 75" (without quotes).
#include <stdio.h>
int main()
{
int marks = 75;
printf("The value of marks = %d", marks);
return 0;
}Write a C program that stores the decimal value 5.123456789 in a float variable and prints it with 9 digits after the decimal point using the %0.9f format specifier.
#include <stdio.h>
int main()
{
float rate = 5.123456789;
printf("The value of rate = %0.9f", rate);
return 0;
}Write a C program that stores the decimal value 12.345678901 in a double variable and prints it using the %lf format specifier.
#include <stdio.h>
int main()
{
double price = 12.345678901;
printf("The value of price = %lf", price);
return 0;
}Write a C program that declares two int variables, one named total with value 150 and another named Total with value 275, and prints both values using %d to show that C identifiers are case-sensitive.
#include <stdio.h>
int main()
{
int total = 150;
int Total = 275;
printf("The total is %d ", total);
printf("\nThe Total is %d ", Total);
return 0;
}Write a C program that declares two integer variables: one named 'marks' with value 85 and another named 'Marks' with value 92. Print both values with appropriate labels to demonstrate that C is case-sensitive and that variable names can differ only by case.
#include <stdio.h>
int main() {
int marks = 85;
int Marks = 92;
printf("The marks is %d\n", marks);
printf("The Marks is %d\n", Marks);
return 0;
}Write a C program that reads an array of integers from the user, calculates the sum of all elements, and then checks whether the sum is divisible by the number of elements. If it is divisible, print "<sum> is divisible by <count>"; otherwise print "<sum> is not divisible by <count>". Assume the array size is at most 20.
#include <stdio.h>
int main() {
int n, i, sum = 0;
int arr[20];
printf("Enter array size : ");
scanf("%d", &n);
printf("Enter elements : ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++) {
sum += arr[i];
}
if (sum % n == 0)
printf("%d is divisible by %d\n", sum, n);
else
printf("%d is not divisible by %d\n", sum, n);
return 0;
}Write a C program that takes the name and monthly salary of two employees as input from the user and writes them to a text file named "staff.txt" in the following format:
name1, 4500
name2, 8200
(Each line should contain the name, a comma, a space, and the salary.)#include <stdio.h>
int main(void) {
char name1[50], name2[50];
int salary1, salary2;
FILE *fp;
printf("Enter name and salary of employee 1: ");
scanf("%49s %d", name1, &salary1);
printf("Enter name and salary of employee 2: ");
scanf("%49s %d", name2, &salary2);
fp = fopen("staff.txt", "w");
if (fp == NULL) {
printf("Could not open file.\n");
return 1;
}
fprintf(fp, "%s, %d\n", name1, salary1);
fprintf(fp, "%s, %d\n", name2, salary2);
fclose(fp);
printf("Data written to staff.txt\n");
return 0;
}
Write a C statement that opens a file named "marks.dat" located in the current directory for reading in binary mode. Store the file pointer in a variable named fp.
fp = fopen("marks.dat", "rb");In C, what happens when a file that already exists with some content is opened using fopen with mode "w"? Write a small C program that opens an existing file "notes.txt" in "w" mode and then writes the text "Hello" into it, and state clearly what happens to the old content.
#include <stdio.h>
int main(void) {
FILE *fp = fopen("notes.txt", "w");
if (fp == NULL) {
printf("File could not be opened\n");
return 1;
}
fprintf(fp, "Hello");
fclose(fp);
printf("Done\n");
return 0;
}Write a C program that prints the natural numbers from 10 to 20 (both inclusive), each on a new line, using a for loop whose counter variable i is initialized to 0.
#include <stdio.h>
int main() {
int i;
for (i = 0; i <= 20; i++) {
if (i >= 10) {
printf("%d\n", i);
}
}
return 0;
}Write a C program that reads an integer n from the user and prints the first n natural numbers (1 to n), each on a new line, using a for loop.
#include <stdio.h>
int main() {
int n, i;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("%d\n", i);
}
return 0;
}Write a C program that reads a number n and prints the first n natural numbers in reverse order (from n down to 1). For example, if n = 6, the output should be: 6 5 4 3 2 1.
#include <stdio.h>
int main()
{
int n, i;
printf("Enter n : ");
scanf("%d", &n);
for(i = n; i >= 1; i--)
printf("%d ", i);
return 0;
}Write a C program that reads n integers from the user and prints them in descending order (largest first). For example, if the user enters 5 numbers: 41 38 45 36 40, the program should print: 45 41 40 38 36.
#include <stdio.h>
int main()
{
int i, j, n, a[50], temp;
printf("Enter how many numbers : ");
scanf("%d", &n);
printf("Enter %d numbers : ", n);
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
for(j = i + 1; j < n; j++)
{
if(a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("----Descending Order----\n");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}Write a C program that reads an integer from the user and prints whether it is Even or Odd. For example, if the user enters 7, the program should print Odd.
#include <stdio.h>
int main()
{
int num;
printf("Enter the number : ");
scanf("%d", &num);
if (num % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}Write a C program that reads an integer from the user and prints its factorial. For example, if the user enters 6, the program should print the factorial of 6.
#include <stdio.h>
int main()
{
int n, i;
long long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
fact *= i;
}
printf("Factorial of %d is %lld\n", n, fact);
return 0;
}Write a C program that reads an integer from the user and prints its factorial. For example, if the user enters 6, the program should print 720.
#include <stdio.h>
int main()
{
int i, fact = 1, num;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 1; i <= num; i++) {
fact = fact * i;
}
printf("Factorial of %d is: %d", num, fact);
return 0;
}Write a C program that reads an integer n from the user and prints the first n Fibonacci numbers, where the series starts with 0 and 1. For example, if the user enters 6, the output should be: 0 1 1 2 3 5
#include <stdio.h>
int main()
{
int a = 0, b = 1, c, i, n;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 1)
{
printf("Invalid input\n");
}
else if (n == 1)
{
printf("%d\n", a);
}
else
{
printf("%d %d", a, b);
for (i = 3; i <= n; i++)
{
c = a + b;
a = b;
b = c;
printf(" %d", c);
}
printf("\n");
}
return 0;
}Write a C program that reads two non-negative integers from the user and prints their GCD (greatest common divisor) using a recursive function. For example, if the user enters 0 and 8, the output should be: G.C.D of 0 and 8 = 8
#include <stdio.h>
int gcd(int a, int b);
int main()
{
int a, b;
printf("Enter two non-negative integers: ");
scanf("%d %d", &a, &b);
printf("G.C.D of %d and %d = %d\n", a, b, gcd(a, b));
return 0;
}
int gcd(int a, int b)
{
if (b != 0)
return gcd(b, a % b);
else
return a;
}Write a C program to find the LCM (Least Common Multiple) of two positive integers entered by the user. For example, if the user enters 15 and 20, the program should print 'LCM of 15 and 20 is 60'.
#include <stdio.h>
int main() {
int a, b, max;
printf("Enter two positive integers: ");
scanf("%d %d", &a, &b);
max = (a > b) ? a : b;
while (1) {
if (max % a == 0 && max % b == 0) {
printf("LCM of %d and %d is %d\n", a, b, max);
break;
}
max++;
}
return 0;
}Write a C program that performs a linear search on an array of integers. The program should read the number of elements, then the elements themselves, then a value to search for. It should print the 1-based position of the FIRST occurrence of the value if found, otherwise print that the value is not present. Use a separate function that returns the index (or -1 if not found).
#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
int i;
for (i = 0; i < n; i++) {
if (arr[i] == key)
return i;
}
return -1;
}
int main() {
int arr[100], n, key, i, pos;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers: ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter value to search: ");
scanf("%d", &key);
pos = linearSearch(arr, n, key);
if (pos != -1)
printf("%d found at position %d\n", key, pos + 1);
else
printf("%d not found in the array\n", key);
return 0;
}Write a C program that searches an array for a given value and prints every position (1-based) where the value occurs, along with the total number of occurrences. If the value is absent, print a not-found message.
#include <stdio.h>
int main()
{
int arr[100], n, i, key, count = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter value to search: ");
scanf("%d", &key);
for (i = 0; i < n; i++)
{
if (arr[i] == key)
{
printf("%d is present at position %d.\n", key, i + 1);
count++;
}
}
if (count == 0)
printf("%d is not present in the array.\n", key);
else
printf("%d occurs %d time(s) in the array.\n", key, count);
return 0;
}Write a C program that performs a linear search using a separate function. The program should read the number of elements, read the elements into an array, read the value to search for, call a function to find its position, and print the position (1-based) or a not-found message.
#include <stdio.h>
int linear_search(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
{
if (arr[i] == key)
return i;
}
return -1;
}
int main()
{
int arr[100], n, i, key, pos;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter value to search: ");
scanf("%d", &key);
pos = linear_search(arr, n, key);
if (pos == -1)
printf("%d is not present in the array.\n", key);
else
printf("%d is present at position %d.\n", key, pos + 1);
return 0;
}Write a C program to print the following number pattern using nested loops. The pattern has 5 lines. On the first line, print the digit 1 once. On the second line, print the digit 2 twice. On the third line, print the digit 3 three times, and so on, until the fifth line prints the digit 5 five times. Use three for loops: the outer loop controls the line number (1 to 5), the second loop prints the current line number repeatedly, and the third loop is not needed; actually, only two loops are required. However, the problem statement mentions three for loops; you may use two loops to achieve the pattern.
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}
return 0;
}Write a C program to print the following number pattern using nested loops. The pattern has 5 lines. On the first line, print numbers from 1 to 5 consecutively (i.e., 12345). On the second line, print numbers from 2 to 5 (i.e., 2345), and so on, until the fifth line prints only 5. Use two for loops: the outer loop controls the line number and the starting value, and the inner loop prints the numbers from the current starting value up to 5.
#include <stdio.h>
int main() {
int line, num;
for (line = 1; line <= 5; line++) {
for (num = line; num <= 5; num++) {
printf("%d", num);
}
printf("\n");
}
return 0;
}Write a C program that prints a mirrored right-angled triangle of stars with 5 rows. Row 1 has 4 spaces followed by 1 star, row 2 has 3 spaces followed by 2 stars, and so on, so that row 5 has 0 spaces followed by 5 stars. Use nested for loops (one loop for the leading spaces, one for the stars).
#include<stdio.h>
int main()
{
int i, j, k;
for(i = 1; i <= 5; i++)
{
for(j = 5; j > i; j--)
{
printf(" ");
}
for(k = 1; k <= i; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that prints an inverted right-angled triangle of stars with 5 rows: the first row has 5 stars, the second row has 4 stars, and so on down to the last row with 1 star. Use nested for loops (an outer loop for each row and an inner loop for the stars in that row).
#include<stdio.h>
int main()
{
int i, j;
for(i = 1; i <= 5; i++)
{
for(j = i; j <= 5; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program to check whether a given number is prime or not. The program should read an integer from the user and print whether it is prime or not. For example, if the input is 111, the output should be "111 is not a prime number".
#include <stdio.h>
int main() {
int num, i, isPrime = 1;
printf("Enter a number to check if it is prime: ");
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime)
printf("%d is a prime number", num);
else
printf("%d is not a prime number", num);
return 0;
}Write a C program to check whether a given number is prime or not. The program should read an integer from the user and print "prime" if it is a prime number, otherwise print "not prime". For example, if the input is 44, output should be "not prime"; if input is 7, output should be "prime".
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 1) {
flag = 1;
} else {
for (i = 2; i <= n / 2; i++) {
if (n % i == 0) {
flag = 1;
break;
}
}
}
if (flag == 0)
printf("prime\n");
else
printf("not prime\n");
return 0;
}Write a C program that reads a positive decimal integer from the user and prints its binary (base-2) representation. For example, if the user enters 13, the program should print 1101.
#include <stdio.h>
int main() {
int bits[32], num, i;
printf("Enter a decimal number: ");
scanf("%d", &num);
for (i = 0; num > 0; i++) {
bits[i] = num % 2;
num = num / 2;
}
printf("Binary of the given number is = ");
for (i = i - 1; i >= 0; i--) {
printf("%d", bits[i]);
}
printf("\n");
return 0;
}Write a C program that reads the number of rows from the user and prints a rhombus (parallelogram) star pattern: each row contains the given number of stars, and each successive row is shifted one space to the left, starting with the maximum indentation on the first row.
#include <stdio.h>
int main()
{
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i - 1; j++)
{
printf(" ");
}
for (int k = 1; k <= rows; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}Write a C program that reads the number of rows from the user and prints a right-angled triangle of stars, where row 1 has one star, row 2 has two stars, and so on up to the given number of rows. Each star should be followed by a space.
#include <stdio.h>
int main()
{
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}Write a C program to check whether a number is a palindrome. Input: 481 Output: not palindrome number. Input: 12321 Output: palindrome number.
#include<stdio.h>
int main()
{
int n, r, rev = 0, temp;
printf("enter the number=");
scanf("%d", &n);
temp = n;
while (n > 0)
{
r = n % 10;
rev = (rev * 10) + r;
n = n / 10;
}
if (temp == rev)
printf("palindrome number");
else
printf("not palindrome number");
return 0;
}Write a C program that reads a 3-digit integer and checks whether it is an Armstrong number (a number equal to the sum of the cubes of its digits). For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
#include<stdio.h>
int main()
{
int num, rem, sum = 0, temp;
printf("Enter the number = ");
scanf("%d", &num);
temp = num;
while(num > 0)
{
rem = num % 10;
sum = sum + (rem * rem * rem);
num = num / 10;
}
if(temp == sum)
printf("Armstrong number");
else
printf("Not an Armstrong number");
return 0;
}Write a C program that reads an integer from the user and prints the sum of its digits. For example, if the user enters 4729, the program should print 22.
#include<stdio.h>
int main()
{
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
while(num > 0)
{
digit = num % 10;
sum = sum + digit;
num = num / 10;
}
printf("Sum is = %d", sum);
return 0;
}INTMAT EDTECH (OPC) PRIVATE LIMITED
No - 16, Javaraiahna Doddi, Mysore Road,
Bengaluru Urban, BENGALURU,
Karnataka 560098, India
contact@intmatedtech.com