C Lang Coding

Download as pdf or txt
Download as pdf or txt
You are on page 1of 147

Please practice these code(BASIC TO

Moderator)

1. Program to print "Hello World!!".

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

printf("Hello World!!");

getch();

@C_Codings

2. Program to assign values of two numbers and print their addition.

#include<stdio.h>

#include<conio.h>

void main()

int a,b,ans;
clrscr();

a=10;

b=20;

ans=a+b;

printf("Addition is : %d",ans);

getch();

@C_Codings

3. Program to accept values of two numbers and print their addition.

#include<stdio.h>

#include<conio.h>

void main()

int a,b,ans;

clrscr();

printf("Enter 1st number:");

scanf("%d",&a);

printf("Enter 2nd number:");

scanf("%d",&b);

ans=a+b;

printf("Addition is : %d",ans);

getch();

@C_Codings
4. Program to print simple interest.

#include<stdio.h>

#include<conio.h>

void main()

float interest, p, r, n;

clrscr();

printf("Enter value of P: ");

scanf("%f",&p);

printf("Enter value of R: ");

scanf("%f",&r);

printf("Enter value of N: ");

scanf("%f",&n);

interest=p*r*n/100f;

printed("Simple Interest : %f", interest);

getch();

@C_Codings

5. Program to accept value of radius and print area of a circle.

#include<stdio.h>

#include<conio.h>

void main()
{

float area,radius;

clrscr();

printf("Enter Radius:");

scanf("%f",&radius);

area=3.14*radius*radius;

printf("Area of the given radius is : %6.2f",area);

getch();

@C_Codings

6. Program to accept a number from user and print it’s square & cube.

#include<stdio.h>

#include<conio.h>

void main()

int n,sqre,cube;

clrscr();

printf("Enter Number: ");

scanf("%d",&n);

sqre=n*n;

cube=n*n*n;

printf("\nSquare: %d\nCube: %d",sqre,cube);

getch();

}
@C_Codings

7. Program to accept two values of a & b and swap their values.

#include<stdio.h>

#include<conio.h>

void main()

int a,b,temp;

clrscr();

printf("Enter 1st number: ");

scanf("%d",&a);

printf("Enter 2nd number: ");

scanf("%d",&b);

printf("\nBefore Swapping..\n A=%d, B=%d",a,b);

temp=a;

a=b;

b=temp;

printf("\nAfter Swapping..\n A=%d, B=%d",a,b);

getch();

@C_Codings

8. Program to accept two number and print largest among them.

#include<stdio.h>
#include<conio.h>

void main()

int a,b;

clrscr();

printf("Enter 1st number: ");

scanf("%d",&a);

printf("Enter 2nd number: ");

scanf("%d",&b);

if(a>b)

printf("Largest value is: %d",a);

else

printf("Largest value is: %d",b);

getch();

@C_Codings

9. Program to accept a number and check whether the number is Positive, Negative
or Zero.

#include<stdio.h>

#include<conio.h>

void main()

int n;

clrscr();

printf("Enter number: ");


scanf("%d",&n);

if(n>0)

printf("Number is positive");

else if(n<0)

printf("Number is negative");

else

printf("Number is Zero");

getch();

@C_Codings

10. Program to check whether the number is even or odd.

#include<stdio.h>

#include<conio.h>

void main()

int n;

clrscr();

printf("Enter number: );

scanf("%d",&n);

if(n%2==0)

printf("Number is even");

else

printf("Number is odd");

getch();
}

@C_Codings

11. Program to accept three numbers from user and print them in ascending and
decending order.

#include<stdio.h>

#include<conio.h>

void main()

int a,b,c;

clrscr();

printf("Enter numbers:");

scanf("%d%d%d",&a,&b,&c);

if((a>=b)&&(a>=c))

if(b>=c)

