C Language Programs
61. Program to calculate Square of 'n' numbers.
62. Program to take an alphabet from user and check whether it is a vowel or not.
64. Program to accept a number and print the factors of that number.
65. Program to accept two integer numbers and print the GCD(Greatest Common Divisor).
66. Program to find power of number.
67. Program to calculate HCF & LCM.
68. Program to find largest among 3 numbers using ternary operator.
69. Program to find largest number of 'n' numbers.
70. Program to check whether the number is neon number or not.
71. Program to check Niven number (Harshad number).
72. Program to check whether the number is palindrome or not.
73. Program to check perfect number.
74. Program to find the square root of a number.
75. Program to print sum of 'n' prime numbers.
76. Program to print sum of factorial series 1/1! + 2/2! +...1/N!
77. Program to calculate the sum of 'n' terms in Taylor series.
78. Program to swap two numbers without using third variable.
79. Program to swap two numbers using bitwise XOR.
80. Program to swap two numbers using pointer.
CODE
#include<stdio.h>
#include<conio.h>
void main()
{
int n, r, i, sqr=0;
clrscr();
printf("\nEnter the range : ");
scanf("%d", &r);
for (i = 1; i <= r; i++)
{
n = i;
sqr = n * n;
printf("\nSquare of %d is : %d .", n, sqr);
}
getch();
}
62. Program to take an alphabet from user and check whether it is a vowel or not.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter an alphabet : ");
scanf("%c", &ch);
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.", ch);
else
printf("%c is not a vowel.", ch);
getch();
}
63. Program to take two numbers and check whether they are amicable numbers or not.
#include<stdio.h>
#include<conio.h>
//check function
int check(int a, int b)
{
int s = 0, i;
for (i = 1; i < a; i++)
{
if (a % i == 0)
{
s = s + i;
}
}
if (s == b)
{
s = 0;
for (i = 1; i < b; i++)
{
if (b % i == 0)
{
s = s + i;
}
}
if (s == a)
return 1;
else
return 0;
}
return 0;
}
void main()
{
int a, b;
clrscr();
printf("Enter 1st number : ");
scanf("%d", &a);
printf("Enter 2nd number : ");
scanf("%d", &b);
if (check(a, b))
{
printf("\n%d and %d are Amicable Numbers.", a, b);
}
else
{
printf("\n%d and %d are not Amicable Numbers.", a, b);
}
}
64. Program to accept a number and print the factors of that number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
clrscr();
printf("Enter a number : ");
scanf("%d", &n);
printf("Factors of %d are : ", n);
for (i = 1; i <= n; ++i)
{
if (n % i == 0)
printf("\n%d ", i);
}
getch();
}
65. Program to accept two integer numbers and print the GCD(Greatest Common Divisor).
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, m, i;
clrscr();
printf("Enter 1st number : ");
scanf("%d", &x);
printf("Enter 2nd number : ");
scanf("%d", &y);
if (x > y)
m = y;
else
m = x;
for (i = m; i >= 1; i--)
{
if (x % i == 0 && y % i == 0)
{
printf("GCD of two number is : %d", i);
break;
}
}
getch();
}
66. Program to find power of number.
#include<stdio.h>
#include<conio.h>
void main()
{
int base, expo;
int value = 1;
clrscr();
printf("Enter base number : ");
scanf("%d", &base);
printf("Enter exponent number : ");
scanf("%d", &expo);
while (expo != 0)
{
// value = value * base;
value *= base;
--expo;
}
printf("Answer : %d", value);
getch();
}
67. Program to calculate HCF & LCM.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, x, y, t, hcf, lcm;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
hcf = a;
lcm = (x * y) / hcf;
printf("\nHighest Common Factor of %d and %d : %d", x, y, hcf);
printf("\nLeast Common Multiple of %d and %d : %d", x, y, lcm);
getch();
}
68. Program to find largest among 3 numbers using ternary operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, big;
clrscr();
printf("Enter 3 numbers : ");
scanf("%d %d %d", &a, &b, &c);
big = (a > b && a > c ? a : b > c ? b : c);
printf("\nThe biggest number is : %d", big);
getch();
}
69. Program to find largest number of 'n' numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, num, i;
int big;
clrscr();
printf("Enter total numbers : ");
scanf("%d", &n);
printf("Number %d : ", 1);
scanf("%d", &big);
for (i = 2; i <= n; i++)
{
printf("Number %d : ", i);
scanf("%d", &num);
if (big < num)
big = num;
}
printf("Largest number is : %d", big);
getch();
}
70. Program to check whether the number is neon number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sq, i, sum = 0;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
sq = n * n;
for (i = sq; i > 0; i = i / 10)
sum = sum + i % 10;
if (sum == n)
printf("%d is a neon number.", n);
else
printf("%d is not a neon number.", n);
getch();
}
71. Program to check Niven number (Harshad number).
#include<stdio.h>
#include<conio.h>
void main()
{
int n, d, a, sum = 0;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
a = n;
while (a > 0)
{
d = a % 10;
sum = sum + d;
a = a / 10;
}
if (n % sum == 0)
printf("\nThe number is Niven Number.");
else
printf("\nThe number is not a Niven Number.");
getch();
}
72. Program to check whether the number is palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, rev = 0, temp;
clrscr();
printf("Enter a number : ");
scanf("%d", &n);
temp = n;
while (temp != 0)
{
rev = rev * 10;
rev = rev + temp % 10;
temp = temp / 10;
}
if (n == rev)
printf("\n%d is palindrome number.", n);
else
printf("\n%d is not palindrome number.", n);
getch();
}
73. Program to check perfect number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i = 1, sum = 0;
clrscr();
printf("Enter a number : ");
scanf("%d", &n);
/*The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6.*/
while (i < n)
{
if (n % i == 0)
{
sum = sum + i;
}
i++;
}
if (sum == n)
{
printf("\n%d is a perfect number.", i);
}
else
{
printf("\n%d is not a perfect number.", i);
}
getch();
}
74. Program to find the square root of a number.
#include<math.h>
#include<stdio.h>
#include<conio.h>
void main()
{
double num, result;
clrscr();
printf("Enter number : ");
scanf("%lf", &num);
result = sqrt(num);
printf("Square root of %lf is %lf.", num, result);
getch();
}
75. Program to print sum of 'n' prime numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i = 3, count, c, sum = 2;
clrscr();
printf("Enter total number of prime numbers for addition : ");
scanf("%d", &n);
if (n >= 1)
{
printf("\nFirst %d prime numbers are :", n);
printf("\n2 ");
}
for (count = 2; count <= n;)
{
for (c = 2; c <= i - 1; c++)
{
if (i % c == 0)
break;
}
if (c == i)
{
sum = sum + i;
printf("%d ", i);
count++;
}
i++;
}
printf("\nSum : %d", sum);
getch();
}
76. Program to print sum of factorial series 1/1! + 2/2! +...1/N!
#include<stdio.h>
#include<conio.h>
double sumseries(double);
void main()
{
double number, sum;
clrscr();
printf("Enter the number : ");
scanf("%lf", &number);
sum = sumseries(number);
printf("\nSum of the above series = %lf ", sum);
getch();
}
double sumseries(double m)
{
double sum2 = 0, f = 1, i;
for (i = 1; i <= m; i++)
{
f = f * i;
sum2 = sum2 + (i / f);
if (i == m)
{
printf("%.2lf / %.2lf = %lf", i, f, sum2);
}
else
{
printf("%.2lf / %.2lf + \n", i, f);
}
}
return(sum2);
}
77. Program to calculate the sum of 'n' terms in Taylor series.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x, i;
int fact = 1, n;
float sum = 0;
clrscr();
printf("Enter the value of x : ");
scanf("%d", &x);
printf("Enter the number of terms : ");
scanf("%d", &n);
for (i = 1; i < n; i++)
{
fact = fact * i;
sum = sum + (pow(x, i) / fact);
}
sum = sum + 1;
printf("The sum of taylor series is : ");
printf("%f", sum);
getch();
}
78. Program to swap two numbers without using third variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 10, y = 5;
clrscr();
printf("Enter x : ");
scanf("%d", &x);
printf("Enter y : ");
scanf("%d", &y);
printf("\nBefore Swapping : \n x = %d \n y = %d", x, y);
// Code to swap x and y
x = x + y;
y = x - y;
x = x - y;
printf("\nAfter Swapping : \n x = %d \n y = %d", x, y);
getch();
}
79. Program to swap two numbers using bitwise XOR.
#include<stdio.h>
#include<conio.h>
void main()
{
long i, k;
clrscr();
printf("Enter two integers : \n");
scanf("%ld %ld", &i, &k);
printf("\n Before swapping i : %ld and k : %ld", i, k);
i = i ^ k;
k = i ^ k;
i = i ^ k;
printf("\nAfter swapping i : %ld and k : %ld", i, k);
getch();
}
80. Program to swap two numbers using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
int *ptra, *ptrb, *temp;
clrscr();
printf("Enter a : ");
scanf("%d", &a);
printf("Enter b : ");
scanf("%d", &b);
printf("\nBefore swapping : a : %d, b : %d", a, b);
ptra = &a;
ptrb = &b;
temp = ptra;
*ptra = *ptrb;
*ptrb = *temp;
printf("\nAfter swapping : a : %d, b : %d", a, b);
getch();
}
THANK YOU
0 Comments