Simple C Programs With Output

C programs

Numeric Value Programs

In this section, we will see basic programs which we should know to code and also to implement.These may type of programs help to think more logically and effectively.

Numeric value programs provide a way to play with number without any difficulty.These programs are generally asked in interviews or aptitude programming round.All programs and its explanation are listed below as.

1.Write a program to check whether a number is even or odd.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n Enter a number \n");
scanf("%d",&n);
if (n % 2 == 0) {
printf("%d is even",n);
}
else {
printf("%d is odd",n);
}
getch();
}

2.Write a program to print factorial of a number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact=1,i=0;
clrscr();
printf("\n Enter a number \n");
scanf("%d",&n);
for (i = 1; i <= n; i++) {
fact = fact * i;
}
printf("\n Factorial of %d is %d",n,fact);
getch();
}

3.Write a program to print reverse of a number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,value=0,rev=0,d=0;
clrscr();
printf("\n Enter a number \n");
scanf("%d",&n);
value = n;
while (n > 0) {
d = n % 10;
rev = (rev * 10) + d;
n = n / 10;
}
printf("\n The reverse of %d is %d",value,rev);
getch();
}

4.Write a program to check whether a number is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,flag=0,i=0;
clrscr();
printf("\n Enter a number \n");
scanf("%d",&n);
for (i = 2; i < n; i++) {
if (n % i == 0) {
flag = 0;
break;
} else {
flag = 1;
}
}
if (flag == 0) {
printf("\n %d  is not prime  number",n);
}
else {
printf("\n %d is a prime number",n);
}
getch();
}

5.Write a program to check the given number is either palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,value=0,rev=0,d=0;
clrscr();
printf("\n Enter a number \n");
scanf("%d",&n);
value = n;
while (n > 0) {
d = n % 10;
rev = (rev * 10) + d;
n = n / 10;
}
if (rev == value) {
printf("\n %d is a palindrome",value);
}
else {
printf("\n %d is not a palindrome",value);
}
getch();
}

6. Write a program to swap two numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,t=0;
clrscr();
printf("\n Enter x ");
scanf("%d",&x);
printf("\n Enter y ");
scanf("%d",&y);
printf("\n Before swapping x=%d and y=%d",x,y);
t = x;
x = y;
y = t;
printf("\n After swapping x=%d and y=%d",x,y);
getch();
}

7.Write a program for x raise y.

#
include < stdio.h > #include < conio.h >
void main() {
int x, y, value = 1, i = 0;
clrscr();
printf("\n Enter x ");
scanf("%d", & x);
printf("\n Enter y ");
scanf("%d", & y);
for (i = 0; i < y; i++) {
value = value * x;
}
printf("\n x^y = %d^%d  is %d", x, y, value);
getch();
}

8.Write a program to find LCM of number.

#include<stdio.h>
#include<conio.h>
void main()
{
int lcm = 1, a, b,i=0;
clrscr();
printf("\n Enter the 1st number : ");
scanf("%d",&a);
printf("\n Enter the 2nd number : ");
scanf("%d",&b);
for (i = a; i <= a * b; i++) {
if (i % a == 0 && i % b == 0) {
lcm = i;
break;
}
}
printf("\n  L.C.M.=%d",lcm);
getch();
}

9.Write a program to print 1 to n without using loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n Print numbers till ? ");
scanf("%d",&n);
printf("\n");
print(n);
getch();
}
print(int n) {
if (n >= 1) {
print(n - 1);
printf("%d\n",n);
}
}

10. Write a program to swap two numbers without using third variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\n Enter x ");
scanf("%d",&x);
printf("\n Enter y ");
scanf("%d",&y);
printf("\n Before swapping x=%d and y=%d",x,y);
x = x + y;
y = x - y;
x = x - y;
printf("\n After swapping x=%d and y=%d",x,y);
getch();
}

11. Write a program to calculate a power of 2 without using any function.

#include<stdio.h>
#include<conio.h>
void main()
{
int number,i,answer = 2;
clrscr();
printf("\n Enter the number:");
scanf("%d",&number);
for (i = 1; i < number; i++) {
answer = 2 * answer;
}
printf("Value of given power is %d ",answer);
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *