0% found this document useful (0 votes)
3 views84 pages

C Programming Code Final

The document contains various C programming code snippets that demonstrate algorithms for mathematical computations, including prime number checking, factorial calculation, Fibonacci series generation, palindrome checking, and GCD/LCM calculation. Each section provides a specific problem-solving approach, either iteratively or recursively, showcasing fundamental programming concepts. The code is attributed to Kishor Roy.

Uploaded by

Isabella Colebe
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views84 pages

C Programming Code Final

The document contains various C programming code snippets that demonstrate algorithms for mathematical computations, including prime number checking, factorial calculation, Fibonacci series generation, palindrome checking, and GCD/LCM calculation. Each section provides a specific problem-solving approach, either iteratively or recursively, showcasing fundamental programming concepts. The code is attributed to Kishor Roy.

Uploaded by

Isabella Colebe
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Prime Number Checking


#include<stdio.h>
int main()
{
int n,i,c=0;
printf("Enter the number to be checked:");
scanf("%d",&n);
for(i=2;i<=(n/2);i++)
{
if(n%i==0)
{
c++;
break;
}
}
if(c==1)
{
printf("Not Prime");
}
else
{
printf("Prime");
}
return 0;
}

© Kishor Roy
2. Prime Number List in range
#include<stdio.h>
int main()
{
int n,m,i,j,c;
printf("Enter the lower range:");
scanf("%d",&n);
printf("Enter the Upper range:");
scanf("%d",&m);

for(i=n;i<=m;i++)
{
c=0;
for(j=2;j<=(i/2);j++)
{
if(i%j==0)
{
c++;
break;
}
}
if(c==0)
{
printf("%d\t",i);
}
}
return 0;
}

© Kishor Roy
3. Prime Number Checking using
recursion
#include<stdio.h>

int IsPrime(int n, int i);

int main()
{
int n,i,c;
printf("Enter the number to be checked:");
scanf("%d",&n);
c=IsPrime(n,n/2);
if(c==0)
{
printf("Not Prime");
}
else
{
printf("Prime");
}
}

int IsPrime(int n, int i)


{
if(i==1)
{
return 1;
}
else
{
if(n%i==0)
return 0;
else
IsPrime(n,i-1);
}
}

© Kishor Roy
4. Factorial Using Recursion
#include<stdio.h>

int Factorial(int n);

int main()
{
int n,m;
printf("Please Enter the Number: ");
scanf("%d",&n);
m=Factorial(n);
printf("The Factorial of %d is %d", n, m);
return 0;
}

int Factorial(int n)
{
if(n<=1)
return 1;
else
return n*Factorial(n-1);
}

© Kishor Roy
5. Fibonacci Series
#include<stdio.h>
int main()
{
int a=0,b=1,temp,i,n;
printf("Enter the No of Terms: ");
scanf("%d", &n);
printf("The series is %d\t%d\t",a,b);
temp=a+b;
for(i=3;i<=n;i++)
{
printf("%d\t",temp);
a=b;
b=temp;
temp=a+b;
}
return 0;
}

© Kishor Roy
6. Fibonacci series using Recursion
#include<stdio.h>
int fibonacci(int n);

int main()
{
int n,i;
printf("Enter the No of Terms: ");
scanf("%d",&n);
printf("The series is : ");
for(i=0;i<n;i++)
{
printf("%d\t",fibonacci(i));
}
return 0;
}

int fibonacci(int n)
{
if(n<=1)
return n;
else
return fibonacci(n-1)+fibonacci(n-2);
}

© Kishor Roy
7. Factors of a Number
#include<stdio.h>
int main()
{
int n,i;
printf("Enter The Number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
printf("%d\t",i);
}
}
return 0;
}

© Kishor Roy
8. Factors of Number Using Recursion
#include<stdio.h>
void factor(int n, int i);

int main()
{
int n,i;
printf("Enter The Number: ");
scanf("%d",&n);
factor(n,1);
return 0;
}

void factor(int n,int i)


{
if(i<=n)
{
if(n%i==0)
{
printf("%d\t",i);
}
factor(n,i+1);
}
}

© Kishor Roy
9. Palindrome Number Checking
#include<stdio.h>
int main()
{
int n,a,temp=0,i;
printf("Enter the number");
scanf("%d",&n);
a=n;
while(n!=0)
{
temp=(temp+(n%10));
n=n/10;
if(n==0)
{
break;
}
else
{
temp=temp*10;
}
}
if(a==temp)
{
printf("Palindrome");
}
else
{
printf("Not palindrome");
}
return 0;
}