printf("\n Descending order:

%d %d %d",a,b,c);

printf("\n Ascending order :

%d %d %d",c,b,a);

else

printf("\n Descending order :

%d %d %d",a,c,b);
printf("\n Ascending order :

%d %d %d",b,c,a);

else if((b>=a)&&(b>=c))

if(a>=c)

printf("\n Descending order :

%d %d %d",b,a,c);

getch();

@C_Codings

12. Program to find the roots of a quadratic equation.

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

float a, b, c, determinant, real;

double r1, r2, imag;

printf("Enter coefficients a, b and c: ");


scanf("%f%f%f", &a, &b, &c);

determinant = b * b - 4 * a * c;

if (determinant > 0)

r1 = (-b + sqrt(determinant)) / (2 * a);

r2 = (-b - sqrt(determinant)) / (2 * a);

printf("Roots are: %.2f and %.2f", r1, r2);

else if (determinant == 0)

r1 = r2 = -b / (2 * a);

printf("Roots are: %.2f and %.2f", r1, r2)

else

real = -b / (2 * a);

imag = sqrt(-determinant) / (2 * a);

printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);

getch();

@C_Codings

13. Program to accept rollnumber and marks of three subjects from user and print
total marks, average and grade.
#include<stdio.h>

#include<conio.h>

void main()

int RollNum, m1, m2, m3, total;

float avg;

clrscr();

printf("Enter Roll Number : ");

scanf("%d",&RollNum);

printf("Enter marks for three subjects : ");

scanf("%d%d%d", &m1, &m2, &m3);

total=m1+m2+m3;

avg=total/3.0;

printf("\nTotal is : %d", total);

printf("\nAverage is : %5.2f", avg);

if(avg>80)

printf("\nGrade : A");

else if((avg>60)&&(avg<=80))

printf("\nGrade : B");

else if((avg>40)&&(avg<=60))

printf("\nGrade : C");

else if((avg>=33)&&(avg<=40)

printf("\nGrade : D");
else

printf("\nGrade : Fail");

getch();

@C_Codings

14. Program to print numbers from 1 to n using while loop.

#include<stdio.h>

#include<conio.h>

void main()

int i=1, n;

clrscr();

printf("Enter n : ");

scanf("%d", &n);

while(i<=n)

printf("%d\t",i);

i++;

getch();

@C_Codings
15. Program to print numbers from n to 1 using Do While loop.

#include<stdio.h>

#include<conio.h>

void main()

int i=1, n;

clrscr();

printf("Enter n : ");

scanf("%d", &n);

i=n;

do

printf("%d\t",i);

i--;

}while(i>=1);

getch();

@C_Codings

16. Program to print first n even numbers.

#include<stdio.h>

#include<conio.h>

void main()

{
int i=2, n;

//to print odd numbers set variable i=1

clrscr();

printf("Enter n : ");

scanf("%d", &n);

while(i<=n)

printf("%d\t",i);

i=i+2;

getch();

@C_Codings

17. Program to accept a number and print that number in reverse order.

Ex:- 1024

Output:- 4201

#include<stdio.h>

#include<conio.h>

void main()

int reminder, n;

clrscr();

printf("Enter n : ");

scanf("%d", &n);
while(n>0)

reminder=n%10;

printf("%d", reminder);

n=n/10;

getch();

@C_Codings

18. Program to accept a number and print sum of it’s digits.

#include<stdio.h>

#include<conio.h>

void main()

int reminder, sum=0, n;

clrscr();

printf("Enter n : ");

scanf("%d", &n);

while(n>0)

reminder=n%10;

sum=sum+reminder;

n=n/10;

}
printf("Sum of digits : %d",sum);

getch();

@C_Codings

19. Program to take a number from user and check whether it is Armstrong number
or not.

#include<stdio.h>

#include<conio.h>

void main()

int i=2, temp, rem, sum=0, n;

clrscr();

printf("Enter n : ");

scanf("%d", &n);

temp = n;

while(n>0)

rem = n%10;

sum = sum+(rem*rem*rem);

n = n/10;

if(temp==sum)

printf("Entered number is an Armstrong Number");


else

printf("Entered number is not an Armstrong Number");

getch();

@C_Codings

20. Program to take number from user and print table of that number.

#include<stdio.h>

#include<conio.h>

void main()

int i, n;

clrscr();

printf("Enter number : ");

scanf("%d", &n);

for(i=1; i<=10; i++)

printf("%d × %d = %d\n", n, i, n*i);

getch();

@C_Codings

21. Pattern 1

••
•••

••••

•••••

#include<stdio.h>

#include<conio.h>

void main()

int i, j, n;

clrscr();

printf("Enter number : ");

scanf("%d", &n);

for(i=1; i<=n; i++)

for(j=1; j<=i; j++)

printf("• ");

printf("\n");

getch();

@C_Codings

22. Pattern 2
•••••

••••

•••

••

#include<stdio.h>

#include<conio.h>

void main()

int i, j, n;

clrscr();

printf("Enter number : ");

scanf("%d", &n);

for(i=n; i>=1; i--)

for(j=1;j<=i;j++)

printf("• ");

printf("\n");

getch();

@C_Codings
23. Pattern 3

••

•••

••••

•••••

#include<stdio.h>

#include<conio.h>

void main()

char ch = '*';

int n;

int i, j, no_of_spaces = 0, spaceCount;

printf("Enter number : ");

scanf("%d", &n);

printf("\n");

no_of_spaces = n - 1;

for (i = 1; i <= n; i++)

for (spaceCount = no_of_spaces; spaceCount >= 1; spaceCount--)

printf(" ");

}
for (j = 1; j <= i; j++)

printf("%2c", ch);

printf("\n");

no_of_spaces--;

getch();

@C_Codings

24. Pattern 4

12

123

1234

12345

#include<stdio.h>

#include<conio.h>

void main()

int n, i, j;
printf("Enter number : ");

scanf("%d", &n);

for (i = 1; i <= n; i++)

for (j = 1; j <= i; j++)

printf("%d ", j);

printf("\n");

getch();

@C_Codings

25. Pattern 5

21

321

4321

54321

#include <stdio.h>

#include<conio.h>

void main()
{

int i, j, n;

printf("Enter number : ");

scanf("%d", &n);

printf("\n");

for (i = 1; i <= n; i++)

for (j = i; j >= 1; j--)

printf("%d ", j);

printf("\n");

getch();

@C_Codings

26. Pattern 6

BC

DEF

GHIJ

KLMNO
#include<stdio.h>

#include<conio.h>

void main()

int i, j, n;

clrscr();

printf("Enter number : ");

scanf("%d", &n);

int c = 'A';

for (i = 0; i < n; i++)

for (j = 0; j <= i; j++)

printf("%c", c);

c = c + 1;

printf("\n");

getch();

@C_Codings

27. Pattern 7
1

121

12321

1234321

123454321

#include<stdio.h>

#include<conio.h>

void main()

int i, j, n;

clrscr();

printf("Enter number : ");

scanf("%d", &n);

for (i = 1; i <= n; i++)

for (j = 1; j <= i; j++)

printf("%d", j);

for (j = i - 1; j >= 1; j--)

printf("%d", j);

printf("\n");

getch();

}
@C_Codings

28. Pattern 8 (Binary Pattern)

01

101

0101

10101

#include<stdio.h>

#include<conio.h>

void main()

int i, j;

int count = 1;

clrscr();

for (i = 1; i <= 5; i++)

for (j = 1; j <= i; j++)

printf("%d", count++ % 2);

if (j == i && i != 5)

printf("\n");

}
if (i % 2 == 0)

count = 1;

else

count = 0;

getch();

@C_Codings

29. Pattern 9

12345

1234

123

12

#include<stdio.h>

#include<conio.h>

void main()

int i, j, k, l, n;

clrscr();

printf("Enter number : ");


scanf("%d", &n);

printf("\n");

for (i = n; i >= 1; i--)

for (j = 1; j <= i; j++)

printf("%d", j);

printf("\n");

getch();

@C_Codings

30. Pattern 10

21

321

4321

54321

#include<stdio.h>

#include<conio.h>

void main()

{
int i, j, k;

clrscr();

for (i = 1; i <= 5; i++)

for (j = 5; j >= 1; j--)

if (j <= i)

printf("%d", j);

else

printf(" ");

printf("\n");

getch();

@C_Codings

31. Pattern 11

1 1

12 21
123 321

12344321

#include<stdio.h>

#include<conio.h>

void main()

int i, j, k;

clrscr();

for (i = 1; i <= 5; i++)

for (j = 1; j <= 5; j++)

if (j <= i)

printf("%d ", j);

else

printf(" ");

for (j = 5; j >= 1; j--)

if (j <= i)
{

printf("%d ", j);

else

printf(" ");

printf("\n");

getch();

@C_Codings

32. Floyd's triangle

23

456

7 8 9 10

11 12 13 14

#include<stdio.h>

#include<conio.h>

void main()

{
int n, i, c, a = 1;

clrscr();

printf("Enter the number of rows : ");

scanf("%d", &n);

for (i = 1; i <= n; i++) {

for (c = 1; c <= i; c++) {

printf("%d ", a);

a++;

printf("\n");

getch();

@C_Codings

33. Pyramid

**

***
****

*****

#include<stdio.h>

#include<conio.h>

void main()

int row, c, n, temp;

clrscr();

printf("Enter the number of rows : ");

scanf("%d", &n);

temp = n;

for (row = 1; row <= n; row++)

for (c = 1; c < temp; c++)

printf(" ");

temp--;

for (c = 1; c <= 2 * row - 1; c++)

printf("*");

printf("\n");
}

getch();

@C_Codings

34. Pyramid 2

*A*

*A*A*

*A*A*A*

*A*A*A*A*

#include<stdio.h>

#include<conio.h>

void main()

int n, c, k, space, count = 1;

clrscr();

printf("Enter the number of rows : ");

scanf("%d", &n);

space = n;

for (c = 1; c <= n; c++)


{

for (k = 1; k < space; k++)

printf(" ");

for (k = 1; k <= c; k++)

printf("*");

if (c > 1 && count < c)

printf("A");

count++;

printf("\n");

space--;

count = 1;

getch();

@C_Codings

35. Number Pyramid


1

232

34543

4567654

567898765

#include<stdio.h>

#include<conio.h>

void main()

int n, c, d, num = 1, space;

clrscr();

printf("Enter the number of rows : ");

scanf("%d", &n);

space = n - 1;

for (d = 1; d <= n; d++)

num = d;

for (c = 1; c <= space; c++)

printf(" ");

space--;
for (c = 1; c <= d; c++)

printf("%d", num);

num++;

num--;

num--;

for (c = 1; c < d; c++)

printf("%d", num);

num--;

printf("\n");

getch();

@C_Codings

36. Pascal triangle

11

121
1331

14641

#include<stdio.h>

#include<conio.h>

long fact(int);

void main()

int line, i, j;

clrscr();

printf("Enter the number : ");

scanf("%d", &line);

for (i = 0; i < line; i++)

for (j = 0; j < line - i - 1; j++)

printf(" ");

for (j = 0; j <= i; j++)

printf("%ld ", fact(i) / (fact(j) * fact(i - j)));


}

printf("\n");

getch();

long fact(int num)

long f = 1;

int i = 1;

while (i <= num)

f = f * i;

i++;

return f;

@C_Codings

37. Pascal triangle without using function

11

121
1331

14641

#include<stdio.h>

#include<conio.h>

void main()

int x, y, n, a, z, s;

clrscr();

printf("Enter the number : ");

scanf("%d", &n);

s = n;

for (x = 0; x <= n; x++)

a = 1;

for (z = s; z >= 0; z--)

printf(" ");

s--;

for (y = 0; y <= x; y++)

printf("%d ", a);

a = (a * (x - y) / (y + 1));
}

printf("\n");

getch();

@C_Codings

38. Pascal triangle 2

121

12321

1234321

123454321

#include<stdio.h>

#include<conio.h>

void main()

int n, c, k, number = 1, space = n;

clrscr();

printf("Enter number of rows : ");

scanf("%d", &n);

printf("\n");

space = n;
for (c = 1; c <= n; c++)

for (k = space; k > 1; k--)

printf(" ");

space--;

for (k = 1; k <= 2 * c - 1; k++)

if (k <= c)

printf("%d", number);

if (k < c)

number++;

else

number--;

printf("%d", number);

number = 1;

printf("\n");

}
getch();

@C_Codings

39. Number Alphabet Pattern

AB

234

CDEF

56789

GHIJKL

#include<stdio.h>

#include<conio.h>

void main()

int num, r, c;

int i = 1;

char ch = 'A';

clrscr();

printf("Enter the number of rows : ");

scanf("%d", &num);

printf("\n");
for (r = 1; r <= num; r++)

for (c = 1; c <= r; c++)

if (r % 2 == 0)

printf(" %c", ch++);

else

printf(" %d", i++);

printf("\n");

getch();

@C_Codings

40. Number Diamond Pattern

123

12345

1234567

123456789
1234567

12345

123

#include<stdio.h>

#include<conio.h>

void main()

int i, j, k;

clrscr();

for(i=1;i<=5;i++)

for(j=i;j<5;j++)

printf(" ");

for(k=1;k<(i*2);k++)

printf("%d",k);

printf("\n");

for(i=4;i>=1;i--)

{
for(j=5;j>i;j--)

printf(" ");

for(k=1;k<(i*2);k++)

printf("%d",k);

printf("\n");

getch();

@C_Codings

41. Diamond of Numbers

222

33333

4444444

555555555

4444444

33333

222

1
#include<stdio.h>

#include<conio.h>

void main()

int i, j, k;

clrscr();

for (i = 1; i <= 5; i++)

for (j = i; j < 5; j++)

printf(" ");

for (k = 1; k < (i * 2); k++)

printf("%d", i);

printf("\n");

for (i = 4; i >= 1; i--)

for (j = 5; j > i; j--)

printf(" ");
}

for (k = 1; k < (i * 2); k++)

printf("%d", i);

printf("\n");

getch();

@C_Codings

42. Diamond Pattern

•••

•••••

•••

#include<stdio.h>

#include<conio.h>

void main()

int n, c, k, space = 1;
clrscr();

printf("Enter number of rows : ");

scanf("%d", &n);

space = n - 1;

for (k = 1; k <= n; k++)

for (c = 1; c <= space; c++)

printf(" ");

space--;

for (c = 1; c <= 2 * k - 1; c++)

printf("•");

printf("\n");

space = 1;

for (k = 1; k <= n - 1; k++)

{
for (c = 1; c <= space; c++)

printf(" ");

space++;

for (c = 1; c <= 2 * (n - k) - 1; c++)

printf("•");

printf("\n");

getch();

@C_Codings

43. Diamond star outline

* *

* *

* *

* *

* *

#include<stdio.h>
#include<conio.h>

void main()

int i, j;

clrscr();

for (i = 1; i <= 5; i++)

for (j = 5; j > i; j--)

printf(" ");

printf("*");

for (j = 1; j < (i - 1) * 2; j++)

printf(" ");

if (i == 1)

printf("\n");

}
else

{ printf("*\n"); }

for (i = 4; i >= 1; i--)

for (j = 5; j > i; j--)

printf(" ");

printf("*");

for (j = 1; j < (i - 1) * 2; j++)

printf(" ");

if (i == 1)

printf("\n");

else

{ printf("*\n"); }

}
getch();

@C_Codings

44. Hollow Diamond

**** ****

*** ***

** **

* *

** **

*** ***

**** ****

#include<stdio.h>

#include<conio.h>

void main()

int i, j, k;

clrscr();

for (i = 1; i <= 5; i++)

for (j = 1; j <= 6 - i; j++)

{
printf("*");

for (k = 1; k < i; k++)

printf(" ");

for (j = 1; j <= 6 - i; j++)

printf("*");

printf("\n");

for (i = 2; i <= 5; i++)

for (j = 1; j <= i; j++)

printf("*");

for (k = 1; k <= 5 - i; k++)

printf(" ");
}

for (j = 1; j <= i; j++)

printf("*");

printf("\n");

getch();

@C_Codings

45. Hollow Square

*****

* *

* *

* *

*****

#include<stdio.h>

#include<conio.h>

void main()
{

int i, j, n;

clrscr();

printf("Enter value of n : ");

scanf("%d", &n);

printf("\n");

for (i = 1; i <= n; i++)

for (j = 1; j <= n; j++)

if (i != 1 && i != n && j != 1 && j != n)

printf(" ");

else

printf("*");

printf("\n");

getch();

@C_Codings
46. Hourglass Pattern

*********

*******

*****

***

***

*****

*******

*********

#include<stdio.h>

#include<conio.h>

void main()

int num, n, r, c, sp;

clrscr();

printf("Enter number of rows: ");

scanf("%d", &num);

printf("\n");

n = num;
for (r = 1; r <= num; r++)

for (sp = 1; sp <= r; sp++)

printf(" ");

for (c = 1; c <= n; c++)

printf("*");

for (c = num - r; c >= 1; c--)

printf("*");

n--;

printf("\n");

for (r = 2; r <= num; r++)

for (sp = num - r + 1; sp >= 1; sp--)

printf(" ");

for (c = 1; c <= r; c++)

printf("*");

for (c = r - 1; c >= 1; c--)

printf("*");
printf("\n");

getch();

@C_Codings

47. Nested Star-Hash Pyramid

#####*#####

####*#*####

###*###*###

##*#####*##

#*#######*#

*#########*

#include<stdio.h>

#include<conio.h>

void main()

int n = 5, r, c;

clrscr();

for (r = 1; r <= 6; r++, n--)

// first pyramid
for (c = 1; c <= n; c++)

printf(" #");

// second pyramid

for (c = 1; c <= r; c++)

if (c == 1)

printf(" *");

else

printf(" #");

// third pyramid

for (c = r; c > 1; c--)

if (c == 2)

printf(" *");

else
{

printf(" #");

// fourth pyramid

for (c = n; c >= 1; c--)

printf(" #");

printf("\n");

getch();

48. Reverse star pyramid

*********

*******

*****

***

#include<stdio.h>

#include<conio.h>
void main()

int i, j, k;

clrscr();

for (i = 5; i >= 1; i--)

for (j = 5; j > i; j--)

printf(" ");

for (k = 1; k < (i * 2); k++)

printf("* ");

printf("\n");

getch();

@C_Codings

49. Rhombus Pattern


11

2 2

3 3

4 4

5 5

4 4

3 3

2 2

11

#include<stdio.h>

#include<conio.h>

void main()

int num, r, c, sp, n;

clrscr();

printf("Enter the number : ");

scanf("%d", &num);

for (r = 1; r <= num; r++)

for (sp = num - r; sp >= 1; sp--)

printf(" ");
printf("%d", r);

for (sp = r * 2; sp > 1; sp--)

printf(" ");

printf("%d", r);

printf("\n");

for (r = 1, n = num - 1; r < num; r++, n--)

for (sp = r; sp >= 1; sp--)

printf(" ");

printf("%d", n);

for (sp = n * 2; sp > 1; sp--)

printf(" ");

printf("%d", n);

printf("\n");

getch();

@C_Codings
50. Square kite pattern

2 2

3 3

4 4

3 3

2 2

#include<stdio.h>

#include<conio.h>

void main()

int i, j, k;

clrscr();

for (i = 1; i <= 4; i++)

for (j = 4; j >= (i - 1) * 2 - 1; j--)

printf(" ");

printf("%d", i);

for (j = 2; j <= (i - 1) * 4; j++)

printf(" ");

if (i > 1)
printf("%d", i);

printf("\n");

for (i = 3; i >= 1; i--)

for (j = 4; j >= (i - 1) * 2 - 1; j--)

printf(" ");

printf("%d", i);

for (j = 2; j <= (i - 1) * 4; j++)

printf(" ");

if (i > 1)

printf("%d", i);

printf("\n");

getch();

@C_Codings

51. Triangle with only border

#include<stdio.h>

#include<conio.h>

* *

* *
* *

* *

* *

******

void drawTriangle(char border, char filler, int length)

int start = 2;

int base = 4;

int i, sp, j, b;

for (i = start; i <= length; i++)

for (sp = 0; sp <= length - i; sp++)

printf(" ");

if (i > start)

printf("%c ", border);

if (i > start)

for (b = base; b <= i; b++)

printf("%c ", filler);

}
}

printf("%c \n", border);

for (j = base; j < length + base; j++)

printf("%c ", border);

printf("\n");

void main()

int length = 6;

clrscr();

drawTriangle('*', ' ', length);

getch();

@C_Codings

52. Program to accept number and print it's factorial.

#include<stdio.h>

#include<conio.h>

void main()

{
int i, fact=1, n;

clrscr();

printf("Enter number : ");

scanf("%d", &n);

for(i=1; i<=n; i++)

fact = fact*i;

printf("Factorial is: %d", fact);

getch();

@C_Codings

53. Program to accept number and print if it is prime number or not.

#include<stdio.h>

#include<conio.h>

void main()

int i, n;

clrscr();

printf("Enter number : ");

scanf("%d", &n);

for(i=2; i<=n/2; i++)


{

if(n%i==0)

printf("Number is not Prime");

getch();

break;

printf("Number is Prime");

getch();

@C_Codings

54. Program to print 'n' prime numbers.

#include<stdio.h>

#include<conio.h>

#include<process.h>

void main()

int i, j, flag=1, n;

clrscr();

printf("Enter number : ");

scanf("%d", &n);

for(i=2; i<=n; i++)


{

flag=1;

for(j=2; j<=i/2; j++)

if(i%j==0)

flag=0;

break;

if(flag==1)

printf("%d\n", i);

getch();

@C_Codings

55. Program to accept a number and print Fibonacci sequence.

#include<stdio.h>

#include<conio.h>

void main()

int pre=1, cur=1, temp, i, n;

//pre means previous number

//cur means current number


clrscr();

printf("Enter number : ");

scanf("%d", &n);

printf("%d\t%d", pre, cur);

for(i=3; i<=n; i++)

temp = cur;

cur = pre + cur;

pre = temp;

printf("\t%d", cur);

getch();

@C_Codings

56. Add 'n' numbers.

#include<stdio.h>

#include<conio.h>

void main()

int n, sum=0, i, value;

clrscr();

printf("Enter total numbers you want to add : ");


scanf("%d", &n);

for (i=1; i<=n; i++)

printf("Enter number %d : ", i);

scanf("%d", &value);

sum = sum + value;

printf("Sum of entered numbers : %d", sum);

getch();

@C_Codings

57. Add 'n' numbers using array.

#include<stdio.h>

#include<conio.h>

void main()

int n, sum = 0, i, array[100];

clrscr();

printf("Enter total numbers you want to add : ");

scanf("%d", &n);

for (i = 1; i <= n; i++)


{

printf("Enter number %d : ", i);

scanf("%d", &array[i]);

sum = sum + array[i];

printf("Sum : %d\n", sum);

getch();

@C_Codings

58. Program to accept a number and add the digits of that number.

#include<stdio.h>

#include<conio.h>

void main()

int n, sum = 0, remainder;

clrscr();

printf("Enter the number : ");

scanf("%d", &n);

while (n != 0)

remainder = n % 10;

sum = sum + remainder;


n = n / 10;

printf("Sum of digits of entered number : %d", sum);

getch();

@C_Codings

59. Program to accept a number and add the digits of that number using recursion.

#include<stdio.h>

#include<conio.h>

int add_digits(int);

void main()

int n, result;

clrscr();

printf("Enter a number : ");

scanf("%d", &n);

result = add_digits(n);

printf("Sum : %d", result);

getch();
}

int add_digits(int n)

static int sum = 0;

if (n == 0)

return 0;

sum = n % 10 + add_digits(n / 10);

return sum;

@C_Codings

60. Average of numbers.

#include<stdio.h>

#include<conio.h>

void main()

int n, i;

float sum=0, x, avg;

clrscr();

printf("Enter total Numbers : ");

scanf("%d", &n);

for (i = 1; i <= n; i++)


{

printf("\nNumber %d : ", i );

scanf("%f", &x);

sum += x;

avg = sum / n;

printf("\nThe Average is : %0.2f", avg);

getch();

@C_Codings

61. Program to calculate Square of 'n' numbers.

#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();

@C_Codings

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();

@C_Codings
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);


}

@C_Codings

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();

@C_Codings

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();

}
@C_Codings

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();

@C_Codings
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();

@C_Codings

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();

@C_Codings

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();

@C_Codings

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();

@C_Codings

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();

@C_Codings

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();

@C_Codings
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();

@C_Codings

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();

@C_Codings
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();

@C_Codings

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);

@C_Codings
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();

@C_Codings

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();

@C_Codings

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();

@C_Codings

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();

@C_Codings
81. Program to add two numbers using pointer.

#include<stdio.h>

#include<conio.h>

void main()

int first, second, *p, *q, sum;

clrscr();

printf("Enter two integers : \n");

scanf("%d %d", &first, &second);

p = &first;

q = &second;

sum = *p + *q;

printf("\nSum of entered numbers : %d", sum);

getch();

@C_Codings

82. Program to add first and last digit of a number.

#include<stdio.h>

#include<conio.h>

void main()
{

int input, firstNum, lastNum;

clrscr();

printf("Enter number : ");

scanf("%d", &input);

lastNum = input % 10;

firstNum = input;

while (firstNum >= 10)

firstNum /= 10;

printf("\nAddition of first and last number : %d + %d = %d",

firstNum, lastNum, firstNum + lastNum);

getch();

@C_Codings

83. Program to find area of triangle using Heron's formula.

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()
{

double a, b, c, s, area;

clrscr();

printf("\nEnter the sides of triangle : \n");

scanf("%lf%lf%lf", &a, &b, &c);

s = (a + b + c) / 2;

area = sqrt(s * (s - a) * (s - b) * (s - c));

printf("\nArea of triangle using Heron's Formula : %.2lf", area);

getch();

@C_Codings

84. Program to convert Binary to Decimal.

#include<stdio.h>

#include<math.h>

#include<conio.h>

int binary_decimal(int n);

void main()

int n;

char c;
clrscr();

printf("Enter Binary number : ");

scanf("%d", &n);

printf("%d in binary = %d in decimal", n, binary_decimal(n));

getch();

//Function to convert binary to decimal.

int binary_decimal(int n)

int decimal = 0, i = 0, rem;

while (n != 0)

rem = n % 10;

n /= 10;

decimal += rem * pow(2, i);

++i;

return decimal;

@C_Codings

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

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

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

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

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

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

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
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

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

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

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

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

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
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

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

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

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

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

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

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

105. Program to remove white space in string.

#include<string.h>

#include<stdio.h>

#include<conio.h>
void main()

int i = 0, j = 0, len;

char buff[50];

clrscr();

printf("Enter String with white space : ");

gets(buff);

len = (int) strlen(buff);

while (i != len)

if (buff[i] != ' ')

buff[j++] = buff[i];

i++;

buff[j] = 0;

printf("\nYour String is : %s.", buff);

getch();

@C_Codings

106. Program to reverse the string.


#include<stdio.h>

#include<conio.h>

#include<string.h>

char *strrev(char *str)

char *p1, *p2;

if (!str || !*str)

return str;

for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)

*p1 ^= *p2;

*p2 ^= *p1;

*p1 ^= *p2;

return str;

void main()

char arr[100];

clrscr();

printf("\nEnter a string to reverse : ");

gets(arr);
strrev(arr);

printf("\nReverse of entered string is : %s", arr);

getch();

@C_Codings

107. Program to reverse the string using pointer.

#include<stdio.h>

#include<conio.h>

void main()

char str_array[10000], *ptr;

int i, len;

clrscr();

printf("Enter a string : ");

gets(str_array);

ptr = str_array;

for (i = 0; i < 10000; i++)

{
if (*ptr == '\0')

break;

ptr++;

len = i;

ptr--;

printf("\nReversed String is : ");

for (i = len; i > 0; i--)

printf("%c", *ptr--);

getch();

@C_Codings

108. Program to sort the strings.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int i, j, n;

char str[20][20], temp[20];


clrscr();

printf("Enter the number of string to be sorted : ");

scanf("%d", &n);

for (i = 0; i <= n; i++)

gets(str[i]);

for (i = 0; i <= n; i++)

for (j = i + 1; j <= n; j++)

if (strcmp(str[i], str[j]) > 0)

strcpy(temp, str[i]);

strcpy(str[i], str[j]);

strcpy(str[j], temp);

printf("\nSorted string :");

for (i = 0; i <= n; i++)

puts(str[i]);

getch();

}
@C_Codings

109. Program to swap two strings.

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

void main()

char str1[100], str2[100], *temp;

clrscr();

printf("Enter first string : ");

gets(str1);

printf("Enter second string : ");

gets(str2);

printf("\nBefore Swapping : \n");

printf("First string : %s\n", str1);

printf("Second string : %s\n", str2);

temp = (char *) malloc(100);


strcpy(temp, str1);

strcpy(str1, str2);

strcpy(str2, temp);

printf("\nAfter Swapping : \n");

printf("First string : %s\n", str1);

printf("Second string : %s\n", str2);

getch();

@C_Codings

110. Program to add two matrix.

#include<stdio.h>

#include<conio.h>

void main()

int m, n, c, d, first[10][10], second[10][10], sum[10][10];

clrscr();

printf("Enter the number of rows and columns of matrix :");

scanf("%d%d", &m, &n);

printf("\nEnter the elements of first matrix : \n");

for (c = 0; c < m; c++)


{

for (d = 0; d < n; d++)

scanf("%d", &first[c][d]);

printf("\nEnter the elements of second matrix : \n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

scanf("%d", &second[c][d]);

printf("\nSum of entered matrices : \n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

sum[c][d] = first[c][d] + second[c][d];

printf("%d\t", sum[c][d]);

}
printf("\n");

getch();

@C_Codings

111. Program to arrange array numbers in ascending order.

#include<stdio.h>

#include<conio.h>

void main()

int i, j, a, n, number[30];

clrscr();

printf("Enter total numbers : ");

scanf("%d", &n);

printf("\n Enter the numbers :\n");

for (i = 0; i < n; ++i)

scanf("%d", &number[i]);

for (i = 0; i < n; ++i)


{

for (j = i + 1; j < n; ++j)

if (number[i] > number[j])

a = number[i];

number[i] = number[j];

number[j] = a;

printf("The numbers arranged in ascending order are :\n");

for (i = 0; i < n; ++i)

printf("%d\n", number[i]);

getch();

@C_Codings

112. Program to check whether the matrix is sparse matrix or not.

#include<stdio.h>

#include<conio.h>
void main()

static int array[10][10];

int i, j, m, n;

int counter = 0;

clrscr();

printf("Enter the order of the matix : ");

scanf("%d %d", &m, &n);

printf("\nEnter the co-efficients of the matix :\n");

for (i = 0; i < m; ++i)

for (j = 0; j < n; ++j)

scanf("%d", &array[i][j]);

if (array[i][j] == 0)

++counter;

if (counter > ((m * n) / 2))

printf("The given matrix is sparse matrix \n");

}
else

printf("The given matrix is not a sparse matrix \n");

printf("There are %d number of zeros", counter);

getch();

@C_Codings

113. Program to delete an element from array.

#include<stdio.h>

#include<conio.h>

void main()

int array[100], position, c, n;

clrscr();

printf("Enter total number of elements in array : ");

scanf("%d", &n);

printf("\nEnter element %d : ", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("\nEnter the location from where you wish to delete element : ");

scanf("%d", &position);

if (position >= n + 1)

printf("\nDeletion not possible.");

else

for (c = position - 1; c < n - 1; c++)

array[c] = array[c + 1];

printf("\nResultant array is :");

for (c = 0; c < n - 1; c++)

printf("\n%d", array[c]);

getch();

@C_Codings

114. Program to delete given number from array.

#include<stdio.h>

#include<conio.h>
void main()

int array[10];

int i, n, pos, element, found = 0;

clrscr();

printf("Enter total number of elements : ");

scanf("%d", &n);

printf("\nEnter the elements : \n");

for (i = 0; i < n; i++)

scanf("%d", &array[i]);

printf("Input array elements are\n");

for (i = 0; i < n; i++)

printf("\n%d", array[i]);

printf("\nEnter the element to be deleted : ");

scanf("%d", &element);
for (i = 0; i < n; i++)

if (array[i] == element)

found = 1;

pos = i;

break;

if (found == 1)

for (i = pos; i < n - 1; i++)

array[i] = array[i + 1];

printf("\nResultant array elements are : ");

for (i = 0; i < n - 1; i++)

printf("\n%d", array[i]);

else

{
printf("\nElement %d is not found in the array", element);

getch();

@C_Codings

You might also like