⁕ A C program to calculate the area of a circle where the radius is given.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(void)
{
float r,a,pi=3.1416;
//r=radius,a=area,
printf("Enter the radius of circle=");
scanf("%f", &r);
a=pi*r*r;
printf("area of circle=%.3f", a);
getch();
return 0;
}
⁕A C program which convert temperature from Fahrenheit to Celsius.
#include<stdio.h>
int main(void)
{
float F,C;
printf("Enter the value of Farenheit :");
scanf("%f", &F);
// C/5=(F-32)/9
C=(F-32)*5/9;
printf("The value of Centigrade is: %.0f\n", C);
//here “\n” is used for going to a new line.
return 0;
}
⁕A C program which convert temperature from Celsius to Fahrenheit.
#include<stdio.h>
int main()
{
int c,f;
printf("Enter temperature in celcius:");
scanf("%d",&c);
//(F-32)/9=C/5
f=((c/5)*9)+32;
printf("Temperature in Farenheit is: %d",f);
return 0;
}
*sum,sub,mul,div,avg,area(triangle's area considering a=height
and b=base)of two numbers.
#include<stdio.h>
int main()
{
float a,b,sum,sub,mul,div,avg,area;
printf("Enter two numbers:");
scanf("%f %f",&a,&b);
sum=(a+b);
sub=(a-b);
mul=(a*b);
div=(a/b);
avg=(sum/2);
printf("sum is: %f\n",sum);
printf("sub is: %f\n",sub);
printf("mul is: %f\n",mul);
printf("div is:%f\n",div);
printf("avg is:%f\n",avg);
//If a=height and b=base of a triangle
area=(0.5*a*b);
printf("area is: %f", area);
return 0;
}
*A program to convert decimal to octal and hexadecimal value.
#include<stdio.h>
int main()
{
int a;
printf("Enter the decimal number:");
scanf("%d",&a);
printf("\nOctal value is:%o",a);
printf("\nHexadecimal value is:%x",a);
//use %o for octal and %x for hexadecimal
return 0;
}
*A program to convert octal to decimal and hexadecimal.
#include<stdio.h>
int main()
{
int a;
printf("Enter the octal number:");
scanf("%o",&a);
printf("\decimal value is:%d",a);
printf("\nHexadecimal value is:%x",a);
//use %o for octal and %x for hexadecimal
return 0;
⁕The values of x & n after each statement of the following code segment:
x=n++; [POST INCREMENT]
#include<stdio.h>
int main(void)
{
float x,n;
printf("Enter the value of n=");
scanf("%f", &n);
x=n++;
printf("The value of X and n is precisely %f and %f",x,n);
return 0;
}
⁕The values of x & n after each statement of the following code segment:
x=++n;
[PRE INCREMENT]
#include<stdio.h>
int main(void)
{
float x,n;
printf("Enter the value of n=");
scanf("%f", &n);
x=++n;
printf("The value of X and n is precisely %f and %f",x,n);
return 0;
}
⁕1x+2x+3x+4x+5x+…+XX
#include<stdio.h>
#include<math.h>
int main(void)
{
int x,i,z,sum=0;
printf("Enter the value of x:");
scanf("%d",&x);
for(i=1;i<=x;i=i++)
{
sum=sum+pow(i,x);
}
printf("The value of sum is %d",sum);
return 0;
}
⁕11+22+33+44+55+…+NN
#include<stdio.h>
int main(void)
{
int n,i,sum;
sum=0;
printf("Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i=i+1)
sum=sum+pow(i,i);
printf("The value of sum is %d",sum);
return 0;
}
⁕A Program of Sine, Cos series
#include<stdio.h>
int main(void)
{
int i,j,n;
float x,t,result;
printf("1.sinx");
printf("2.cosx");
printf("Put your choice:");
scanf("%d",&j);
if(j==1)
{
printf("Enter the value for x;");
scanf("%d", &x);
printf("Enter the value for n;");
scanf("%d", &n);
x=x*3.1416/180;
t=x;
result=x;
for(i=1;i<=n;i++)
{
t=t*(-1)*x*x/(2*i*(2*i+1));
result=result+t;
}
printf("the value of sin(%f) = %.4f", x, result);
}
else if(j==2)
{
printf("Enter the value for x;");
scanf("%d", &x);
printf("Enter the value for n;");
scanf("%d", &n);
x=x*3.1416/180;
for(i=1; i<=n; i++)
{
t=t*(-1)*x*x/(2*i*(2*i-1));
result=result+t;
}
printf("the value of cos(%f) = %.4f", x, result);
}
return 0;
}
⁕A program to calculate the number of digits where the number is given as
input.
#include<stdio.h>
int main(void)
{
long int num;
int count=0;
printf("Enter your number:");
scanf("%11d",&num);
do
{ count++;
num /=10;
}
while(num!=0);
printf("Total digits:%d",count);
return 0;
}
⁕A program to calculate the sum of it’s digits of a given number.
#include<stdio.h>
int main(void)
{
int n,t,sum,reminder;
sum=0;
printf("Enter any number:");
scanf("%d",&n);
t=n;
while (t!=0)
{
reminder=t%10;
sum=sum+reminder;
t=t/10;
}
printf("Sum of the digits of the given number is: %d",sum);
return 0;
}
⁕A program to find second large number(middle number according to value) of
three numbers.
#include<stdio.h>
int main(void)
{
int a,b,c;
printf("Enter the value of a,b,c:");
scanf("%d %d %d",&a,&b,&c);
if ((a>b)&&(a<c))
//if one of the condition is false the value is false. Both the condition should be true.
printf("The second maximum number is %d",a);
else if ((a<b)&&(b<c))
printf("The second maximum number is %d",b);
else
printf("The second maximum number is %d",c);
return 0;
}
⁕A program to find large number of three numbers.
#include<stdio.h>
int main(void)
{
int a,b,c;
printf("Enter the value of a,b,c:");
scanf("%d %d %d",&a,&b,&c);
if((a>b)&&(a>c))
//if one of the condition is false the value is false. Both the condition should be true.
printf("The maximum number is %d",a);
else if((b>a)&&(b>c))
printf("The maximum number is %d",b);
else
printf("The maximum number is %d",c);
return 0;
}
⁕A program to find minimum and maximum number of four numbers.
#include<stdio.h>
int main()
{
int a,b,c,d;
printf("Enter your numbers:");
scanf("%d %d %d %d",&a,&b,&c,&d);
if((a<b)&&(a<c)&&(a<d))
printf("The minimum number is %d",a);
else if ((b<a)&&(b<c)&&(b<d))
printf("The minimum number is %d",b);
else if((c<a)&&(c<b)&&(c<d))
printf("The minimum number is %d",c);
else
printf("The minimum number is %d",d);
return 0;
}
⁕A program to find GCD(Greatest Common Divisor)/HCF(Highest
Common Factor)/GCF(Greatest Common Factor)of two given
numbers.
[In mathematics, the greatest common divisor (gcd) of two or more integers, which are not
all zero, is the largest positive integer that divides each of the integers. For two integers x, y,
the greatest common divisor of x and y is denoted . For example, the gcd of 8(1,2,4,8) and
12(1,2,3,4,6,12) is 4,
the GCD of 20(1,2,4,5,10,20) and 28(1,2,4,7,14,28) is 4. ]
[GCD of two integer number is the Highest common
divisor/factor(গুণনীয়ক) that evenly divides those two numbers]
#include<stdio.h>
int main(void)
{
int a,b,x;
// a and b are the two numbers and x is the gcd of those two numbers.
printf("enter the numbers:");
scanf("%d %d",&a,&b);
if (a<b)
x=a;
else
x=b;
again:
if (a%x==0 && b%x==0)
printf("The GCD of the numbers %d and %d is %d",a,b,x);
else{
x=x-1;
goto again;
}
return 0;
}
⁕ A program to find LCM(Least Common Multiple) of two given numbers.
[The Least Common Multiple ( LCM ) is also referred to as the Lowest Common Multiple
( LCM ) and Least Common Divisor ( LCD) . For two integers a and b, denoted LCM(a,b),
the LCM is the smallest positive integer that is evenly divisible by both a and b. For
example, LCM(2,3) = 6 and LCM(6,10) = 30. N.B. For calculating the LCM of two prime
numbers just multiply the prime numbers. Ex: LCM(2,3) = 6]
[LCM of two integer number is the lowest common multiple( গুণিতক)
that evenly divides those two numbers]
#include<stdio.h>
int main(void)
{
int a,b,x;
printf("Enter the two numbers:");
scanf("%d %d", &a,&b);
if (a>b)
x=a;
else
x=b;
again:
if (x%a==0 && x%b==0)
printf("The LCM of the numbers %d and %d is %d",a,b,x);
else{
x=x+1;
goto again;
}
return 0;
}
⁕A program of Fibonacci numbers.
#include<stdio.h>
int main(void)
{
int i,num,fib0,fib1,fib;
printf("Enter the last digit:");
scanf("%d",&num);
fib0=0;fib1=1;
for(i=1;i<=num;i++)
{
fib=fib0+fib1;
fib1=fib0;
fib0=fib;
printf("%3d",fib);
}
return 0;
}
*A program to find y=ax.
#include<stdio.h>
int main(void)
{
int a,x,y;
printf("Enter the value of a and x:");
scanf("%d %d",&a,&x);
y=pow(a,x);
printf("The value is:%d",y);
return 0;
}
⁕A program to convert a decimal number to a binary number.
#include <stdio.h>
#include <math.h>
long decimalToBinary(int decimalnum)
{
long binarynum = 0;
int rem, temp = 1;
while (decimalnum!=0)
{
rem = decimalnum%2;
decimalnum = decimalnum / 2;
binarynum = binarynum + rem*temp;
temp = temp * 10;
}
return binarynum;
}
int main()
{
int decimalnum;
printf("Enter a Decimal Number: ");
scanf("%d", &decimalnum);
printf("Equivalent Binary Number is: %ld",
decimalToBinary(decimalnum));
return 0;
}
⁕A program of histogram/frequency of an array elements.
#include <stdio.h>
#define MAX 10
#define IN 1
#define OUT 0
int main()
{
int c, len, state;
int nlength[MAX];
c = len = 0;
state = 1;
for(int i = 0; i < 10; ++i)
nlength[i] = 0;
while ((c = getchar()) != EOF) {
++len;
if (c == ' ' || c == '\n' || c == '\t') {
--len;
state = OUT;
}
if(state == OUT) {
if(len != 0 && len <= MAX)
++nlength[len];
len = 0;
state = IN;
}
}
for (int i = 0; i <= MAX; ++i) {
printf("%d ", i);
for (int a = 0; a < nlength[i]; ++a)
printf("*");
printf("\n");
}
return 0;
}
*A program of maximum sub array sum.
#include<stdio.h>
int main(void)
{
int i;
int arr1[]={7,4,7,-6,5,2,-5,6,8};
int ctr=sizeof(arr1)/sizeof(arr1[0]);
printf("The given array is: ");
for (i=0;i<ctr;i++)
{
printf("%d ",arr1[i]);
}
printf("\n");
printf("The largest sum of the subrray is:%d \n",maxSum(arr1,ctr));
return 0;
}
int maxSum(int a[],int n)
{
int i,j,k;
int sum,maxSum=0;
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
sum=0;
for(k=i;k<j;k++);
{
sum=sum+a[k];
}
if (sum>maxSum)
maxSum=sum;
}
}
return maxSum;
}
⁕A program of Prime factorization.
#include <stdio.h>
main()
{
int i,j,num,Prime;
printf("Enter the number to print prime factors:");
scanf("%d",&num);
printf("All prime factors of %d are:",num);
for(i=2;i<=num;i++)
{
if(num%i==0)
{
Prime=1;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
Prime=0;
break;
}
}
if (Prime==1)
{
printf("%d,",i);
}
}
}
return 0;
}
⁕A program of Itoa.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int a;
char buffer[25];
printf("Enter a number:");
scanf("%d",&a);
itoa(a,buffer,2);
printf("Binary value is =%s\n",buffer);
itoa(a,buffer,10);
printf("Decimal value is =%s\n",buffer);
itoa(a,buffer,16);
printf("Hexadecimal value is =%s\n",buffer);
return 0;
}
⁕A program of Atoi.
#include<stdio.h>
#include<stdio.h>
main()
{
char number[30];
int num;
printf("Enter any integer:");
fgets(number,30,stdin);
num=atoi(number);
num=atoi(number);
printf("Converted integer=%d",num);
return 0;
}
⁕A program of index short.
#include<stdio.h>
main()
{
int i,j,num,arr[300],pos,temp;
printf("Enter number:\n");
scanf("%d",&num);
printf("Enter %d integers\n",num);
for(i=0;i<=num;i++)
scanf("%d",&num);
for(i=0;i<(num-1);i++)
{
pos=i;
for(j=i+1;j<num;j++)
{
if (arr[pos]>arr[j])
pos=j;
}
if (pos!=j)
{
temp=arr[i];
arr[i]=arr[pos];
arr[pos]=temp;
}
}
printf("Selection shorting inscending order:\n");
for(i=0;i<num;i++)
printf("%d\n",arr[i]);
return 0;
}
⁕A program of Standard deviation.
#include <math.h>
#include <stdio.h>
float calculateSD(float data[]);
int main()
{
int i;
float data[10];
printf("Enter 10 elements: ");
for (i = 0; i < 10; ++i)
scanf("%f", &data[i]);
printf("\nStandard Deviation = %.6f", calculateSD(data));
return 0;
}
float calculateSD(float data[])
{
float sum = 0.0, mean, SD = 0.0;
int i;
for (i = 0; i < 10; ++i) {
sum += data[i];
}
mean = sum / 10;
for (i = 0; i < 10; ++i)
SD += pow(data[i] - mean, 2);
return sqrt(SD / 10);
}
⁕A program of Merge two sorted arrays into another sorted array.
#include <stdio.h>
void merge(int [], int, int [], int, int []);
int main()
{
int a[100], b[100], m, n, c, sorted[200];
printf("Input number of elements in first array\n");
scanf("%d", &m);
printf("Input %d integers\n", m);
for (c = 0; c < m; c++) {
scanf("%d", &a[c]);
}
printf("Input number of elements in second array\n");
scanf("%d", &n);
printf("Input %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &b[c]);
}
merge(a, m, b, n, sorted);
printf("Sorted array:\n");
for (c = 0; c < m + n; c++) {
printf("%d\n", sorted[c]);
}
return 0;
}
void merge(int a[], int m, int b[], int n, int sorted[]) {
int i, j, k;
j = k = 0;
for (i = 0; i < m + n;) {
if (j < m && k < n) {
if (a[j] < b[k]) {
sorted[i] = a[j];
j++;
}
else {
sorted[i] = b[k];
k++;
}
i++;
}
else if (j == m) {
for (; i < m + n;) {
sorted[i] = b[k];
k++;
i++;
}
}
else {
for (; i < m + n;) {
sorted[i] = a[j];
j++;
i++;
}
}
}
return 0;
}
⁕A program of Insert into array from.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int i, size, num, pos;
printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter element to insert : ");
scanf("%d", &num);
printf("Enter the element position : ");
scanf("%d", &pos);
if(pos > size+1 || pos <= 0)
{
printf("Invalid position! Please enter position between 1 to %d",
size);
}
else
{
for(i=size; i>=pos; i--)
{
arr[i] = arr[i-1];
}
arr[pos-1] = num;
size++;
printf("Array elements after insertion : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
}
return 0;
}
⁕A program of Reverse an array.
#include<stdio.h>
main()
{
int n,*a,i;
printf("Enter integers:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
reverse(n,a);
return 0;
}
void reverse(int n,int *a)
{
int i;
printf("Reverse array is:\n");
for(i=n-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
⁕A program of Change case of an character array.
#include<stdio.h>
main()
{
int c=0;
char ch,s[1000];
printf("Input a string:\n");
gets(s);
while(s[c]!='\0')
{
ch=s[c];
if (ch>='A'&& ch<='Z')
s[c]=s[c]+32;
else if(ch>='a'&& ch<='z')
s[c]=s[c]-32;
c++;
}
printf("%s\n",s);
return 0;
}
⁕A program of Remove all occurrences of a particular elements from
an array.
#include<stdio.h>
#include<string.h>
main()
{
char s[100],c,temp=1;
int i,j,k=0,n;
printf("Enter the string:");
gets(s);
printf("Enter character:");
c=getchar();
for(i=0;s[i];i++)
{
s[i]=s[i+k];
if(s[i]==c)
{
k++;
i--;
}
}
printf("%s",s);
return 0;
}
⁕A program of Find 2nd max from an array.
#include<stdio.h>
main()
{
int a[50];
int n,i,slarge,large;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
large=slarge=a[0];
for(i=1;i<n;i++)
{
if(large<a[i])
{
slarge=large;
large=a[i];
}
else if(slarge<a[i]&& a[i]!=large)
{
slarge=a[i];
}
}
printf("The Second Largest element in the guven array is:
%d",slarge);
return 0;
}
⁕A program to check Anagram.
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20],str2[20];
int len,len1,len2,i,j,found=0,notfound=0;
printf("Enter first string:");
gets(str1);
printf("Enter second string:");
gets(str2);
len1=strlen(str1);
len2=strlen(str2);
if (len1==len2)
{
len=len1;
for(i=0;i<len;i++)
{
found=0;
for(j=0;j<len;j++)
{
if(str1[i]==str2[j])
{found=1;
break;
}
}
if (found ==0)
{
notfound=1;
break;
}
}
if (notfound==1)
printf("Strings are not Anagram");
else
printf("Strings are Anagram");
}
else
printf("Both the strings must contain same number of characters
to be an Anagram Strings");
return 0;
}
⁕A program to check palindrome.
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20],str2[20];
int len,len1,len2,i,j,found=0,notfound=0;
printf("Enter first string:");
gets(str1);
printf("Enter second string:");
gets(str2);
len1=strlen(str1);
len2=strlen(str2);
if (len1==len2)
{
len=len1;
for(i=0;i<len;i++)
{
found=0;
for(j=0;j<len;j++)
{
if(str1[i]==str2[j])
{found=1;
break;
}
}
if (found ==0)
{
notfound=1;
break;
}
}
if (notfound==1)
printf("Strings are not Anagram");
else
printf("Strings are Anagram");
}
else
printf("Both the strings must contain same number of characters
to be an Anagram Strings");
return 0;
}
⁕A program To upper each word.
#include<stdio.h>
#define MAX 100
main()
{
char str[MAX]={0};
int i;
printf("Enter a string:");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
if(i==0)
{
if (str[i]>='a'&& str[i]<='z')
str[i]=str[i]-32;
continue;
}
if(str[i]==' ')
{
++i;
if(str[i]>='a' && str[i]<='z')
{
str[i]=str[i]-32;
continue;
}
}
else
{
if(str[i]>='A' && str[i]<='Z')
str[i]=str[i]+32;
}
}
printf("Capitalized string is:%s",str);
return 0;
}
⁕A program to count number of words.
#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;
printf("Enter the string:\n");
scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ' && s[i+1] != ' ')
count++;
}
printf("Number of words in given string are: %d\n", count + 1);
return 0;
}
⁕A program of Matrix Addition.
#include<stdio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i,j,m,n;
printf("How many rows and columns?");
scanf("%d%d",&m,&n);
printf("\nEnter first matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
printf("\nEnter second matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&b[i][j]);
printf("\nMatrix after addition:\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}
⁕A program of Matrix Multiplication.
#include<stdio.h>
int main(void)
{
int c, d, p, q, m, n, k, tot = 0;
int fst[10][10], sec[10][10], mul[10][10];
printf(" Enter the number of rows and columns for first matrix \n
");
scanf("%d%d", &m, &n);
printf(" Insert your matrix elements : \n ");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &fst[c][d]);
printf(" Enter the number of rows and columns for second matrix\
n");
scanf(" %d %d", &p, &q);
if (n != p)
printf(" Your given matrices cannot be multiplied with each other.
\n ");
else
{
printf(" Insert your elements for second matrix \n ");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &sec[c][d] );
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
tot = tot + fst[c][k] * sec[k][d];
}
mul[c][d] = tot;
tot = 0;
}
}
printf(" The result of matrix multiplication or product of the
matrices is: \n ");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d \t", mul[c][d] );
printf(" \n ");
}
}
return 0;
}