© Kishor Roy
10. Armstrong Number
#include<stdio.h>
int main()
{
int n,c=0,a,b,sum=0;
printf("Enter the Number: ");
scanf("%d",&n);
a=n;
b=n;
while(n!=0)
{
c++;
n=n/10;
}
while(b!=0)
{
sum=sum+pow((b%10),c);
b=b/10;
}
if(a==sum)
{
printf("Armstrong Number");
}
else
{
printf("Not Armstrong Number");
}
return 0;
}

© Kishor Roy
[Link] third variable
#include<stdio.h>
int main()
{
int x,y;
x=5;
y=10;

x=x+y;
y=x-y;
x=x-y;

printf("x=%d y=%d",x,y);
return 0;
}

© Kishor Roy
12. Perfect Number Checking
#include<stdio.h>
int main()
{
int n,i,sum=0;
printf("Enter the Number: ");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(n==sum)
{
printf("Perfect Number");
}
else
{
printf("Not Perfect Number");
}
return 0;
}

© Kishor Roy
[Link] Number Checking Using
Recursion
#include<stdio.h>
int perfect(int n, int i);

int main()
{
int n,m;
printf("Enter the Number: ");
scanf("%d", &n);
m=perfect(n,n/2);
if(m==n)
{
printf("Perfect Number");
}
else
{
printf("Not Perfect Number");
}
return 0;
}
int perfect(int n, int i)
{
if(i==1)
return 1;
else
{
if(n%i==0)
return i+perfect(n,i-1);
else
return perfect(n,i-1);
}

© Kishor Roy
[Link] of Nterms of Natural Numbers
using recursion
#include<stdio.h>
int Sum(int n);

int main()
{
int n;
printf("Enter the value of last number: ");
scanf("%d",&n);
printf("The Sum is %d",Sum(n));
return 0;
}
int Sum(int n)
{
if(n<=1)
return 1;
else
return n+Sum(n-1);
}

© Kishor Roy
[Link] Search
#include<stdio.h>
int main()
{
int a[20],i,n,m,c=0;
printf("Enter the no of elements in the array:");
scanf("%d",&n);
printf("Enter the Array elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the No to be Searched");
scanf("%d",&m);
for(i=0;i<n;i++)
{
if(a[i]==m)
{
c=i;
break;
}
}
if(c!=0)
{
printf("Element Found in the %dth index",c);
}
else
{
printf("Element Not Found");
}
return 0;
}

© Kishor Roy
[Link] Sorting
#include<stdio.h>
int main()
{
int a[20],i,j,n,temp;
printf("Enter the no of elements in the array: ");
scanf("%d",&n);
printf("Enter the array Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The Sorted array is: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}

© Kishor Roy
17.Χͳ‫ͳﺪ‬Κʹ‫ͳﺪ‬Κ͵‫ͳﺪ‬ΚͶ‫‛‛‛‛ﺪ‬
#include<stdio.h>
int main()
{
float n,i,sum=0;
printf("Enter The limit of the series: ");
scanf("%f",&n);
for(i=1;i<=n;i++)
{
sum=sum+(1/i);
}
printf("The sum is %.2f",sum);
return 0;
}

© Kishor Roy
18.Χͳ‫ͳﺪ‬Κʹ‫ͳﺪ‬Κ͵‫ͳﺪ‬ΚͶ‫‛‛‛‛ﺪ‬
Using Recursion
#include<stdio.h>
float sum(float n);

int main()
{
float n,i,s;
printf("Enter The limit of the series: ");
scanf("%f",&n);
s=sum(n);
printf("The sum is %.2f",s);
return 0;
}
float sum(float n)
{
if(n==1)
return 1;
else if(n<=0)
return 0;
else
return (1/n)+sum(n-1);
}

© Kishor Roy
19. ͳζ͵‫ʹﺪ‬ζͷ‫͵ﺪ‬ζ͹‫‛‛‛‛‛‛ﺪ‬
#include<stdio.h>
int main()
{
int n,i,sum=0;
printf("Enter the Value of N: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*((2*i)+1));
}
printf("The sum is %d ", sum);
return 0;
}

© Kishor Roy
20.ͳ‫ﺪ‬Χͳ‫ʹﺪ‬Ψ‫ﺪ‬Χͳ‫͵ﺪʹﺪ‬Ψ‫ﺪ‛ﺪ‬Χ
#include<stdio.h>
int main()
{
int n, i,j,sum=0;
printf("Enter The limit of the series: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
sum=sum+j;
}
}
printf("The sum is %d ", sum);
return 0;
}

© Kishor Roy
[Link] Solving and find roots
#include<stdio.h>
int main()
{
float a,b,c,d, x1,x2,i1,i2;
printf("Enter the value of a,b,c : ");
scanf("%f %f %f",&a,&b,&c);

d=(b*b)-(4*a*c);
if(d>0)
{
x1=((-b)+sqrt(d))/(2*a);
x2=((-b)-sqrt(d))/(2*a);
printf("The first root is %.2f\n",x1);
printf("The Second root is %.2f",x2);
}
else if(d==0)
{
x1=(-b/(2*a));
printf("The first root is %.2f\n",x1);
printf("The Second root is %.2f",x1);
}
else
{
x1=x2=(-b/(2*a));
i1=i2=(sqrt(-d))/(2*a);
printf("The first root is %.2f+i%.2f\n",x1,i1);
printf("The Second root is %.2f - i%.2f\n",x2,i2);
}
return 0;
}

© Kishor Roy
[Link] and Combination
#include<stdio.h>
int main()
{
int a,b,c=1,d=1,e=1,i,p,m;
printf("Enter the value of n and r:");
scanf("%d %d",&a,&b);
if(a>=b)
{
for(i=1;i<=a;i++)
{
c=c*i;
}
for(i=1;i<=(a-b);i++)
{
d=d*i;
}
for(i=1;i<=b;i++)
{
e=e*i;
}
p=c/d;
m=c/(d*e);
printf("The Permutation is: %d\n",p);
printf("The Combination is: %d",m);
}
else
{
printf("Enter the correct value of n and r");
}
return 0;
}

© Kishor Roy
[Link] the number of reoccurring of a
given digit from a Number
#include<stdio.h>
int main()
{
int a,b,c=0;
printf("Enter The number: ");
scanf("%d",&a);
printf("Enter the reoccurring digit: ");
scanf("%d",&b);
while(a!=0)
{
if((a%10)==b)
{
c++;
}
a=a/10;
}
printf("%d times found",c);
return 0;
}

© Kishor Roy
[Link] Square Checking
#include<stdio.h>
int main()
{
float a,b,i,c=0;
printf("Enter the Number: ");
scanf("%f",&a);
b=sqrt(a);
for(i=1;i<a;i++)
{
if(i==b)
{
c++;
break;
}
}
if(c==1)
{
printf("Perfect Square Number");
}
else
{
printf("Not Perfect Square");
}
return 0;
}

© Kishor Roy
[Link] & LCM
#include<stdio.h>
int main()
{
int a,b,i,gcd,lcm;
printf("Enter the value of A and B: ");
scanf("%d %d",&a,&b);
for(i=1;i<=a && i<=b;i++)
{
if(a%i==0 && b%i==0)
{
gcd=i;
}
}
lcm=(a*b)/gcd;
printf("The value of GCD is %d\n",gcd);
printf("The value of lcm is %d",lcm);
return 0;
}

© Kishor Roy
[Link] & LCM Using Recursion
#include<stdio.h>
int gcdlcm(int a,int b);
int main()
{
int a,b,i,gcd,lcm;
printf("Enter the value of A and B: ");
scanf("%d %d",&a,&b);
gcd=gcdlcm(a,b);
lcm=(a*b)/gcd;
printf("The value of GCD is %d\n",gcd);
printf("The value of lcm is %d",lcm);
return 0;
}
int gcdlcm(int a,int b)
{
if(b!=0)
return gcdlcm(b, a%b);
else
return a;
}

© Kishor Roy
[Link] & Combination Using
Recursion
#include<stdio.h>
int factorial(int n);
int main()
{
int a,b,c=1,d=1,e=1,i,p,m;
printf("Enter the value of n and r:");
scanf("%d %d",&a,&b);
if(a>b)
{
c=factorial(a);
d=factorial(a-b);
e=factorial(b);
p=c/d;
m=c/(d*e);
printf("The Permutation is: %d\n",p);
printf("The Combination is: %d",m);
}
else
{
printf("Enter the correct value of n and r");
}
return 0;
}
int factorial(int n)
{
if(n<=1)
return 1;
else
return n*factorial(n-1);
}

© Kishor Roy
[Link] an element to array
#include<stdio.h>
int main()
{
int a[20],n,i,m,p;
printf("Enter how many numbers in the array : ");
scanf("%d",&n);
printf("Enter the array elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the position to be insert: ");
scanf("%d",&p);
printf("Enter the value to be inserted");
scanf("%d",&m);
for(i=n-1; i>=(p-1);i--)
{
a[i+1]=a[i];
}
a[p-1]=m;
for(i=0;i<=n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}

© Kishor Roy
[Link] an element from Array
#include<stdio.h>
int main()
{
int a[20],n,i,pos;
printf("Enter the No of elements in the array: ");
scanf("%d",&n);
printf("Enter the Array elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the Position to deleted: ");
scanf("%d",&pos);
if(pos>=n+1)
{
printf("Deletion is not possible");
}
else
{
for(i=pos-1;i<n-1;i++)
{
a[i]=a[i+1];
}
printf("The Resultant Array is: ");
for(i=0;i<n-1;i++)
{
printf("%d\t",a[i]);
}
}
return 0;
}

© Kishor Roy
[Link] Duplicates From an Array
#include<stdio.h>
int main()
{
int a[20],i,n,j,k;
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
printf("Enter the array Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
j--;
}
}
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}

© Kishor Roy
[Link] to Binary
#include<stdio.h>
int main()
{
int n,temp=0,i=1;
printf("Enter the decimal number: ");
scanf("%d",&n);
while(n!=0)
{
temp=temp+((n%2)*i);
i=i*10;
n=n/2;
}
printf("The Binary value is: %d",temp);
return 0;
}

© Kishor Roy
[Link] to Decimal
#include<stdio.h>
int main()
{
int a,temp=0,i=0;
printf("Enter the Binary Number");
scanf("%d",&a);
while(a!=0)
{
temp=temp+(a%10)*pow(2,i);
i++;
a=a/10;
}
printf("The decimal value is %d",temp);
return 0;
}

© Kishor Roy
[Link] to Octal
#include<stdio.h>
int main()
{
int n,temp=0,i=1;
printf("Enter the decimal number: ");
scanf("%d",&n);
while(n!=0)
{
temp=temp+((n%8)*i);
i=i*10;
n=n/8;
}
printf("The octal value is: %d",temp);
return 0;
}

© Kishor Roy
[Link] to decimal
#include<stdio.h>
int main()
{
int a,temp=0,i=0;
printf("Enter the octal Number : ");
scanf("%d",&a);
while(a!=0)
{
temp=temp+(a%10)*pow(8,i);
i++;
a=a/10;
}
printf("The decimal value is %d",temp);
return 0;
}

© Kishor Roy
[Link] to Hexadecimal
#include<stdio.h>
int main()
{
int n,temp,i=1,j;
char hexa[100];
printf("Enter the decimal number: ");
scanf("%d",&n);
while(n!=0)
{
temp=n%16;
if(temp<10)
temp=temp+48;
else
temp=temp+55;
hexa[i++]=temp;
n=n/16;
}
for(j=i-1;j>0;j--)
{
printf("%c",hexa[j]);
}
return 0;
}

© Kishor Roy
[Link] to Decimal
#include<stdio.h>
int main()
{
char hexa[20];
int i, val, len, decimal=0;
printf("Enter the Hexadecimal Value: ");
gets(hexa);
len=strlen(hexa);
len--;
for(i=0;hexa[i]!='\0';i++)
{
if(hexa[i]>='0' && hexa[i]<='9')
{
val=hexa[i]-48;
}
else if(hexa[i]>='a' && hexa[i]<='f')
{
val=hexa[i]-87;
}
else if(hexa[i]>='A' && hexa[i]<='F')
{
val=hexa[i]-55;
}
decimal=decimal+val*pow(16,len);
len--;
}
printf("The decimal value is %d",decimal);
return 0;
}

© Kishor Roy
[Link] -01

#include<stdio.h>
int main()
{
int i,j,n;
printf("Enter the row number: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<=i;j++)
{
printf("*");
}
}
return 0;
}

© Kishor Roy
[Link] -01

#include<stdio.h>
int main()
{
int i,j,n;
printf("Enter the row number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%d",j);
}
}
return 0;
}

