• // program to find the sum of digits
• #include <stdio.h>
• int main()
• {
• int n, r, sum = 0;
• printf("Enter a number\n");
• scanf("%d", &n);
• while (n> 0)
• {
• r = n % 10;
• sum = sum + r;
• n= n / 10;
• }
• printf("Sum of digits of %d = %d\n", n, sum);
• return 0;
• }
• //program to find the reverse of a number
• #include<stdio.h>
• void main()
• {
• int n,sum=0;
• printf(“enter a number”);
• scanf(“%d”,&n);
• While(n>0)
• {
• r=n%10;
• sum=sum*10+r;
• n= n/10;
• }
• printf(“the reverse of a number is%d”, sum);
• }
• //program to check whether a given number is palindrome or not
• #include <stdio.h>
• int main()
• {
• int n, r, sum= 0, temp;
• printf("Enter an number: ");
• scanf("%d", &n);
• temp = n;
• while( n>0 )
• {
• r = n%10;
• sum = sum*10 + r;
• n /= 10;
• }
• if (n == sum)
• printf("%d is a palindrome.", sum);
• else
• printf("%d is not a palindrome.", sum);
• return 0;
• }
• //program to check whether a given number is Armstrong number or not
• #include<stdio.h>
• void main()
• {
• int n, r, c, sum=0, temp;
• printf(“enter a number:”);
• scanf(“%d”,&n);
• temp=n;
• while(n>0)
• {
• r=n%10;
• c=r*r*r;
• sum = sum + c;
• n = n/10;
• }
• n = temp;
• if(n == sum)
• printf(“the number is Armstrong number”);
• else
• printf(“the number is not an Armstrong number”);
• }
• //program to print Fibonacci series for a given limit
• #include<stdio.h>
• void main()
• {
• int n, a=0, b=1, c;
• printf(“Enter the limit:”);
• scanf(“%d”, &n);
• for(i=0;i<=n;i++)
• {
• printf(“%d\n”, a);
• c=a+b;
• a=b;
• b=c;
• }
• }
• // program to print range of prime numbers
• #include<stdio.h>
• void main()
• {
• int n,i,fact,j;
• printf("Enter the Number");
• scanf("%d",&n);
• printf("Prime Numbers are: \n");
• for(i=1; i<=n; i++)
• {
• fact=0;
• for(j=1; j<=n; j++)
• {
• if(i%j==0)
• fact++;
• }
• if(fact==2)
• printf("%d " ,i);
• }
• getch();
• }
• //program to print pattern1
• #include<stdio.h>
• void main()
• {
• int I,j;
• for(i=1;i<=5;i++)
• {
• for(j=1;j<=5;j++)
• {
• printf(“*”)’
• }
• printf(“\n”);
• }
• }
• //program to print pattern2
• #include<stdio.h>
• void main()
• {
• int i,j;
• for(i=1;i<=5;i++)
• {
• for(j=1;j<=i;j++)
• {
• printf(“*”)’
• }
• printf(“/n”);
• }
• }
• //program to print pattern3 using decrement operator
• #include<stdio.h>
• void main()
• {
• int i,j;
• for(i=5;i>=1;i--)
• {
• for(j=5;j>=i;j--)
• {
• printf(“*”)’
• }
• printf(“/n”);
• }
• }
• //program to print pattern4
• #include<stdio.h>
• void main()
• {
• int I,j;
• for(i=1;i<=5;i++)
• {
• for(j=i;j<=5;j++)
• {
• printf(“*”)’
• }
• printf(“/n”);
• }
• }
• /* C program to calculate total electricity bill */
• #include <stdio.h>
• int main()
• {
• int unit;
• float amt, total_amt, sur_charge;
• printf("Enter total units consumed: ");
• scanf("%d", &unit); /* Calculate electricity bill according to given conditions */
• if(unit <= 50)
• {
• amt = unit * 0.50;
• }
• else
• if(unit <= 150)
• {
• amt = 25 + ((unit-50) * 0.75);
• }
• else
• if(unit <= 250)
• {
• amt = 100 + ((unit-150) * 1.20);
• }
• else
• {
• amt = 220 + ((unit-250) * 1.50);
• }
• /* Calculate total electricity bill after adding surcharge */
• sur_charge = amt * 0.20;
• total_amt = amt + sur_charge;
• printf("Electricity Bill = Rs. %.2f", total_amt); return 0;
• }
• /* C program to calculate gross salary of an employee */
• #include <stdio.h>
• int main()
• {
• float basic, gross, da, hra;
• printf("Enter basic salary of an employee: ");
• scanf("%f", &basic);
• if(basic <= 10000)
• {
• da = basic * 0.8;
• hra = basic * 0.2;
• }
• else
• if(basic <= 20000)
• {
• da = basic * 0.9;
• hra = basic * 0.25;
• }
• else
• {
• da = basic * 0.95;
• hra = basic * 0.3;
• }
• /* Calculate gross salary */
• gross = basic + hra + da;
• printf("GROSS SALARY OF EMPLOYEE = %.2f",
gross);
• return 0;
• }
• Example: Program to Multiply Two Matrices
• #include <stdio.h>
• int main()
• {
• int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
• printf("Enter rows and column for first matrix: ");
• scanf("%d %d", &r1, &c1);
• printf("Enter rows and column for second matrix: ");
• scanf("%d %d",&r2, &c2); // Column of first matrix should be equal to column
of second matrix and
• while (c1 != r2)
• {
• printf("Error! column of first matrix not equal to row of second.\n\n");
• printf("Enter rows and column for first matrix: ");
• scanf("%d %d", &r1, &c1);
• printf("Enter rows and column for second matrix: ");
• scanf("%d %d",&r2, &c2);
• }
• // Storing elements of first matrix.
• printf("\nEnter elements of matrix 1:\n"); for(i=0; i<r1; ++i)
• for(j=0; j<c1; ++j)
• {
• printf("Enter elements a%d%d: ",i+1, j+1); scanf("%d", &a[i][j]); }
• // Storing elements of second matrix.
• printf("\nEnter elements of matrix 2:\n");
• for(i=0; i<r2; ++i)
• for(j=0; j<c2; ++j)
• {
• printf("Enter elements b%d%d: ",i+1, j+1); scanf("%d",&b[i][j]); }
• // Initializing all elements of result matrix to 0
• for(i=0; i<r1; ++i)
• for(j=0; j<c2; ++j)
• {
• result[i][j] = 0;
• }
• // Multiplying matrices a and b and // storing result in result matrix
• for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
• for(k=0; k<c1; ++k)
• {
• result[i][j]+=a[i][k]*b[k][j];
• }
• // Displaying the result
• printf("\nOutput Matrix:\n");
• for(i=0; i<r1; ++i)
• for(j=0; j<c2; ++j)
• {
• printf("%d\t ", result[i][j]);
• if(j == c2-1)
• printf("\n\n");
• }
• return 0;
• Output
• Enter rows and column for first matrix: 3 2
• Enter rows and column for second matrix: 3 2
Error! column of first matrix not equal to row of second.
• Enter rows and column for first matrix: 2 3
• Enter rows and column for second matrix: 3 2
• Enter elements of matrix 1:
• Enter elements a11: 3
• Enter elements a12: -2
• Enter elements a13: 5
• Enter elements a21: 3
• Enter elements a22: 0
• Enter elements a23: 4
• Enter elements of matrix 2:
• Enter elements b11: 2
• Enter elements b12: 3
• Enter elements b21: -9
• Enter elements b22: 0
• Enter elements b31: 0
• Enter elements b32: 4
• Output Matrix: 24 29
• //c program to print transpose of a matrix
• #include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of a matrix\n");
scanf("%d%d", &m, &n);
• printf("Enter elements of the matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &matrix[c][d]);
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
transpose[d][c] = matrix[c][d];
printf("Transpose of the matrix:\n");
for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
printf("%d\t", transpose[c][d]);
printf("\n");
}
• return 0;
• //c program to print Floyd’s triangle
• #include <stdio.h>
• int main()
{
int n, i, c, a = 1;
• printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d", &n);
• for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ", a); // Please note space after %d
a++;
}
printf("\n");
}
• return 0;
}
• //c program to find reverse of an array
• #include <stdio.h>
• int main()
{
int n, c, d, a[100], b[100];
• printf("Enter the number of elements in array\n");
scanf("%d", &n);
• printf("Enter array elements\n");
• for (c = 0; c < n ; c++)
scanf("%d", &a[c]);
• // Copying elements into array b starting from the end of array a
• for (c = n - 1, d = 0; c >= 0; c--, d++)
b[d] = a[c];
• // Copying reversed array into the original, we are modifying the original array.
• for (c = 0; c < n; c++)
a[c] = b[c];
• printf("The array after reversal:\n");
• for (c = 0; c < n; c++)
printf("%d\n", a[c]);
• return 0;
}
• //C program to check vowel or consonant using if else
• #include <stdio.h>
• int main()
{
char ch;
• printf("Input a character\n");
scanf("%c", &ch);
• if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z')) {
if (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=='I' || ch=='o' || ch=='O' |
| ch== 'u' || ch=='U')
printf("%c is a vowel.\n", ch);
else
printf("%c is a consonant.\n", ch);
}
else
printf("%c is neither a vowel nor a consonant.\n", ch);
• return 0;
}
• //c program to delete an element from an array
• #include <stdio.h>
• int main()
{
int array[100], position, c, n;
• printf("Enter number of elements in array\n");
scanf("%d", &n);
• printf("Enter %d elements\n", n);
• for (c = 0; c < n; c++)
scanf("%d", &array[c]);
• printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
• if (position >= n+1)
printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];
• printf("Resultant array:\n");
• for (c = 0; c < n - 1; c++)
printf("%d\n", array[c]);
}
• return 0;
}
• //C program to find roots of a quadratic equation
• #include <stdio.h>
#include <math.h>
• int main()
{
int a, b, c, d;
double root1, root2;
• printf("Enter a, b and c where a*x*x + b*x + c = 0\n");
scanf("%d%d%d", &a, &b, &c);
• d = b*b - 4*a*c;
• if (d < 0) { // complex roots, i is for iota (√-1, square root of -1)
printf("First root = %.2lf + i%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
printf("Second root = %.2lf - i%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
}
else { // real roots
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);
• printf("First root = %.2lf\n", root1);
printf("Second root = %.2lf\n", root2);
}
• return 0;
}
• //C program to read an array of 10 integer and find sum of all even and odd
numbers.
#include <stdio.h>
int main()
{
int a[8],i,e=0,o=0;
printf("Enter 8 Numbers:\n");
for(i=0;i<8;i++)
scanf("%d",&a[i]);
for(i=0;i<8;i++)
{
if(a[i]%2==0)
e=e+a[i];
else
o=o+a[i];
}
printf("\nSum of Even Numbers = %d",e);
printf("\nSum of Odd Numbers = %d",o);
return 0;
}
• //c program to sort an array in ascending order
• #include <stdio.h>
int main()
{
int a[7],i,j,t;
printf("Enter 7 Numbers:\n");
for(i=0;i<7;i++)
scanf("%d",&a[i]);
for(i=0;i<6;i++)
for(j=i+1;j<7;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("\nNumbers in Ascending Order:\n");
for(i=0;i<7;i++)
printf("%d ",a[i]);
return 0;
}