85. Program to convert Decimal numbers to Binary.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, c, k;
clrscr();
printf("Enter a decimal number : ");
scanf("%d", &n);
printf("\n%d in binary number system is : ", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
{
printf("1");
}
else
{
printf("0");
}
}
printf("\n");
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int n, c, k;
clrscr();
printf("Enter a decimal number : ");
scanf("%d", &n);
printf("\n%d in binary number system is : ", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
{
printf("1");
}
else
{
printf("0");
}
}
printf("\n");
getch();
}
@C_Codings
86. Program to find f(x) by Lagrange's interpolation method.
#include<stdio.h>
#include<conio.h>
void main()
{
float x[10], y[10], temp = 1, f[10], sum, p;
int i, n, j, k = 0, c;
clrscr();
printf("How many record you will enter : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\n\nenter the value of x%d: ", i);
scanf("%f", &x[i]);
printf("\n\nEnter the value of f(x%d): ", i);
scanf("%f", &y[i]);
}
printf("\n\nEnter X for finding f(x): ");
scanf("%f", &p);
for (i = 0; i < n; i++)
{
temp = 1;
k = i;
for (j = 0; j < n; j++)
{
if (k == j)
{
continue;
}
else
{
temp = temp * ((p - x[j]) / (x[k] - x[j]));
}
}
f[i] = y[i] * temp;
}
for (i = 0; i < n; i++)
{
sum = sum + f[i];
}
printf("\n\nf(%.1f) = %f ", p, sum);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
float x[10], y[10], temp = 1, f[10], sum, p;
int i, n, j, k = 0, c;
clrscr();
printf("How many record you will enter : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\n\nenter the value of x%d: ", i);
scanf("%f", &x[i]);
printf("\n\nEnter the value of f(x%d): ", i);
scanf("%f", &y[i]);
}
printf("\n\nEnter X for finding f(x): ");
scanf("%f", &p);
for (i = 0; i < n; i++)
{
temp = 1;
k = i;
for (j = 0; j < n; j++)
{
if (k == j)
{
continue;
}
else
{
temp = temp * ((p - x[j]) / (x[k] - x[j]));
}
}
f[i] = y[i] * temp;
}
for (i = 0; i < n; i++)
{
sum = sum + f[i];
}
printf("\n\nf(%.1f) = %f ", p, sum);
getch();
}
@C_Codings
87. Program to check the leap year.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year : ");
scanf("%d", &year);
if (year % 400 == 0)
printf("\n%d is a leap year.", year);
else if (year % 100 == 0)
printf("\n%d is not a leap year.", year);
else if (year % 4 == 0)
printf("\n%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year : ");
scanf("%d", &year);
if (year % 400 == 0)
printf("\n%d is a leap year.", year);
else if (year % 100 == 0)
printf("\n%d is not a leap year.", year);
else if (year % 4 == 0)
printf("\n%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
getch();
}
@C_Codings
88. Program to find nCr & nPr.
#include<stdio.h>
#include<conio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
void main()
{
int n, r;
long ncr, npr;
clrscr();
printf("Enter the value of n and r : \n");
scanf("%d %d", &n, &r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf("%dC%d = %ld\n", n, r, ncr);
printf("%dP%d = %ld\n", n, r, npr);
getch();
}
long find_ncr(int n, int r)
{
long result;
result = factorial(n) / (factorial(r) * factorial(n - r));
return result;
}
long find_npr(int n, int r)
{
long result;
result = factorial(n) / factorial(n - r);
return result;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return (result);
}
@C_Codings
#include<stdio.h>
#include<conio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
void main()
{
int n, r;
long ncr, npr;
clrscr();
printf("Enter the value of n and r : \n");
scanf("%d %d", &n, &r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf("%dC%d = %ld\n", n, r, ncr);
printf("%dP%d = %ld\n", n, r, npr);
getch();
}
long find_ncr(int n, int r)
{
long result;
result = factorial(n) / (factorial(r) * factorial(n - r));
return result;
}
long find_npr(int n, int r)
{
long result;
result = factorial(n) / factorial(n - r);
return result;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return (result);
}
@C_Codings
89. Program for Newton Raphson General.
#include<stdio.h>
#include<math.h>
int user_power, i = 0, cnt = 0, flag = 0;
int coef[10] = {0};
float x1 = 0, x2 = 0, t = 0;
float fx1 = 0, fdx1 = 0;
int main()
{
printf("PROGRAM FOR NEWTON RAPHSON GENERAL");
printf("\nEnter the total no. of power : ");
scanf("%d", &user_power);
for (i = 0; i <= user_power; i++)
{
printf("\nx^%d : ", i);
scanf("%d", &coef[i]);
}
printf("\n");
printf("\n\nThe Polynomial is ");
//printing coeff.
for (i = user_power; i >= 0; i--)
{
printf("%dx^%d", coef[i], i);
}
printf("\n\nIntial x1 -> ");
scanf("%f", &x1);
printf("Iteration\tX1\tFX1\tF'X1");
do
{
cnt++;
fx1 = fdx1 = 0;
for (i = user_power; i >= 1; i--)
{
fx1 += coef[i] * (pow(x1, i));
}
fx1 += coef[0];
for (i = user_power; i >= 0; i--)
{
fdx1 += coef[i] * (i * pow(x1, (i - 1)));
}
t = x2;
x2 = (x1 - (fx1 / fdx1));
x1 = x2;
printf("\n\t%d\t%.3f\t%.3f\t%.3f ", cnt, x2, fx1, fdx1);
}
while ((fabs(t - x1)) >= 0.0001);
printf("\n\nThe root of equation is %f", x2);
return 0;
}
@C_Codings
#include<stdio.h>
#include<math.h>
int user_power, i = 0, cnt = 0, flag = 0;
int coef[10] = {0};
float x1 = 0, x2 = 0, t = 0;
float fx1 = 0, fdx1 = 0;
int main()
{
printf("PROGRAM FOR NEWTON RAPHSON GENERAL");
printf("\nEnter the total no. of power : ");
scanf("%d", &user_power);
for (i = 0; i <= user_power; i++)
{
printf("\nx^%d : ", i);
scanf("%d", &coef[i]);
}
printf("\n");
printf("\n\nThe Polynomial is ");
//printing coeff.
for (i = user_power; i >= 0; i--)
{
printf("%dx^%d", coef[i], i);
}
printf("\n\nIntial x1 -> ");
scanf("%f", &x1);
printf("Iteration\tX1\tFX1\tF'X1");
do
{
cnt++;
fx1 = fdx1 = 0;
for (i = user_power; i >= 1; i--)
{
fx1 += coef[i] * (pow(x1, i));
}
fx1 += coef[0];
for (i = user_power; i >= 0; i--)
{
fdx1 += coef[i] * (i * pow(x1, (i - 1)));
}
t = x2;
x2 = (x1 - (fx1 / fdx1));
x1 = x2;
printf("\n\t%d\t%.3f\t%.3f\t%.3f ", cnt, x2, fx1, fdx1);
}
while ((fabs(t - x1)) >= 0.0001);
printf("\n\nThe root of equation is %f", x2);
return 0;
}
@C_Codings
90. Program to calculate the sum of even numbers from 1 to n.
#include<stdio.h>
#include<conio.h>
void main()
{
int sum = 0, n;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
// Using Math formula
// (n/2)((n / 2) + 1)
sum = ((n / 2) * ((n / 2) + 1));
printf("Sum of even numbers from 1 to %d : %d", n, sum);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int sum = 0, n;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
// Using Math formula
// (n/2)((n / 2) + 1)
sum = ((n / 2) * ((n / 2) + 1));
printf("Sum of even numbers from 1 to %d : %d", n, sum);
getch();
}
@C_Codings
91. Simpson 1/3 rule.
#include<stdio.h>
float f(float x)
{
return (1 / (1 + x));
}
void main()
{
int i, n;
float x0, xn, h, y[20], so, se, ans, x[20];
clrscr();
printf("\nEnter values of x0,xn,h: ");
scanf("%f%f%f", &x0, &xn, &h);
n = (xn - x0) / h;
if (n % 2 == 1)
{
n = n + 1;
}
h = (xn - x0) / n;
printf("\nRefined value of n and h are:%d and %f\n", n, h);
printf("\n Y values: \n");
for (i = 0; i <= n; i++)
{
x[i] = x0 + i * h;
y[i] = f(x[i]);
printf("\n %f\n", y[i]);
}
so = 0;
se = 0;
for (i = 1; i < n; i++)
{
if (i % 2 == 1)
{
so = so + y[i];
}
else
{
se = se + y[i];
}
}
ans = h / 3 * (y[0] + y[n] + 4 * so + 2 * se);
printf("\nFinal integration is %f", ans);
getch();
}
@C_Codings
#include<stdio.h>
float f(float x)
{
return (1 / (1 + x));
}
void main()
{
int i, n;
float x0, xn, h, y[20], so, se, ans, x[20];
clrscr();
printf("\nEnter values of x0,xn,h: ");
scanf("%f%f%f", &x0, &xn, &h);
n = (xn - x0) / h;
if (n % 2 == 1)
{
n = n + 1;
}
h = (xn - x0) / n;
printf("\nRefined value of n and h are:%d and %f\n", n, h);
printf("\n Y values: \n");
for (i = 0; i <= n; i++)
{
x[i] = x0 + i * h;
y[i] = f(x[i]);
printf("\n %f\n", y[i]);
}
so = 0;
se = 0;
for (i = 1; i < n; i++)
{
if (i % 2 == 1)
{
so = so + y[i];
}
else
{
se = se + y[i];
}
}
ans = h / 3 * (y[0] + y[n] + 4 * so + 2 * se);
printf("\nFinal integration is %f", ans);
getch();
}
@C_Codings
92. Program to add two strings without using concat() function.
#include<stdio.h>
#include<string.h>
void concat(char[], char[]);
void main()
{
char s1[50], s2[30];
clrscr();
printf("\nEnter String 1 : ");
gets(s1);
printf("\nEnter String 2 : ");
gets(s2);
concat(s1, s2);
printf("\nConcated string is : %s", s1);
getch();
}
void concat(char s1[], char s2[])
{
int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '\0'; i++, j++)
{
s1[i] = s2[j];
}
s1[i] = '\0';
}
@C_Codings
#include<stdio.h>
#include<string.h>
void concat(char[], char[]);
void main()
{
char s1[50], s2[30];
clrscr();
printf("\nEnter String 1 : ");
gets(s1);
printf("\nEnter String 2 : ");
gets(s2);
concat(s1, s2);
printf("\nConcated string is : %s", s1);
getch();
}
void concat(char s1[], char s2[])
{
int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '\0'; i++, j++)
{
s1[i] = s2[j];
}
s1[i] = '\0';
}
@C_Codings
93. Program to check vowels in string.
#include<stdio.h>
int count_vowels(char []);
int check_vowel(char);
void main()
{
char array[100];
int c;
clrscr();
printf("Enter a string : ");
gets(array);
c = count_vowels(array);
printf("\nNumber of vowels in %s : %d", array, c);
getch();
}
int count_vowels(char a[])
{
int count = 0, c = 0, flag;
char d;
do
{
d = a[c];
flag = check_vowel(d);
if (flag == 1)
count++;
c++;
} while (d != '\0');
return count;
}
int check_vowel(char a)
{
if (a >= 'A' && a <= 'Z')
{
// Converting to lower case
a = a + 'a' - 'A';
}
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;
return 0;
}
@C_Codings
#include<stdio.h>
int count_vowels(char []);
int check_vowel(char);
void main()
{
char array[100];
int c;
clrscr();
printf("Enter a string : ");
gets(array);
c = count_vowels(array);
printf("\nNumber of vowels in %s : %d", array, c);
getch();
}
int count_vowels(char a[])
{
int count = 0, c = 0, flag;
char d;
do
{
d = a[c];
flag = check_vowel(d);
if (flag == 1)
count++;
c++;
} while (d != '\0');
return count;
}
int check_vowel(char a)
{
if (a >= 'A' && a <= 'Z')
{
// Converting to lower case
a = a + 'a' - 'A';
}
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;
return 0;
}
@C_Codings
94. Program to compare two strings.
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[100];
clrscr();
printf("\nEnter the first string : ");
gets(a);
printf("\nEnter the second string : ");
gets(b);
if (strcmp(a, b) == 0)
printf("\nEntered strings are equal.");
else
printf("\nEntered strings are not equal.");
getch();
}
@C_Codings
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[100];
clrscr();
printf("\nEnter the first string : ");
gets(a);
printf("\nEnter the second string : ");
gets(b);
if (strcmp(a, b) == 0)
printf("\nEntered strings are equal.");
else
printf("\nEntered strings are not equal.");
getch();
}
@C_Codings
95. Program to compare strings without using strcmp() function.
#include<stdio.h>
int stringCompare(char[], char[]);
void main()
{
char str1[100], str2[100];
int compare;
clrscr();
printf("Enter first string : ");
scanf("%s", str1);
printf("Enter second string : ");
scanf("%s", str2);
compare = stringCompare(str1, str2);
if (compare == 1)
printf("\nBoth strings are equal.");
else
printf("\nBoth strings are not equal.");
getch();
}
int stringCompare(char str1[], char str2[])
{
int i = 0, flag = 0;
while (str1[i] != '\0' && str2[i] != '\0')
{
if (str1[i] != str2[i])
{
flag = 1;
break;
}
i++;
}
if (flag == 0 && str1[i] == '\0' && str2[i] == '\0')
return 1;
else
return 0;
}
@C_Codings
#include<stdio.h>
int stringCompare(char[], char[]);
void main()
{
char str1[100], str2[100];
int compare;
clrscr();
printf("Enter first string : ");
scanf("%s", str1);
printf("Enter second string : ");
scanf("%s", str2);
compare = stringCompare(str1, str2);
if (compare == 1)
printf("\nBoth strings are equal.");
else
printf("\nBoth strings are not equal.");
getch();
}
int stringCompare(char str1[], char str2[])
{
int i = 0, flag = 0;
while (str1[i] != '\0' && str2[i] != '\0')
{
if (str1[i] != str2[i])
{
flag = 1;
break;
}
i++;
}
if (flag == 0 && str1[i] == '\0' && str2[i] == '\0')
return 1;
else
return 0;
}
@C_Codings
96. Program to convert string from uppercase to lowercase.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str[20];
int i;
clrscr();
printf("Enter string : ");
gets(str);
for (i = 0; i <= strlen(str); i++)
{
if (str[i] >= 65 && str[i] <= 90)
{
str[i] = (char) (str[i] + 32);
}
}
printf("String in lowercase : %s", str);
getch();
}
@C_Codings
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str[20];
int i;
clrscr();
printf("Enter string : ");
gets(str);
for (i = 0; i <= strlen(str); i++)
{
if (str[i] >= 65 && str[i] <= 90)
{
str[i] = (char) (str[i] + 32);
}
}
printf("String in lowercase : %s", str);
getch();
}
@C_Codings
97. Program to copy char array / copy string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char source[] = "C program";
char destination[50];
clrscr();
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char source[] = "C program";
char destination[50];
clrscr();
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
getch();
}
@C_Codings
98. Program to copy string without using strcpy() function.
#include<stdio.h>
#include<conio.h>
void strCopy(char[], char[]);
void main()
{
char str1[100], str2[100];
printf("Enter any string: ");
scanf("%s", str1);
strCopy(str1, str2);
printf("After copying: %s", str2);
getch();
}
void strCopy(char str1[], char str2[])
{
int i = 0;
while (str1[i] != '\0')
{
str2[i] = str1[i];
i++;
}
str2[i] = '\0';
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void strCopy(char[], char[]);
void main()
{
char str1[100], str2[100];
printf("Enter any string: ");
scanf("%s", str1);
strCopy(str1, str2);
printf("After copying: %s", str2);
getch();
}
void strCopy(char str1[], char str2[])
{
int i = 0;
while (str1[i] != '\0')
{
str2[i] = str1[i];
i++;
}
str2[i] = '\0';
}
@C_Codings
99. Program to count frequency of characters in a string.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str[1500];
int c = 0, count[26] = {0};
clrscr();
printf("Enter a string : ");
gets(str);
while (str[c] != '\0')
{
if (str[c] >= 'a' && str[c] <= 'z')
count[str[c] - 'a']++;
c++;
}
for (c = 0; c < 26; c++)
{
if (count[c] != 0)
printf("%c occurs %d times in the string.\n", c + 'a', count[c]);
}
getch();
}
@C_Codings
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str[1500];
int c = 0, count[26] = {0};
clrscr();
printf("Enter a string : ");
gets(str);
while (str[c] != '\0')
{
if (str[c] >= 'a' && str[c] <= 'z')
count[str[c] - 'a']++;
c++;
}
for (c = 0; c < 26; c++)
{
if (count[c] != 0)
printf("%c occurs %d times in the string.\n", c + 'a', count[c]);
}
getch();
}
@C_Codings
100. Program to count total number of uppercase and lowercase in a string.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int upper = 0, lower = 0;
char ch[80];
int i;
clrscr();
printf("Enter string : ");
gets(ch);
i = 0;
while (ch[i] != '\0')
{
//uppercase counter
if (ch[i] >= 'A' && ch[i] <= 'Z')
{
upper++;
}
//lowercase counter
if (ch[i] >= 'a' && ch[i] <= 'z')
{
lower++;
}
i++;
}
printf("\nUppercase Letters : %d", upper);
printf("\nLowercase Letters : %d", lower);
getch();
}
@C_Codings
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int upper = 0, lower = 0;
char ch[80];
int i;
clrscr();
printf("Enter string : ");
gets(ch);
i = 0;
while (ch[i] != '\0')
{
//uppercase counter
if (ch[i] >= 'A' && ch[i] <= 'Z')
{
upper++;
}
//lowercase counter
if (ch[i] >= 'a' && ch[i] <= 'z')
{
lower++;
}
i++;
}
printf("\nUppercase Letters : %d", upper);
printf("\nLowercase Letters : %d", lower);
getch();
}
@C_Codings
101. Program to calculate the length of string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100];
int length;
clrscr();
printf("\nEnter a string to calculate it's length : ");
gets(a);
length = strlen(a);
printf("\nLength of entered string is : %d", length);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100];
int length;
clrscr();
printf("\nEnter a string to calculate it's length : ");
gets(a);
length = strlen(a);
printf("\nLength of entered string is : %d", length);
getch();
}
@C_Codings
102. Program to calculate the length of string without using strlen() function.
#include<stdio.h>
#include<conio.h>
void main()
{
char s[1000], i;
clrscr();
printf("Enter a string : ");
scanf("%s", s);
for (i = 0; s[i] != '\0'; ++i);
printf("Length of string : %d", i);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
char s[1000], i;
clrscr();
printf("Enter a string : ");
scanf("%s", s);
for (i = 0; s[i] != '\0'; ++i);
printf("Length of string : %d", i);
getch();
}
@C_Codings
103. Program to check the palindrome of string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char text[100];
int begin, middle, end, length = 0;
clrscr();
printf("Enter the string to check if it is a palindrome : \n");
gets(text);
while (text[length] != '\0')
{
length++;
}
end = length - 1;
middle = length / 2;
for (begin = 0; begin < middle; begin++)
{
if (text[begin] != text[end])
{
printf("\nString is not a palindrome.");
break;
}
end--;
}
if (begin == middle)
printf("\nString is Palindrome.");
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char text[100];
int begin, middle, end, length = 0;
clrscr();
printf("Enter the string to check if it is a palindrome : \n");
gets(text);
while (text[length] != '\0')
{
length++;
}
end = length - 1;
middle = length / 2;
for (begin = 0; begin < middle; begin++)
{
if (text[begin] != text[end])
{
printf("\nString is not a palindrome.");
break;
}
end--;
}
if (begin == middle)
printf("\nString is Palindrome.");
getch();
}
@C_Codings
104. Program to check the palindrome of word using array.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char word[100];
int length, counter;
clrscr();
printf("Enter a word : ");
scanf("%s", word);
length = strlen(word);
int flag = 1;
for (counter = 0; counter < length / 2 && flag; counter++)
{
if (word[counter] != word[length - counter - 1])
{
flag = 0;
break;
}
}
if (flag)
{
printf("\n%s is a palindrome.", word);
}
else
{
printf("\n%s is NOT a palindrome.", word);
}
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char word[100];
int length, counter;
clrscr();
printf("Enter a word : ");
scanf("%s", word);
length = strlen(word);
int flag = 1;
for (counter = 0; counter < length / 2 && flag; counter++)
{
if (word[counter] != word[length - counter - 1])
{
flag = 0;
break;
}
}
if (flag)
{
printf("\n%s is a palindrome.", word);
}
else
{
printf("\n%s is NOT a palindrome.", word);
}
getch();
}
@C_Codings