© Kishor Roy
[Link] -01

#include<stdio.h>
int main()
{
int n,i,j;
printf("Enter the row Number: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
printf("\n");
for(j=i;j>=1;j--)
{
printf("*");
}
}
return 0;
}

© Kishor Roy
[Link] -01

#include<stdio.h>
int main()
{
int n,i,j;
printf("Enter the row Number: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%d",j);
}
}
return 0;
}

© Kishor Roy
[Link] Triangle

#include<stdio.h>
int main()
{
int n,i,j,number=1;
printf("Enter the Number of row: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%d\t",number);
number++;
}
}
return 0;
}

© Kishor Roy
[Link] of star

#include<stdio.h>
int main()
{
int n,i,j;
printf("Enter the no of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=((2*n)-1);j++)
{
if(j>=(n-(i-1)) && j<=(n+(i-1)))
printf("*");
else
printf(" ");
}
}
return 0;
}

© Kishor Roy
[Link] triangle

#include <stdio.h>
int main() {
int rows, coef = 1, space, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i < rows; i++) {
for (space = 1; space <= rows - i; space++)
printf(" ");
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}

© Kishor Roy
[Link] Table
#include<stdio.h>
int main()
{
int n,i,s;
printf("Enter the number: ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("%d x %d = %d \n",n,i,(n*i));
}
return 0;
}

