You are on page 1of 10

1.

Write a C program to add any 10 numbers given by the user

#include<stdio.h>
#include<conio.h>
void main()
{ int i,n,sum=0;

printf("enter the 10 number\n");


for (i=1;i<=10;i++)
{
scanf("%d",&n);
sum +=n;
}

printf("The sum of 10 no is =%d",sum);


getch();
}

Output:
2. Write a C program to check whether a number is even or odd

#include <stdio.h>
void main()
{
int number;

printf("Enter an integer: ");


scanf("%d", &number);

// True if the number is perfectly divisible by 2


if(number % 2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);

}
Output:
3. Write a C program to reverse a number

#include <stdio.h>

void main()
{
int n, reverse = 0;

printf("Enter a number to reverse\n");


scanf("%d", &n);

while (n > 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}

printf("Reverse of entered number is = %d\n", reverse);

Output:
4. Write a C Program to Find Factorial of a Number

#include <stdio.h>
void main()
{
int n, i;
unsigned long factorial = 1;

printf("Enter an integer: ");


scanf("%d",&n);

// show 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)
{
factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}

Output:
5. Write a C program to add two matrix

#include <stdio.h>
void main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");

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


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
}
Output::
6. Write a C program to print the binary equivalent of a decimal number

#include <stdio.h>
#include <conio.h>

long decimalToBinary(long n);


void main()
{
long decimal;
printf("Enter a decimal number\n");
scanf("%ld", &decimal);
printf("Binary number of %ld is %ld", decimal, decimalToBinary(decimal));

getch();

/* Function to convert a decinal number to binary number */


long decimalToBinary(long n)
{
int remainder;
long binary = 0, i = 1;

while(n != 0) {
remainder = n%2;
n = n/2;
binary= binary + (remainder*i);
i = i*10;
}
return binary;
}

Output:
7. Write a Program to read age of 10 persons and display only those persons whose between 50
and 60

#include<stdio.h>
#include<conio.h>

void main()
{
int i,age[10],count=0;

for (i=0;i<10;i++)
{
printf("\nEnter age of %d persons :: ",i);
scanf("%d",&age[i]);
}

for (i=0;i<10;i++)
{
if(age[i]>50 && age[i] < 60)
count++;
else
continue;
}

printf("\n\nNumber of persons whose age between 50-60 are :: %d",count);


getch();
}
Output:
8. Write a pow function to calculate 3n where n is given by the user
#include <stdio.h>
#include <math.h>

void main()
{
double base, exponent, result;

printf("Enter a base number: ");


scanf("%lf", &base);

printf("Enter an exponent: ");


scanf("%lf", &exponent);

// calculates the power


result = pow(base, exponent);

printf("%.1lf^%.1lf = %.2lf", base, exponent, result);

Output:
9. Write a C program to swap two numbers
#include <stdio.h>

void main()
{
int x, y, temp;

printf("Enter the value of x and y\n");


scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n",x,y);

temp = x;
x = y;
y = temp;

printf("After Swapping\nx = %d\ny = %d\n",x,y);

Output:
10. Write a C program to add any three numbers using given by the user using pointer concept

#include <stdio.h>

void main()
{
int first, second,third, *p, *q, *r,sum;

printf("Enter three integers to add\n");


scanf("%d%d%d", &first, &second,&third);

p = &first;
q = &second;
r=&third;

sum = *p + *q+ *r;

printf("Sum of entered numbers = %d\n",sum);

Output:

You might also like