0% found this document useful (0 votes)
51 views

C Demo Programs

The document contains 21 code snippets showing C programs that demonstrate various programming concepts like input/output, operators, conditional statements, loops, functions, arrays, pointers, structures etc. Each code snippet is followed by sample input/output. The programs cover concepts like printing ASCII values, data type sizes, swapping values, finding largest number, quadratic equation roots, series summation, factorials, Fibonacci series, GCD, LCM, palindrome checking and matrix operations.

Uploaded by

radhikagarg0210
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

C Demo Programs

The document contains 21 code snippets showing C programs that demonstrate various programming concepts like input/output, operators, conditional statements, loops, functions, arrays, pointers, structures etc. Each code snippet is followed by sample input/output. The programs cover concepts like printing ASCII values, data type sizes, swapping values, finding largest number, quadratic equation roots, series summation, factorials, Fibonacci series, GCD, LCM, palindrome checking and matrix operations.

Uploaded by

radhikagarg0210
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Q1.

Program to Print ASCII Value

#include <stdio.h>

int main() {

char c;

printf("Enter a character: ");

scanf("%c", &c);

// %d displays the integer value of a character

// %c displays the actual character

printf("ASCII value of %c = %d", c, c);

return 0;

Output:

Enter a character: 4

ASCII value of 4 = 52

Q2. C Program to Find the Size of int, float, double and char.

#include<stdio.h>

int main() {

int intType;

float floatType;

double doubleType;

char charType;

// sizeof evaluates the size of a variable

printf("Size of int: %zu bytes\n", sizeof(intType));

printf("Size of float: %zu bytes\n", sizeof(floatType));

printf("Size of double: %zu bytes\n", sizeof(doubleType));

printf("Size of char: %zu byte\n", sizeof(charType));


return 0;

Output:

Size of int: 4 bytes

Size of float: 4 bytes

Size of double: 8 bytes

Size of char: 1 byte

Q3. C Program to Swap Two Numbers.

#include<stdio.h>

int main() {

double first, second, temp;

printf("Enter first number: ");

scanf("%lf", &first);

printf("Enter second number: ");

scanf("%lf", &second);

// value of first is assigned to temp

temp = first;

// value of second is assigned to first

first = second;

// value of temp (initial value of first) is assigned to second

second = temp;

// %.2lf displays number up to 2 decimal points

printf("\nAfter swapping, first number = %.2lf\n", first);

printf("After swapping, second number = %.2lf", second);


return 0;

Output:

Enter first number: 24

Enter second number: 23

After swapping, first number = 23.00

After swapping, second number = 24.00

Q4. C Program to Find the Largest Number Among Three Numbers.

#include <stdio.h>

int main() {

double n1, n2, n3;

printf("Enter three different numbers: ");

scanf("%lf %lf %lf", &n1, &n2, &n3);

// if n1 is greater than both n2 and n3, n1 is the largest

if (n1 >= n2 && n1 >= n3)

printf("%.2f is the largest number.", n1);

// if n2 is greater than both n1 and n3, n2 is the largest

if (n2 >= n1 && n2 >= n3)

printf("%.2f is the largest number.", n2);

// if n3 is greater than both n1 and n2, n3 is the largest

if (n3 >= n1 && n3 >= n2)

printf("%.2f is the largest number.", n3);


return 0;

Output:

Enter three different numbers: 3

7.00 is the largest number.

Q5. C Program to Find the Roots of a Quadratic Equation.

#include <math.h>

#include <stdio.h>

int main() {

double a, b, c, discriminant, root1, root2, realPart, imagPart;

printf("Enter coefficients a, b and c: ");

scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;

// condition for real and different roots

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("root1 = %.2lf and root2 = %.2lf", root1, root2);

// condition for real and equal roots

else if (discriminant == 0) {

root1 = root2 = -b / (2 * a);

printf("root1 = root2 = %.2lf;", root1);

}
// if roots are not real

else {

realPart = -b / (2 * a);

imagPart = sqrt(-discriminant) / (2 * a);

printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);

return 0;

Output:

Q6. Sum of Natural Numbers Using for Loop.

#include <stdio.h>

int main() {

int n, i, sum = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

for (i = 1; i <= n; ++i) {

sum += i;

printf("Sum = %d", sum);

return 0;

Output:

Enter a positive integer: 5

Sum = 15
Q7. C Program to Find Factorial of a Number.

#include <stdio.h>

int main() {

int n, i;

unsigned long long fact = 1;

printf("Enter an integer: ");

scanf("%d", &n);

// shows error if the user enters a negative integer

if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {

for (i = 1; i <= n; ++i) {

fact *= i;

printf("Factorial of %d = %llu", n, fact);

return 0;

Output:

Enter an integer: 8

Factorial of 8 = 40320

Q8. C Program to Display Fibonacci Sequence.

#include <stdio.h>

int main() {

int i, n;
// initialize first and second terms

int t1 = 0, t2 = 1;

// initialize the next term (3rd term)

int nextTerm = t1 + t2;

// get no. of terms from user

printf("Enter the number of terms: ");

scanf("%d", &n);

// print the first two terms t1 and t2

printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms

for (i = 3; i <= n; ++i) {

printf("%d, ", nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

return 0;

Output:

Enter the number of terms: 4

Fibonacci Series: 0, 1, 1, 2

Q9. C Program to Find GCD of two Numbers

Q10. C Program to Find LCM of two Numbers

Q11. C Program to Display Characters from A to Z Using Loop


Q12. C Program to Reverse a Number.

#include <stdio.h>

int main() {

int n, reverse = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &n);

while (n != 0) {

remainder = n % 10;

reverse = reverse * 10 + remainder;

n /= 10;

printf("Reversed number = %d", reverse);

return 0;

Output:

Enter an integer: 12345

Reversed number = 54321

Q13. C Program to Check Whether a Number is Palindrome or Not.

#include <stdio.h>

int main() {

int n, reversed = 0, remainder, original;

printf("Enter an integer: ");


scanf("%d", &n);

original = n;

// reversed integer is stored in reversed variable

while (n != 0) {

remainder = n % 10;

reversed = reversed * 10 + remainder;

n /= 10;

// palindrome if orignal and reversed are equal

if (original == reversed)

printf("%d is a palindrome.", original);

else

printf("%d is not a palindrome.", original);

return 0;

Output: Enter an integer: 123

123 is not a palindrome.

Enter an integer: 1221

1221 is a palindrome.

Q14. C Program to Display Prime Numbers Between Two Intervals.

Q15. C Program to Find Factorial of a Number Using Recursion

#include<stdio.h>

long int multiplyNumbers(int n);

int main() {

int n;

printf("Enter a positive integer: ");


scanf("%d",&n);

printf("Factorial of %d = %ld", n, multiplyNumbers(n));

return 0;

long int multiplyNumbers(int n) {

if (n>=1)

return n*multiplyNumbers(n-1);

else

return 1;

Output:

Enter a positive integer: 6

Factorial of 6 = 720

Q16. C Program to Convert Binary Number to Decimal and vice-versa

// convert binary to decimal

#include <stdio.h>

#include <math.h>

// function prototype

int convert(long long);

int main() {

long long n;

printf("Enter a binary number: ");

scanf("%lld", &n);

printf("%lld in binary = %d in decimal", n, convert(n));

return 0;
}

// function definition

int convert(long long n) {

int dec = 0, i = 0, rem;

while (n!=0) {

rem = n % 10;

n /= 10;

dec += rem * pow(2, i);

++i;

return dec;

Output:

Enter a binary number: 111

111 in binary = 7 in decimal

Q17. C Program to Convert Octal Number to Decimal and vice-versa

Q18. C Program to Convert Binary Number to Octal and vice-versa

Q19. C Program to Calculate Average Using Arrays

#include <stdio.h>

int main() {

int n, i;

float num[100], sum = 0.0, avg;

printf("Enter the numbers of elements: ");

scanf("%d", &n);
while (n > 100 || n < 1) {

printf("Error! number should in range of (1 to 100).\n");

printf("Enter the number again: ");

scanf("%d", &n);

for (i = 0; i < n; ++i) {

printf("%d. Enter number: ", i + 1);

scanf("%f", &num[i]);

sum += num[i];

avg = sum / n;

printf("Average = %.2f", avg);

return 0;

Output:

Enter the numbers of elements: 4

1. Enter number: 2

2. Enter number: 3

3. Enter number: 5

4. Enter number: 6

Average = 4.00

Q20. C Program to Add Two Matrices Using Multi-dimensional Arrays.

#include <stdio.h>

int main() {

int r, c, a[100][100], b[100][100], sum[100][100], i, j;

printf("Enter the number of rows (between 1 and 100): ");

scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");

scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

printf("Enter element a%d%d: ", i + 1, j + 1);

scanf("%d", &a[i][j]);

printf("Enter elements of 2nd matrix:\n");

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

printf("Enter element b%d%d: ", i + 1, j + 1);

scanf("%d", &b[i][j]);

// adding two matrices

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

sum[i][j] = a[i][j] + b[i][j];

// printing the result

printf("\nSum of two matrices: \n");

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

printf("%d ", sum[i][j]);

if (j == c - 1) {
printf("\n\n");

return 0;

Output:

Enter the number of rows (between 1 and 100): 2

Enter the number of columns (between 1 and 100): 2

Enter elements of 1st matrix:

Enter element a11: 1

Enter element a12: 2

Enter element a21: 3

Enter element a22: 4

Enter elements of 2nd matrix:

Enter element b11: 1

Enter element b12: 2

Enter element b21: 3

Enter element b22: 4

Sum of two matrices:

2 4

6 8

Q21. C Program to Multiply Two Matrices Using Multi-dimensional Arrays

Q22. C Program to Find Transpose of a Matrix

Q23. Half Pyramid of *

**

***
****

*****

#include <stdio.h>

int main() {

int i, j, rows;

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; ++i) {

for (j = 1; j <= i; ++j) {

printf("* ");

printf("\n");

return 0;

You might also like