© Kishor Roy
[Link] number checking
#include<stdio.h>
int factorial(int n);

int main()
{
int n, temp=0, b;
printf("Enter the Number: ");
scanf("%d",&n);
b=n;
while(n!=0)
{
temp=temp+factorial(n%10);
n=n/10;
}
if(b==temp)
{
printf("Strong Number");
}
else
{
printf("Not Strong Number");
}
return 0;
}

int factorial(int n)
{
if(n<=1)
return 1;
else
return n*factorial(n-1);
}

© Kishor Roy
[Link] Maximum Element in Array
#include<stdio.h>
int main()
{
int a[20],i,j,n,temp;
printf("Enter the no of elements in the array: ");
scanf("%d",&n);
printf("Enter the array Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]>a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The Second Maximum Element is: ");
printf("%d\t",a[1]);
return 0;
}

© Kishor Roy
[Link] an array, Summation of pair =
given value
#include<stdio.h>
int main()
{
int a[20],n,i,j,c=0,m;
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
printf("Enter the elements of the array : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the compared value: ");
scanf("%d",&m);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((a[i]+a[j])==m)
{
printf("{%d , %d}",a[i],a[j]);

}
}
}
return 0;
}

© Kishor Roy
[Link] Length without Library
Function
#include<stdio.h>
int main()
{
char a[100];
int c=0;
printf("Enter the String : ");
gets(a);
while(a[c]!='\0')
{
c++;
}
printf("The Length is %d",c);
return 0;
}

© Kishor Roy
[Link] Reverse without Library
function
#include<stdio.h>
int main()
{
char a[100],b[100];
int l=0,i,j;
printf("Enter the string : ");
gets(a);
while(a[l]!='\0')
{
l++;
}
for(i=0,j=l-1;i<l,j>=0;i++,j--)
{
b[j]=a[i];
}
for(j=0;j<l;j++)
{
printf("%c",b[j]);
}
return 0;
}

© Kishor Roy
[Link] Palindrome
#include<stdio.h>
#include<string.h>
int main()
{
int a[100],b[100];
int i,j,n,c=0;
printf("Enter the string: ");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
{
printf("palindrome");
}
else
{
printf("Not Palindrome");
}
return 0;
}

© Kishor Roy
[Link] Copy without Library
Function
#include<stdio.h>
int main()
{
char a[100],b[100];
int n=0,i;
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<n;i++)
{
b[i]=a[i];
}
for(i=0;i<n;i++)
{
printf("%c",b[i]);
}
return 0;
}

© Kishor Roy
[Link] Palindrome without any
library function
#include<stdio.h>
int main()
{
char a[100],b[100];
int n=0,i,j,c=0;
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0,j=n-1;i<n,j>=0;i++,j--)
{
b[j]=a[i];
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
{
c++;
}
}
if(c==n)
{
printf("Palindrome");
}
else
{
printf("Not Palindrome");
}
return 0;
}

© Kishor Roy
53. String Sorting
#include<stdio.h>
int main()
{
char a[100],temp;
int n=0,i,j;
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%c",a[i]);
}
return 0;
}

© Kishor Roy
[Link] Checking
#include<stdio.h>
int main()
{
char a[100],b[100],temp;
int n=0,m=0,i,j,c=0;
gets(a);
gets(b);
while(a[n]!='\0')
{
n++;
}
while(b[m]!='\0')
{
m++;
}
if(n!=m)
{
printf("Not Anagram");
}
else
{
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<=i;j++)
{
if(b[j]>b[i])

© Kishor Roy
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
{
c++;
}
}
if(c==m)
{
printf("Anagram");
}
else
{
printf("Not Anagram");
}
}
return 0;
}

© Kishor Roy
[Link] Word from a string
#include<stdio.h>
int main()
{
char a[100];
int n=0,i,c=0;
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<n;i++)
{
if(a[i]==' ' && a[i+1]!=' ')
{
c++;
}
}
printf("Total words = %d",c+1);
return 0;
}

© Kishor Roy
[Link] Concatenation without
Library Function
#include<stdio.h>
int main()
{
char a[100],b[100];
int n=0,i;
gets(a);
gets(b);
while(a[n]!='\0')
{
n++;
}
for(i=0;b[i]!='\0';i++)
{
a[n]=b[i];
n++;
}
a[n]='\0';
puts(a);
return 0;
}

© Kishor Roy
[Link] of Occurring of an alphabet in a
string
#include<stdio.h>
int main()
{
char a[100],ch;
int n=0,i,l=0;
printf("Enter the string: ");
gets(a);
printf("Enter the character: ");
scanf("%c",&ch);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<n;i++)
{
if(a[i]==ch)
{
l++;
}
}
printf("%d",l);
return 0;
}

© Kishor Roy
[Link] Comparison without Library
Function
#include<stdio.h>
int main()
{
char a[100],b[100];
int n=0,m=0,i,c=0;
gets(a);
gets(b);
while(a[n]!='\0')
{
n++;
}
while(b[m]!='\0')
{
m++;
}
if(n!=m)
{
printf("String Not Matched");
}
else
{
for(i=0;i<n;i++)
{
if(a[i]==b[i])
{
c++;
}
}
if(n==c)
{
printf("String Matched");
}
else
{
printf("String Not Matched");
}
}
return 0;
}

© Kishor Roy
[Link] Determinant
#include<stdio.h>
int main()
{
int a[3][3],i,j,d;
printf("Enter The Matrix elements: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
d=(a[0][0]*(a[1][1]*a[2][2]-a[1][2]*a[2][1]))-
(a[0][1]*(a[1][2]*a[2][0]-
a[1][0]*a[2][2]))+(a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]));
printf("The Determinant is %d",d);
return 0;
}

© Kishor Roy
[Link] Summation and Subtraction
#include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],d[3][3],i,j;
printf("Enter the first matrix element: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the Second matrix element: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
}

printf("Enter the Summation matrix element: \n");


for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf ("%d\t",c[i][j]);
}
}

© Kishor Roy
printf("Enter the Subtraction matrix element: \n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf ("%d\t",d[i][j]);
}
}
return 0;
}

© Kishor Roy
[Link] Search
#include<stdio.h>
int main()
{
int a[100],n,m,first,last,middle,i;
printf("Enter the number of Array elements: ");
scanf("%d",&n);
printf("Enter the Array Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the Number to be searched: ");
scanf("%d",&m);
first=0;
last=n-1;
middle=(first+last)/2;
while(first<=last)
{
if(a[middle]<m)
{
first=middle+1;
}
else if(a[middle]>m)
{
last=middle-1;
}
else
{
printf("The element found in the place
%d",middle+1);
break;
}
middle=(first+last)/2;
}
if(first>last)
{
printf("The element not found");
}
return 0;
}

© Kishor Roy
[Link] All Space from a String
#include <string.h>
int main()
{
char s[1000];
int i,k=0;
printf("Enter the string : ");
gets(s);
for(i=0;s[i];i++)
{
s[i]=s[i+k];
if(s[i]==' '|| s[i]=='\t')
{
k++;
i--;
}
}
printf("string after removing all blank spaces:\n");
printf("%s",s);
return 0;
}

© Kishor Roy
[Link] & Uppercase Alphabet
#include<stdio.h>
int main()
{
char ch;
printf("Enter the character: ");
scanf("%c",&ch);
if(ch>='a' && ch<='z')
{
printf("lowercase");
}
else if(ch>='A' && ch<='Z')
{
printf("Uppercase");
}
else
{
printf("Not a character");
}
return 0;
}

© Kishor Roy
[Link], Consonant, Number and
Special Character Count from a
String
#include<stdio.h>
int main()
{
char ch[100],l=0,v=0,c=0,n=0,s=0,i;
printf("Enter the string: ");
gets(ch);
while(ch[l]!='\0')
{
l++;
}
for(i=0;i<l;i++)
{

if(ch[i]=='a'||ch[i]=='e'||ch[i]=='i'||ch[i]=='o'||ch[i]=='u'||c
h[i]=='A'||ch[i]=='E'||ch[i]=='I'||ch[i]=='O'||ch[i]=='U')
{
v++;
}
else
if(ch[i]=='1'||ch[i]=='2'||ch[i]=='3'||ch[i]=='4'||ch[i]=='5'||c
h[i]=='6'||ch[i]=='7'||ch[i]=='8'||ch[i]=='9'||ch[i]=='0')
{
n++;
}
else
if(ch[i]=='!'||ch[i]=='@'||ch[i]=='#'||ch[i]=='$'||ch[i]=='%'||c
h[i]=='^'||ch[i]=='&'||ch[i]=='*'||ch[i]=='+'||ch[i]=='-
'||ch[i]=='|')
{
s++;
}
else
{
c++;

© Kishor Roy
}
}
printf("No of Vowel %d \n",v);
printf("No of Consonant %d \n",c);
printf("No of Number %d \n",n);
printf("No of Special Character %d \n",s);
return 0;
}

© Kishor Roy
[Link] to Lowercase &
Lowercase to Uppercase
#include<stdio.h>
int main()
{
char ch;
printf("Enter a Character: ");
scanf("%c",&ch);
if(ch>='a' && ch<='z')
{
ch=ch-32;
}
else if(ch>='A' && ch<='Z')
{
ch=ch+32;
}
printf("The Converted Character is %c",ch);
return 0;
}

© Kishor Roy
[Link] Conversion in a String
#include<stdio.h>
int main()
{
char a[100];
int l=0,i;
printf("Enter the string: ");
gets(a);
while(a[l]!=0)
{
l++;
}
for(i=0;i<l;i++)
{
if((a[i]>='a' && a[i]<='z')|| (a[i]>='A' && a[i]<='Z'))
{
a[i]=CaseConvert(a[i]);
}
}
for(i=0;i<l;i++)
{
printf("%c",a[i]);
}
return 0;
}
void CaseConvert(char ch)
{
if(ch>='a' && ch<='z')
{
ch=ch-32;
}
else if(ch>='A' && ch<='Z')
{
ch=ch+32;
}
return ch;
}

© Kishor Roy
[Link] Conversion in a String Reverse
Order
#include<stdio.h>
int main()
{
char a[100],b[100];
int l=0,i,j;
printf("Enter the string: ");
gets(a);
while(a[l]!=0)
{
l++;
}
for(i=0;i<l;i++)
{
if((a[i]>='a' && a[i]<='z')|| (a[i]>='A' && a[i]<='Z'))
{
a[i]=CaseConvert(a[i]);
}
}
for(i=0,j=l-1;i<l,j>=0;i++,j--)
{
b[j]=a[i];
}
for(i=0;i<l;i++)
{
printf("%c",b[i]);
}
return 0;
}
void CaseConvert(char ch)
{
if(ch>='a' && ch<='z')
{
ch=ch-32;
}
else if(ch>='A' && ch<='Z')
{
ch=ch+32;
}
return ch;
}

© Kishor Roy
[Link] to number Conversion
#include<stdio.h>
#include<stdlib.h>
int main()
{
char a[100];
int d;
printf("Enter a string:");
gets(a);
d=atoi(a);
printf("%d",d);
return 0;
}

© Kishor Roy
[Link] Address Validation, Classification
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char a[100],b[100];
int l,i,j=0,c[4],d=0,ip[4],e=0;
printf("Enter The IP Address: ");
gets(a);
l=strlen(a);
for(i=0;i<l;i++)
{
if(a[i]!='.')
{
b[j++]=a[i];
}
if(a[i]=='.' || i==l-1)
{
b[j]='\0';
j=0;
c[d++]=atoi(b);
}
}
ip[0]=c[0];
ip[1]=c[1];
ip[2]=c[2];
ip[3]=c[3];

if((ip[0]>=0 && ip[0]<=255) && (ip[1]>=0 && ip[1]<=255) &&


(ip[2]>=0 && ip[2]<=255) && (ip[3]>=0 && ip[3]<=255))
{
printf("Valid IP Address.\n\n");
e++;
}
else
{
printf("Not Valid IP address");
}

© Kishor Roy
if(e==1)
{
if((ip[0]>=0 && ip[0]<=127))
{
printf("Class A IP Address");
}
else if((ip[0]>=128 && ip[0]<=191))
{
printf("Class B IP Address");
}
else if((ip[0]>=192 && ip[0]<=223))
{
printf("Class C IP Address");
}
else if((ip[0]>=224 && ip[0]<=239))
{
printf("Class D IP Address");
}
else
{
printf("Class E IP Address");
}
}
return 0;
}

© Kishor Roy
[Link] frequency in a string
#include<stdio.h>
#include<string.h>
int main()
{
char a[100];
int l,i,freq[26];
printf("Enter The String: ");
gets(a);
l=strlen(a);
for(i=0;i<26;i++)
{
freq[i]=0;
}
for(i=0;i<l;i++)
{
if(a[i]>='a' && a[i]<='z')
{
freq[a[i]-97]++;
}
else if(a[i]>='A' && a[i]<='Z')
{
freq[a[i]-65]++;
}
}
printf("The frequency of each alphabet are: \n");
for(i=0;i<26;i++)
{
if(freq[i]!=0)
{
printf("%c = %d times\n\n",i+97,freq[i]);
}
}
return 0;
}

© Kishor Roy
[Link] Search in a given String
#include<stdio.h>
#include<string.h>
int main()
{
char a[100],b[100];
int i,j,m=0,n=0,flag;
printf("Enter the String: ");
gets(a);
printf("Enter The SubString: ");
gets(b);
while(a[m]!='\0')
{
m++;
}
while(b[n]!='\0')
{
n++;
}
for(i=0;i<=(m-n);i++)
{
for(j=i;j<=(i+n);j++)
{
flag=1;
if(a[j]!=b[j-i])
{
flag=0;
break;
}
}
if(flag==1)
break;
}
if(flag==1)
{
printf("Found");
}
else
{
printf("Not Found");
}
return 0;

© Kishor Roy
[Link] Pointer-01
#include<stdio.h>
int main()
{
int x=10, *px;
px=&x;
printf("The Value of X= %d\n",x);
printf("The Address of X= %d\n",&x);
printf("The Address of Pointer= %d\n",&px);
printf("The Value of the Address to be point= %d\n",*px);
return 0;
}

© Kishor Roy
[Link] Pointer(Summation)
#include<stdio.h>
int main()
{
int x=10,y=20,sum,*p,*p1;
p=&x;
p1=&y;
sum=*p+*p1;
printf("Sum= %d",sum);
return 0;
}

© Kishor Roy
[Link] Pointer (Swapping Variable)
#include<stdio.h>
int main()
{
int x=10,y=20,temp;
int *p1,*p2;
p1=&x;
p2=&y;
temp=*p1;
*p1=*p2;
*p2=temp;
printf("x = %d\n",x);
printf("y = %d",y);
return 0;
}

© Kishor Roy
[Link] pointer (Swapping Using
Function)
#include<stdio.h>
int main()
{
int x=10,y=20;
Swapping(&x,&y);
printf("x = %d\n",x);
printf("y = %d",y);
return 0;
}
void Swapping(int *x, int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;

© Kishor Roy
[Link] pointer (Array ElementsSum)
#include<stdio.h>
int main()
{
int a[10], i,n,sum=0;
printf("Enter How many elements in the array : ");
scanf("%d",&n);
printf("Enter the elements ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+*(a+i);
}
printf(" Sum is =%d",sum);
return 0;
}

© Kishor Roy
[Link] Reverse Using Pointer
#include<stdio.h>
int main()
{
char a[100];
int i,l;
printf("Enter a String: ");
gets(a);
l=strlen(a);
for(i=l-1;i>=0;i--)
{
printf("%c",*(a+i));
}
return 0;
}

© Kishor Roy
[Link] Concatenation Using Pointer
#include<stdio.h>
int main()
{
char a[100],b[100];
printf("Enter The First String : ");
gets(a);
printf("Enter The Second String : ");
gets(b);
char *aa=a;
char *bb=b;
while(*aa)
{
aa++;
}
while(*bb)
{
*aa=*bb;
bb++;
aa++;
}
*aa='\0';
printf("%s",a);
return 0;
}

© Kishor Roy
[Link] sequence or not
Input:
6
1 2 1 1 3 4
Output: Not Mountain

Input:
6
1 2 1 3 2 4
Output: Mountain

Idea: To check if the sequence is mountain we have to make sure


that no neighbouring elements are the same.

#include<stdio.h>
int main()
{
int a[10],n,i,c=0;
printf("Enter how many elements: ");
scanf("%d",&n);
printf("Enter the Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]==a[i+1])
{
c++;
break;
}
}
if(c==1)
{
printf("Not Mountain Sequence");
}
else
{
printf("Mountain Sequence");
}
return 0;
}

© Kishor Roy

You might also like