0% found this document useful (0 votes)
406 views109 pages

C Programming Exercises

The document contains 16 C programming examples covering topics such as: 1. Adding two numbers with and without a third variable. 2. Subtracting one number from another with and without a third variable. 3. Calculating the average of four numbers. 4. Adding two numbers without the + operator. 5. Calculating the area of a circle given the radius. 6. Calculating the area of a rectangle given length and breadth. 7. Converting Celsius to Fahrenheit.

Uploaded by

sam_003
Copyright
© Attribution Non-Commercial (BY-NC)
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)
406 views109 pages

C Programming Exercises

The document contains 16 C programming examples covering topics such as: 1. Adding two numbers with and without a third variable. 2. Subtracting one number from another with and without a third variable. 3. Calculating the average of four numbers. 4. Adding two numbers without the + operator. 5. Calculating the area of a circle given the radius. 6. Calculating the area of a rectangle given length and breadth. 7. Converting Celsius to Fahrenheit.

Uploaded by

sam_003
Copyright
© Attribution Non-Commercial (BY-NC)
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

'C' Programs

1. Enter two numbers, and Add them. Show Addition with or without
using third variable.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int a,b,c;
printf ("Enter first number :");
scanf ("%d",&a);
printf ("Enter second number :");
scanf ("%d",&b);
c = a+b; /* With Using Third Variable */

// a = a+b; /* Without Using Third Variable */

printf ("\n\n\n\t\t\tAddition is %d",c);


getch ( );
}

2. Enter two numbers, and Subtract second number from first. Show
result with or without using third variable.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int a,b,c;
printf ("Enter first number :");
scanf ("%d",&a);
printf ("Enter second number :");
scanf ("%d",&b);
c = a-b; /* With Using Third Variable */

// a = a-b; /* Without Using Third Variable */

printf ("\n\n\n\t\t\tSubtraction Result is %d",c);


getch ( );
}

1
Madhukar E
'C' Programs

3. Enter Four numbers and find out their Average.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int a,b,c,d;
float avg;
printf ("Enter first number :");
scanf ("%d",&a);
printf ("Enter second number :");
scanf ("%d",&b);
printf ("Enter Third number :");
scanf ("%d",&c);
printf ("Enter Fourth number :");
scanf ("%d",&d);
avg = (float) (a + b + c + d)/ 4; /* Type Casting is done here, because this
operation
will produce a floating value. */

printf ("\n\n\n\t\t\tAddition is %.2f",avg); /* It will show the result in,


two digits after the decimal. */
getch ( );
}

4. Enter two numbers, and Add them without using '+' operator.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int a,b;
printf ("Enter first number :");
scanf ("%d",&a);
printf ("Enter second number :");
scanf ("%d",&b);
a = a -(-b);
printf ("\n\n\n\t\t\tAddition is %d",a);
getch ( );
}
2
Madhukar E
'C' Programs

5. Enter Radius , and find out Area of the Circle.

# include <stdio.h>
# include <conio.h>
void main ( )
{

float a,r;
printf ("Enter the radius of circle:");
scanf ("%f",&r);
a = 3.142857 * r * r; /* Also, a = (float) 22 / 7 * r * r; */
printf ("Area of circle = %f",a);
getch ( );
}

6. Enter Length and Breadth of a Rectangle and find out it's Area.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int l,b,a;
printf ("\n\nEnter the Length of Rectangle :");
scanf ("%d",&l);
printf ("\n\nEnter the Bredth of Rectangle :");
scanf ("%d",&b);
a = l * b;
printf ("\n\n\t\tArea of Rectangle = %d",a);
getch ( );
}

7. Enter the degree in Celcius and change it to degree in Ferhenite.

# include <stdio.h>
# include <conio.h>
main ( )
{
float f,c;
3
Madhukar E
'C' Programs

clrscr ( );
printf ("\nEnter the Degree Celcius :");
scanf ("%f",&c);
f = (c * 5/ 9) +32;
printf ("\n\n\t\tDegree in Ferhenite is = %.2f",f);
getch ( );
}

8. Swap the values of two variables with using a third variable.

# include <stdio.h>
# include <conio.h>
main ( )
{
int a,b,c;
clrscr ( );
printf ("\nEnter the first Number( A ) :");
scanf ("%d",&a);
printf ("\nEnter the Second Number( B ) :");
scanf ("%d",&b);
c = a;
a = b;
b = c;
printf ("\n\n\n\t\tNow the first Number( A ) is : %d",a);
printf ("\n\n\t\tNow the Second Number( B ) is : %d",b);
getch ( );
}
9. Swap the values of two variables without using a third variable.

# include <stdio.h>
# include <conio.h>
main ( )
{
int a,b;
clrscr ( );
printf ("\nEnter the first Number( A ) :");
scanf ("%d",&a);
printf ("\nEnter the Second Number( B ) :");
scanf ("%d",&b);
a = a + b;
b = a - b;
a = a - b;
printf ("\n\n\n\t\tNow the first Number( A ) is : %d",a);
4
Madhukar E
'C' Programs

printf ("\n\n\t\tNow the Second Number( B ) is : %d",b);


getch ( );
}

10. Swap values of two variables without using third variable and
without using '+' operator.

# include <stdio.h>
# include <conio.h>
main ( )
{
int a,b,c;
clrscr ( );
printf ("\nEnter the first Number( A ) :");
scanf ("%d",&a);
printf ("\nEnter the Second Number( B ) :");
scanf ("%d",&b);
a = a * b; /* also , a = a - (-b);*/
b = a / b; /* also , b = a - b; */
a = a / b; /* also , a = a - b; */

printf ("\n\n\n\t\tNow the first Number( A ) is : %d",a);


printf ("\n\n\t\tNow the Second Number( B ) is : %d",b);
getch ( );
}

11. Enter Basic Salary,T.A., D.A., and the percent of increment in salary.
Now calculate the Total Salary after increment.

# include <stdio.h>
# include <conio.h>
void main ( )
{

float s,in;
int ta,da;
printf ("\n\nEnter the salary in Rs. :");
scanf ("%f",&s);
printf ("\nEnter T.A. in Rs. :");
scanf ("%d",&ta);
printf ("\nEnter D.A. in Rs. :");
scanf ("%d",&da);
5
Madhukar E
'C' Programs

printf ("\nEnter the increment (in %):");


scanf ("%f",&in);
s + = ta + da + (s /100) * in;
printf ("\n\n\t\tSalary after increment is Rs %.2f",s);
getch ( );
}

12. Create a Marksheet. Enter marks, and calculate Total and Percentage.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int h,e,m,tot;
float per;
printf ("SUBJECT");
printf ("\t M.M.");
printf ("\t M.O.");
printf ("\n\nHindi");
printf ("\t 100 \t");
scanf ("%d",&h);
printf ("\nEnglish");
printf ("\t 100 \t");
scanf ("%d",&e);
printf ("\nMaths");
printf ("\t 100 \t");
scanf ("%d",&m);
printf ("\n\n\nTOTAL");
printf ("\t 300 ");
printf ("\t%d",tot=(h+e+m));
printf ("\n\nPERCENT");
per = (float) tot/3;
printf ("\t %.2f %",per);
getch ( );
}

13. Make this in the center (Horizontally) of the screen.

*
# include <stdio.h> ***
# include <conio.h> *****
6
Madhukar E
'C' Programs

void main ( ) ***


{ *

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


printf ("\n\t\t\t\t ***");
printf ("\n\t\t\t\t*****");
printf ("\n\t\t\t\t ***");
printf ("\n\t\t\t\t *");
getch ( );
}

14. Enter any Number and find out it's Square and Cube.
For example, if no. is n then find out n2 and n3.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int n;
long s,c;
printf ("Enter any Number : ");
scanf ("%d",&n);
s = (long)n*n;
c = (long)n*n*n;
printf ("\n\n\t\tSquare of %d = %ld",n,s);
printf ("\n\n\t\tCube of %d = %ld",n,c);
getch ( );
}

15. Enter two numbers, and find out the maximum. We assume that the
numbers should not be equal.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int a,b;
printf ("\nEnter the first Number :");
scanf ("%d",&a);
printf ("\nEnter the Second Number :");
7
Madhukar E
'C' Programs

scanf ("%d",&b);
if (a > b)
{
printf ("\n\n\n\t\t%d is Maximum.",a);
}
else
{
printf ("\n\n\n\t\t%d is Maximum.",b);
}
getch ( );
}
16. Check Validity of an entered number. If number is between 0-100,
then print valid, else print invalid.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n;
clrscr ( );
printf ("Enter any number (0-100) :");
scanf ("%d",&n);

if (n >= 0 && n <= 100)


printf ("\n\tValid Input.");
else
printf ("\n\tInvalid Input.");

getch ( );
}

17. Enter any Negative number and change it's to Positive.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int a;
printf ("Enter any negative number :");
scanf ("%d",&a);
if (a < 0)
{
8
Madhukar E
'C' Programs

a = -a; /* also, a = a- (a * 2); */


printf ("\n\n\n\t\t\tNow the number is %d",a);
}
else
{
printf ("\n\n\n\t\t! No Change;the number is already positive.");
}
getch ( );
}

18. Enter any Year and check, is it a Leap Year or not ?

# include <stdio.h>
# include <conio.h>
void main( )
{

int y;
printf ("\n\nEnter the year :");
scanf ("%d",&y);
if ((y % 400 = =0) || (y %100 != 0 && y % 4 = = 0))
{
printf ("\n\n\t\tLeap Year");
}
else
{
printf ("\n\n\t\tNot a Leap Year");
}
getch ( );
}

19. Enter two numbers, and find out the maximum. We assume that the
numbers could be equal.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int a,b;
printf ("\n\nEnter the value of A:");
scanf ("%d",&a);
9
Madhukar E
'C' Programs

printf ("\nEnter the value of B:");


scanf ("%d",&b);
if(a > b)
printf ("\n\n\t\tA is max.");
else
if (b > a)
printf ("\n\n\t\tB is max.");
else
printf ("\n\n\t\tA and B are equal");
getch ( );
}

20. Enter three numbers, and find out the maximum. We assume that the
numbers should not be equal.

# include <stdio.h>
# include <conio.h>
void main ( )
{

printf ("\nEnter the First Number :");


scanf ("%d",&a);
printf ("\nEnter the Second Number :");
scanf ("%d",&b);
printf ("\nEnter the Third Number :");
scanf ("%d",&c);
if (a > b && a > c)
printf ("\n\n\n\t\t%d is Maximum.",a);
else
if (b > a && b > c)
printf ("\n\n\n\t\tB is Maximum..",b);
else
printf ("\n\n\n\t\tC is Maximum.",c);
getch ( );
}

21. Enter three numbers, and find out the maximum, without using logical operators.
We assume that the entered numbers should not be equal.

# include <stdio.h>
# include <conio.h>
void main ( )
10
Madhukar E
'C' Programs

{
int a,b,c;
printf ("\nEnter value for A : ");
scanf ("%d",&a);
printf ("\nEnter value for B : ");
scanf ("%d",&b);
printf ("\nEnter value for C : ");
scanf ("%d",&c);
if (a>b)
{
if (a>c)
{
printf ("\n\n\t\tA is Maximum");
}
else
{
printf ("\n\n\t\tC is Maximum");
}
}
else
{
if (b>c)
{
printf ("\n\n\t\tB is Maximum");
}
else
{
printf ("\n\n\t\tC is Maximum");
}
}
getch ( );
}

22. Enter three numbers, and find out the maximum. We assume that two
or more numbers could be equal.

# include <stdio.h>
# include <conio.h>
void main ( )
{
11
Madhukar E
'C' Programs

int a,b,c;
printf ("Enter the value of A :");
scanf ("%d",&a);
printf ("Enter the value of B :");
scanf ("%d",&b);
printf ("Enter the value of C :");
scanf ("%d",&c);
if (a = = b && a = = c)
printf ("A,B, and C are equal.");
else
if (a > b && a > c)
printf ("A is Maximum.");
else
if(b > c)
if(a = = b)
printf ("A and B are equal and Maximum.");
else
printf ("B is Maximum.");
else
if (b = = c)
printf ("B and C are equal and Maximum.");
else
if(a = = c)
printf ("A and C are equal and Maximum.");
else
printf ("C is Maximum.");
getch ( );
}

23. Enter any alphabet, and change it's case. Upper to lower and lower to upper.

# include <stdio.h>
# include <conio.h>
void main ( )
{

char a;
printf ("Enter any alphabet :");
scanf ("%c",&a);
if (a >= 65 && a <= 90)
{
a = a +32;
12
Madhukar E
'C' Programs

printf ("%c",a);
}
else
if (a >= 97 && a <= 122)
{
a = a-32;
printf ("%c",a);
}
else
printf ("!invalid charactor.");
getch ( );
}

24. Enter any alphabet, and check it's case. Upper case or Lower case.

# include <stdio.h>
# include <conio.h>
void main ( )
{

char a;
printf ("\n\nEnter any Alphabat : ");
scanf ("%c",&a);
if (a >= 65 && a <= 90) /* also, if(a >= 'A' && a <= 'Z') */
printf ("\n\n\t\tCapital Latter");
else
if (a >= 97 && a <= 122) /* also, if(a >= 'a' && a <= 'z') */
printf ("\n\n\t\tSmall Latter");
else
printf ("\n\n\t\t!ERROR. It is not an Alphabat.");
getch ( );
}

25. Enter any number and check, is it an Even number or an Odd number ?

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n;
printf ("\n\nEnter any Number :");
scanf ("%d",&n);
13
Madhukar E
'C' Programs

if (n > 0)
if ( (n % 2)==0)
printf ("\n\n\t\tEVEN");
else
printf ("\n\n\t\tODD");
else
printf ("\n\n\t\t! Please enter any number (n > 0).");
getch ( );
}

26. Enter any alphabet and check for Vowel or Consonant.

# include <stdio.h>
# include <conio.h>
void main ( )
{
char c;
clrscr ( );
printf ("\n\nEnter any Alphabet :");
scanf ("%c",&c);

if (c >= 65 && c <= 90 || c >= 97 && c <= 122)


{
if (c >= 97 && c <= 122)
{
c=c-32;
}
if (c = = 'A' || c = = 'E' || c = = 'I' || c = = 'O' || c = = 'U')
printf ("\n\n\t\tIt is A Vowel.");
else
printf ("\n\n\t\tIt is A Consonent.");
}
else
printf ("\n\n\t\t!ERROR. It is not an Alphabet.");
getch ( );
}

27. Enter any character and check, whether it is an Alphabet, Number,


or any Other Character.

# include <stdio.h>
14
Madhukar E
'C' Programs

# include <conio.h>
void main ( )
{

char c;
printf ("\n\nEnter any Character : ");
scanf ("%c",&c);

if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122))


printf ("\n\n\t\tEntered Character is an Alphabat.");
else
if (c >= '0' && c <= '9')
printf ("\n\n\t\tEntered Character is a Number.");
else
printf ("\n\n\t\t!Any Other Character.");
getch ( );
}

28. Enter any number between 0 to 50 and print it's range.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int n;
printf ("\nEnter any number (0-50) : ");
scanf ("%d",&n);
if (n >= 0 && n<10)
printf ("\n\t\tRange = 0-9");
else if (n >= 10 && n< 20)
printf ("\n\t\tRange = 10-19");
else if (n >= 20 && n< 30)
printf ("\n\t\tRange = 20-29");
else if (n >= 30 && n< 40)
printf ("\n\t\tRange = 30-39");
else if (n >= 40 && n <= 50)
printf ("\n\t\tRange = 40-50");
else
printf ("\n\n\t\t!Invalid Input");
getch ( );
}
15
Madhukar E
'C' Programs

29. Enter any Day number between 1 to 7, and show that Week Day.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int d;
printf ("\nEnter the value of day in number (0 - 6) :");
scanf ("%d",&d);
if (d = = 1)
printf ("\n\n\t\t\t\t\tSunday");
else if (d = = 2)
printf ("\n\n\t\t\t\t\tMonday");
else if (d = = 3)
printf ("\n\n\t\t\t\t\tTuesday");
else if (d = = 4)
printf ("\n\n\t\t\t\t\tWednesday");
else if (d = = 5)
printf ("\n\n\t\t\t\t\tThrusday");
else if (d = = 6)
printf ("\n\n\t\t\t\t\tFriday");
else if (d = = 7)
printf ("\n\n\t\t\t\t\tSaturday");
else
printf ("\n\n\t\t\t!Error Enter a value between 1 - 7 .");
getch ( );
}
30. Enter a number between 0 to 9 and print it's spelling.

# include <stdio.h>
# include <conio.h>
void main ( )
{

char c;
printf ("\n\nEnter any number (0-9) : ");
scanf("%c",&c);
if (c = = '1')
printf ("\n\n\t\tOne");
else if (c = = '2')
printf ("\n\n\t\tTwo");
else if(c = = '3')
16
Madhukar E
'C' Programs

printf ("\n\n\t\tThree");
else if (c = = '4')
printf ("\n\n\t\tFour");
else if (c = = '5')
printf ("\n\n\t\tFive");
else if (c = = '6')
printf ("\n\n\t\tSix");
else if (c = = '7')
printf("\n\n\t\tSeven");
else if (c = = '8')
printf ("\n\n\t\tEight");
else if (c = = '9')
printf ("\n\n\t\tNine");
else if (c = = '0')
printf ("\n\n\t\tZero");
else
printf ("\n\n\t\t! Invalid Number.");
getch ( );
}

31. Enter marks of three subjects. Calculate Total, Percentage and Grade as following :

Basis Grade
Percentage >= 60 First
Percentage >= 48 Second
Percentage >= 38 Third
Percentage >= 36 Pass
In one sub. < 36 Supplementary
In two or more sub.< 36 Fail

# include <stdio.h>
# include <conio.h>
void main ( )
{

int h,e,m,t,c = 0;
float p;
printf ("Enter Marks (0 – 100) :-\n");
printf ("\n\nEnglish : ");
scanf ("%d",&e);
if (e >= 0 && e <= 100)
{
printf ("\nHindi : ");
17
Madhukar E
'C' Programs

scanf ("%d",&h);
if (h >= 0 && h <= 100)
{
printf ("\nMaths : ");
scanf ("%d",&m);
if (m >= 0 && m <= 100)
{
t = e + h + m;
p = (float) t / 3;
printf ("\n\n\tTotal : %d",t);
printf ("\n\n\tPercentage : %.2f",p);
if (h < 36)
c = c +1;
if (e < 36)
c = c +1;
if (m < 36)
c = c +1;

if (c = = 0)
{
if (p >= 60)
printf ("\n\n\tGrade : First");
else if (p >= 48)
printf("\n\n\tGrade : Second");
else if (p >= 38)
printf("\n\n\tGrade : Third");
else if (p >= 36)
printf("\n\n\tGrade : Pass");
}
else if (c = =1)
printf ("\n\n\tGrade : Supplementary");
else
printf ("\n\n\tGrade : Fail");
}
else
printf ("\n\t! Invalid Entry. ");
}
else
printf ("\n\t! Invalid Entry. ");
}
else
printf ("\n\t! Invalid Entry ");

getch ( );
18
Madhukar E
'C' Programs

32. Enter choice 1 to 4. 1- Add, 2-Subtract, 3-Multiply, 4-Division.

# include <stdio.h>
# include <conio.h>
void main ( )
{

float a,b,c;
char ch;
printf ("\n\t1-Addition\n\t2-Subtraction\n\t3-Multiplication\n\t4-Division");
printf ("\n\nEnter your choice :-");
scanf ("%c",&ch);
if(ch = = '1' || ch = = '2' || ch = = '3' || ch = = '4')
{
printf ("Enter the value for first number :");
scanf ("%f",&a);
printf ("Enter the value for second number :");
scanf ("%f",&b);
if (ch = = '1')
{
c = a + b;
printf ("\n\n\t\tAddition of two numbers = %.2f",c);
}
else if (ch = = '2')
{
c = a - b;
printf ("\n\n\t\tSubtracting second from first = %.2f",c);
}
else if (ch = = '3')
{
c = a * b;
printf ("\n\n\t\tMultiplication of first and second = %.2f",c);
}
else if (ch = = '4')
{
c = a / b;
printf ("\n\n\t\tDivision of first by second = %.2f",c);
}
}
else
printf ("\n\n\t\t\t! Enter Correct Value " );
19
Madhukar E
'C' Programs

getch ( );
}

33. Enter two numbers and find out Maximum, using conditional operator ( ? ) .
We assume that the numbers should not be equal.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int a,b;
printf ("\nEnter the first Number :");
scanf ("%d",&a);
printf ("\nEnter the Second Number :");
scanf ("%d",&b);
(a > b) ? printf ("%d is Maximum.",a) : printf ("%d is Maximum.",b);
getch ( );
}

34. Enter any number. Check and print either it is Even or Odd, using conditional
operator ( ? ).

# include <stdio.h>
# include <conio.h>
void main ( )
{

int n;
printf ("\nEnter any number :");
scanf ("%d",&n);
printf ((n%2= = 0)?"\n\n\t\tEven" : "\n\n\t\tOdd");
getch ( );
}

20
Madhukar E
'C' Programs

35. Enter any Year and check whether it is Leap Year or Not a Leap Year using
Conditional operator ( ? ).

# include <stdio.h>
# include <conio.h>
void main ( )
{

int n;
printf ("\nEnter any Year :");
scanf ("%d",&n);
printf((n%4 = = 0 && n%100!= 0 || n%400 = = 0)?"\n\tLeapYear" : "\n\tNot
LeapYear");
getch ( );
}

36. Print "Dheeraj" five times using goto, and then print "Pareek".

# include <stdio.h>
# include <conio.h>
void main()
{

int i=1;
name:
printf (" Dheeraj ");
if (i<6)
{
i++;
goto name;
}
printf ("\n\n Pareek ");
getch ( );
}

37. Enter a number (n). Print inverse counting using goto, from n to 1.

# include <stdio.h>
21
Madhukar E
'C' Programs

# include <conio.h>
void main ( )
{
int n;
printf ("Enter any number :");
scanf ("%d",&n);
a: printf ("\n %d",n);
if (n>1)
{
n--;
goto a;
}
getch ( );
}

38. Enter a number and calculate it's Factorial using goto.

# include <stdio.h>
# include <conio.h>
void main ( )
{
long n,fact=1,c=0;
clrscr ( );
printf ("Enter the number for factorial :");
scanf ("%lu",&n);
x: c = c +1;
fact = fact*c;
if (c = = n)
printf ("Factorial = %lu",fact);
else
goto x;
getch ( );
}

39. Enter any Day number between 1 to 7, and show that Week Day.

# include <stdio.h>
# include <conio.h>
void main ( )
{

int day;
22
Madhukar E
'C' Programs

printf ("enter the value for day between 1-7 :");


scanf ("%d",&day);
printf ("\n\n\n\t\t\t");
switch (day)
{
case 1 : printf ("Sunday");break;
case 2 : printf ("Monday");break;
case 3 : printf ("Tuesday");break;
case 4 : printf ("Wednesday");break;
case 5 : printf ("Thrusday");break;
case 6 : printf ("Friday");break;
case 7 : printf ("Saturday");break;
default : printf ("! Wrong Value.");
}
getch ( );
}

40. Enter any alphabet and check for Vowel or Consonant.

# include <stdio.h>
# include <conio.h>
void main ( )
{
char c;
clrscr ( );
printf ("Enter any Alphabat :");
scanf ("%c",&c);
if (c >= 65 && c <= 90 || c >= 97 && c <= 122)
{
if (c >= 97 && c <= 122)
c = c - 32;
switch (c)
{
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' : printf ("\n\t\tVOWEL");break;
default : printf ("\n\t\tCONSONANT");
}
}
else
printf ("!Error it is not an Alphabat.");
23
Madhukar E
'C' Programs

getch ( );
}

41. Enter two numbers, and choose an operator ( +, -, *, / ) to perform


the function on them.

# include <stdio.h>
# include <conio.h>
void main ( )
{
float a,b,c;
int flag = 0;
char op;
printf ("\n\nEnter the value for A :");
scanf ("%f",&a);
printf ("\n\nEnter the value for B :");
scanf ("%f",&b);
printf ("\n\n\nEnter any operator (+, -, *, / ) : ");
scanf (" %c",&op);
switch (op)
{
case '+': c = a + b; break;
case '-': c = a - b; break;
case '*': c = a * b; break;
case '/': c = a / b; break;
default : printf ("\n\n\t\t\t! Invalid Operator.");flag=1;
}
if (flag = = 0)
printf ("\n\n\n\t\t%.2f %c %.2f = %.2f",a,op,b,c);
getch ( );
}

42. Enter any number between 0 to 9, and spell that number.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int c;
clrscr ( );
printf ("Enter the value between 0-9 :");
scanf ("%d",&c);
24
Madhukar E
'C' Programs

if (c >= 0 && c <= 9)


switch (c)
{
case 0 : printf ("\n\n\t\t\tZero");break;
case 1 : printf ("\n\n\t\t\tOne"); break;
case 2 : printf ("\n\n\t\t\tTwo"); break;
case 3 : printf ("\n\n\t\t\tThree"); break;
case 4 : printf ("\n\n\t\t\tFour"); break;
case 5 : printf ("\n\n\t\t\tFive"); break;
case 6 : printf ("\n\n\t\t\tSix"); break;
case 7 : printf ("\n\n\t\t\tSeven"); break;
case 8 : printf ("\n\n\t\t\tEight"); break;
case 9 : printf ("\n\n\t\t\tNine"); break;
//default : printf ("\n\n\t\t\t! Wrong Value.");
}
else
printf ("\n\n\t\t\t! Enter the correct value (0-9).");
getch ( );
}

43. Enter any number between 0 to 99, and print Grade as per the range it comes in.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,s,flag = 0;
char ch;
printf ("Grades as per range are as follows :- \n\n");
printf (" 0-9 = A \n 10-19 = B \n 20-29 = C \n 30-39 = D \n 40-49 = E");
printf ("\n 50-59 = F \n 60-69 = G \n 70-79 = H \n 80-89 = I \n 90-99 = J");
printf ("\n\nEnter any number (0-99) : ");
scanf ("%d",&n);
s = n/10;
switch (s)
{
case 0 : ch = 'A'; break;
case 1 : ch = 'B'; break;
case 2 : ch = 'C'; break;
case 3 : ch = 'D'; break;
case 4 : ch = 'E'; break;
case 5 : ch = 'F'; break;
case 6 : ch = 'G'; break;
25
Madhukar E
'C' Programs

case 7 : ch = 'H'; break;


case 8 : ch = 'I'; break;
case 9 : ch = 'J'; break;
default : printf ("\n\n!Invalid Input "); flag=1;
}
if (flag = = 0) printf ("\n\n\t\tGrade = %c",ch);
getch ( );
}

44. Enter a character and check wether it is a Digit or not.

# include <stdio.h>
# include <conio.h>
void main( )
{
char c;
clrscr ( );
printf ("\n\nEnter any character :");
c = getchar ( );
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': printf ("\n\n\t\tIt is a Digit.");break;
default : printf ("\n\n\t\t\t! Not a Digit");
}
getch ( );
}

45. Enter any no. between 1-12, and print the Month associated with that.

# include <stdio.h>
# include <conio.h>
main ( )
{
26
Madhukar E
'C' Programs

int m;
clrscr ( );
printf ("\n\nEnter the value for Month between 1-12 :");
scanf ("%d",&m);
printf ("\n\n\n\t\t\t");
switch (m)
{
case 1 : printf ("January");break;
case 2 : printf ("Feburary");break;
case 3 : printf ("March");break;
case 4 : printf ("April");break;
case 5 : printf ("May");break;
case 6 : printf ("June");break;
case 7 : printf ("July");break;
case 8 : printf ("August");break;
case 9 : printf ("September");break;
case 10: printf ("October");break;
case 11: printf ("November");break;
case 12: printf ("December");break;
default : printf ("! WRONG VALUE.");
}
getch ( );
}

46. Enter your choice (1 – 3) to run the specific no. of program.


As :- 1- Vowel or Consonent, 2- Spell Count, 3- Day of week

# include <stdio.h>
# include <conio.h>
void main ( ){
int choice;
printf ("\n 1-Vowel or Consonent \n 2-Spell Count \n 3-Day of Week");
printf ("\n\nEnter your choice to run a program : ");
scanf ("%d",&choice);
switch (choice)
{
case 1:
fflush (stdin);
char ch;
printf ("\n\nEnter any Alphabat :");
scanf ("%c",&ch);
if (ch >= 65 && ch <= 90 || ch >= 97 && ch <= 122)
{
27
Madhukar E
'C' Programs

if (ch >= 97 && ch <= 122)


ch = ch-32;
switch (ch)
{
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' : printf ("\n\t\tVowel");break;
default : printf ("\n\t\tConsonant");
}
}
else
printf ("\n\n\t\t!ERROR This Is Not an Alphabat.");
break;

case 2:
int c;
printf ("\n\nEnter any number between 0-9 :");
scanf ("%d",&c);
if(c >= 0 && c <= 9)
switch (c)
{
case 0 : printf ("\n\n\t\t\tZero");break;
case 1 : printf ("\n\n\t\t\tOne");break;
case 2 : printf ("\n\n\t\t\tTwo");break;
case 3 : printf ("\n\n\t\t\tThree");break;
case 4 : printf ("\n\n\t\t\tFour");break;
case 5 : printf ("\n\n\t\t\tFive");break;
case 6 : printf ("\n\n\t\t\tSix");break;
case 7 : printf ("\n\n\t\t\tSeven");break;
case 8 : printf ("\n\n\t\t\tEight");break;
case 9 : printf ("\n\n\t\t\tNine");break;
//default : printf ("\n\n\t\t\t! WRONG VALUE.");
}
else
printf ("\n\n\t\t\t! Enter the correct value (0-9).");
break;

case 3:
int day;
printf ("\n\nEnter the value for day between 1-7 :");
scanf ("%d",&day);
printf ("\n\n\n\t\t\t");
28
Madhukar E
'C' Programs

switch (day)
{
case 1 : printf ("Sunday");break;
case 2 : printf ("Monday");break;
case 3 : printf ("Tuesday");break;
case 4 : printf ("Wednesday");break;
case 5 : printf ("Thrusday");break;
case 6 : printf ("Friday");break;
case 7 : printf ("Saturday");break;
default : printf("! WRONG VALUE.");
}
break;

default: printf ("\n\n\t\t!Invalid Choice.");


}
getch ( );
}

47. Enter a number n. Print counting till n, starting from 1.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i,n;
i=1;
printf ("\nEnter the value for N :");
scanf ("%d",&n);
while (i <= n)
{ printf ("\n %d",i);
i++;
}
getch ( );
}
48. Enter a number n. Print counting till n, starting from 1, except those which are
divisible by 5.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=1;
printf ("Enter no. for counting : ");
scanf ("%d",&n);
29
Madhukar E
'C' Programs

while (i <= n)
{
if (i%5!=0)
printf ("%4d",i);
i++;
}
getch ( );
}

49. Enter a number and print it's table.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i,n,t;
i=1;
printf ("\nEnter no. for table :");
scanf ("%d",&n);
while (i<=10)
{
t=n*i;
printf ("\n\n %d",t);
i++;
}
getch ( );
}

50. Enter any number and print it's factorial.

# include <stdio.h>
# include <conio.h>
void main ( )
{
long n,fact=1,c=0;
clrscr ( );
printf ("Enter the number for factorial :");
scanf ("%lu",&n);
while (n>1)
{
fact*=n;
n--;
30
Madhukar E
'C' Programs

}
printf ("\n\n\t\tFactorial = %lu",fact);
getch ( );
}

51. Enter value for Base, and and it’s Exponent say x and n respectively.
Now calculate and print the value of xn .

# include <stdio.h>
# include <conio.h>
void main ( )
{
int x,n,t=1;
printf ("\nEnter Value for Base :");
scanf ("%d",&x);
printf ("\nEnter the value of exponent :");
scanf ("%d",&n);
printf ("\n\n\t\t");
while (n>0)
{
t* = x;
printf ("%d",x);
if (n>1)
printf (" * ");
n--;
}
printf ("= %d",t);
getch ( );
}

52. Enter a value for n. Print the first 25 ASCII symbols starting from n.

# include <stdio.h>
# include <conio.h>
main ( )
{
int a,i=1;
clrscr ( );
printf ("\nEnter the Starting Number :");
scanf ("%d",&a);
while (a<=255)
{
if (i <= 25)
printf ("\n\n\tASCII Symbole for value %d is %c",a,a);
31
Madhukar E
'C' Programs

i++;
a++;
}
getch ( );
}
53. Enter 10 values and print it's total.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int day,total=0,i=0;
while (i!=10)
{
printf ("Enter the value : ");
scanf ("%d",&day);
total = total + day;
i++;
}
printf ("\n\n\t\t\t\tTotal = %d",total);
getch ( );
}

54. Print and add all Even numbers till n. (n = Limit)

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=2,sum=0;
printf ("Enter limit :");
scanf ("%d",&n);
while (i<= n)
{
printf ("\n%d",i);
sum += i;
i += 2;
}
printf ("\n\n\t\tSum of Even no.'s till %d = %d",n,sum);
getch ( );
}

32
Madhukar E
'C' Programs

55. Print and add first n odd numbers. (n = Terms)

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=1,sum=0,j=1;
printf ("Enter Terms :");
scanf ("%d",&n);
while (j<= n)
{
printf ("\n%d",i);
sum += i;
i += 2;
j++;
}
printf ("\n\n\t\tSum of Even no.'s till %d = %d",n,sum);
getch ( );
}

56. Enter any number and print the sum of digits of that number.

# include <stdio.h>
# include <conio.h>
main ( )
{
long n;
int r,s = 0;
clrscr ( );
printf ("\nEnter any Number :");
scanf ("%ld",&n);

while (n>0)
{
r = n %10;
s = s+r;
n = n/10;
}
printf ("\n\n\t\tSum of digits of the number = %d",s);
getch ( );
}

33
Madhukar E
'C' Programs

57. Enter a Number and reverse the digits of that number.

# include <stdio.h>
# include <conio.h>
main ( )
{
long n,r,s=0;
clrscr ( );
printf ("\nEnter any Number :");
scanf ("%ld",&n);
while (n>0)
{
r = n %10;
s = (s*10) + r;
n = n/10;
}
printf ("\n\n\t\tNow the Number is = %ld",s);
getch ( );
}

58 Enter any number and check whether it is Prime or not.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i,count=0;
printf ("\nEnter any no. :");
scanf ("%d",&n);
for(i=1;i<=n;i++)
{
if ((n%i)==0)
count++;
}
if (count==2)
printf ("\n\n\t\tPrime");
else
printf ("\n\n\t\tNot Prime");
getch ( );
}

59 Enter any number and check whether it is Armstrong or not.

34
Madhukar E
'C' Programs

# include <stdio.h>
# include <conio.h>
main ( )
{
int k,n,r,s=0;
clrscr ( );
printf ("\nEnter any Number :");
scanf ("%d",&n);
k = n;
while (n > 0)
{
r = n %10;
s += r * r * r;
n = n/10;
}
if(s= =k)
printf ("\n\n\t\tArmstrong");
else
printf ("\n\n\t\tNot Armstrong");
getch ( );
}

60 Enter a Limit for n, and print Fibonaccii series till n.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b= 0,c=1,n;
clrscr ( );
printf ("\nEnter limit :");
scanf ("%d",&n);
printf ("\n\n");
while (n > 0)
{
printf (" %d ",c);
a = b;
b = c;
c = a+b;
n--;
}
getch ( );
}
35
Madhukar E
'C' Programs

61 Enter values while choice is True, and print their sum.

# include <stdio.h>
# include <conio.h>
void main ( )
{
clrscr ( );
int n, sum = 0;
char ch = 'y';
while (ch = = 'y' || ch = ='Y')
{
printf ("\nEnter the value to Add :");
scanf ("%d",&n);
sum += n;
fflush (stdin);
printf ("\n\n\t\tDo you want to Add more ? y/n :");
scanf ("%c",&ch);
}
printf ("\n\n\t\tSum = %d ",sum);
getch ( );
}

62. Print the series and calculate it's sum. (1+ 2 + 3 + 4 + 5 + 6 + ….........n )

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,t=1,sum=0;
clrscr ( );
printf ("Enter the Limit:");
scanf ("%d",&n);
printf ("\n\n");
while (t<=n)
{
sum += t;
printf ("%d",t);
if (t < n)
36
Madhukar E
'C' Programs

printf ("+");
t++;
}
printf ("\n\n\t\tSum of the series = %d",sum);
getch ( );
}

63. Print the series and calculate it's sum. (!1 + !2 + !3 + !4 + !5 + !6 + ….........!n )

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=1;
long fact=1,sum = 0;
clrscr ( );
printf ("Enter the Limit:");
scanf ("%d",&n);
printf ("\n\n");
while (i <= n)
{
fact *= i;
sum += fact;
printf ("!%d",i);
if (i<n)
printf ("+");
i++;
}
printf ("\n\n\t\tSum of the [Link] = %ld",sum);
getch ( );
}

64. Print the series and calculate it's sum. (x0 + x1 + x2 + x3 + x4 + …......... xn )

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=0,x;
long exp=1,sum = 0;
37
Madhukar E
'C' Programs

clrscr ( );
printf ("Enter the value for Base :");
scanf ("%d",&x);
printf ("Enter value for Exponent :");
scanf ("%d",&n);
printf ("\n\n");
while (i <= n)
{
sum += exp;
printf ("%d",exp);
if (i < n)
printf("+");
exp *= x;
i++;
}
printf ("\n\n\t\tSum of the series = %ld",sum);
getch ( );
}

65. Print the series and calculate it's sum. (1/1+ 1/2 + 1/3 + 1/4 +….........1/n )

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,j=1;
float i,sum = 0;
clrscr ( );
printf ("\n\nEnter the number of terms for sum of series :");
scanf ("%d",&n);
printf ("\n\n\n");
while (j <= n)
{
i=1.0/j; /* i= (float) 1/j; */
printf("1/%d",j);
if (n > j)
printf (" + ");
sum += i;
j++;
}
printf ("\n\n\t\tSum of Series = %f",sum);
getch ( );
38
Madhukar E
'C' Programs

}
66. Print the series and calculate it's sum. (1/!1 + 1/!2 + 1/!3 + 1/!4 +….........!n )

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=1;
float fact =1,sum = 0;
clrscr ( );
printf ("Enter the Limit:");
scanf ("%d",&n);
printf ("\n\n");
while (i <= n)
{
fact *= i;
sum += 1/fact;
printf ("1/!%d",i);
if (i < n)
printf(" + ");
i++;
}
printf ("\n\n\t\tSum of the series = %f",sum);
getch ( );
}

67. Print the series and calculate it's sum. (x1/1 + x2/2 + x3/3 + x4/4+…......... xn/n)

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=1,x;
float sum = 0;
long exp = 1;
clrscr ( );
printf ("Enter the value for Base :");
scanf ("%d",&x);
printf ("Enter value for Exponent :");
scanf ("%d",&n);
printf ("\n\n");
while (i <= n)
{
39
Madhukar E
'C' Programs

exp *= x;
sum += (float)exp/i;
printf ("%ld/%d",exp,i);
if (i < n)
printf (" + ");
i++;
}
printf ("\n\n\t\tSum of the series = %f",sum);
getch ( );
}
68. Print the series and calculate it's sum. (x1/!1 + x2/!2 + x3/!3 +…......... xn/!n)

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=1,x;
long exp=1,fact=1;
float sum;
clrscr ( );
printf ("\nEnter the value for Base :");
scanf ("%d",&x);
printf ("\nEnter Limit or say, value for Exponent :");
scanf ("%d",&n);
printf ("\n\n");
while (i <= n)
{
exp *= x;
fact *= i;
sum += (float) exp/fact;
printf ("%ld/!%d",exp,i);
if (i < n)
printf (" + ");
i++;
}
printf ("\n\n\t\tSum of the series = %f",sum);
getch ( );
}

.69 Print the Alternate series and calculate it's sum. (1- 2 + 3 - 4 + 5 - 6 + ….........n )

# include <stdio.h>
# include <conio.h>
40
Madhukar E
'C' Programs

void main ( )
{
int n, sum = 0, t = 1,op = -1;
printf ("Enter the limit : ");
scanf ("%d",&n);
printf ("\n\n");
while (t <= n)
{
op = op * (-1);
sum = sum + op * t;
printf("%d",t);
if (t < n)
if (t % 2 = = 0)
printf (" + ");
else
printf (" - ");
t++;
}
/*
while (t <= n)
{
if (t%2 = = 0)
sum = sum - t;
else
sum = sum + t;
printf("%d",t);
if (t < n)
if (t % 2 = = 0)
printf (" + ");
else
printf (" - ");
t++;
}
*/
printf ("\n\n\t\tSum of the series = %d ",sum);
getch ( );
}

70. Print the following by entering the value of n.


if n = 3.
1 2 3
1 2 3
1 2 3
41
Madhukar E
'C' Programs

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,c,r = 1;
printf ("\nEnter the value for n :");
scanf ("%d",&n);
printf ("\n\n");
while (r <= n)
{
c = 1;
while (c <= n)
{
printf (" %d ",c);
c++;
}
printf ("\n\n");
r++;
}
getch ( );
}

71. Print the following by entering the value of n.


if n = 3.
1 2 3
# include <stdio.h> 4 5 6
# include <conio.h> 7 8 9
void main ( )
{

int n,c,r = 1,i = 1;


printf ("\nEnter the value for n :");
scanf ("%d",&n);
printf ("\n\n");
while (r <= n)
{
c = 1;
while (c <= n)
{
printf ("%3d",i);
42
Madhukar E
'C' Programs

c++;
i++;
}
printf ("\n\n");
r++;
}
getch ( );
}

72. Print the structure like this :


If n = 3. * * *
* * *
# include <stdio.h> * * *
# include <conio.h>
void main ( )
{
int n,i=1,j;
printf ("Enter the value for n :");
scanf ("%d",&n);
printf ("\n\n");
while (i <= n)
{
j=1;
while (j <= n)
{
printf (" * ");
j++;
}
printf ("\n\n");
i++;
}
getch ( );
}
73. Print a triangle of stars like this :
If n = 3. *
* *
* * *
# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=1,j;
printf ("Enter the value for n :");
43
Madhukar E
'C' Programs

scanf ("%d",&n);
while (i <= n)
{
j=1;
while (j <= i)
{
printf (" * ");
j++;
}
printf ("\n\n");
i++;
}
getch ( );
}

74. Print a triangle of stars like this :


If n = 3. * * *
* *
*
# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=1,j;
printf ("Enter the value for n :");
scanf ("%d",&n);
while (n > 0)
{
j=1;
while (j <= n)
{
printf (" * ");
j++;
}
printf ("\n\n");
n --;
}
getch ( );
}

75. Print a triangle of stars like this :


If n = 3. *
* *
44
Madhukar E
'C' Programs

* * *
# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,k,i=1,j;
printf ("Enter the value for n :");
scanf ("%d",&n);
k = n;
printf ("\n");
while (i <= n)
{
j=k;
while (j > 0)
{
printf (" ");
j--;
}
j=1;
while (j<=i)
{
printf (" * ");
j++;
}
printf ("\n\n");
k--;
i++;
}
getch ( );
}

76. Print a triangle of stars like this :


If n = 3. * * *
* *
*
# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=1,j,k;
printf ("Enter thevalue for n:");
scanf ("%d",&n);
k = n;
45
Madhukar E
'C' Programs

printf ("\n");
while (i <= n)
{
j=1;
while (j< i)
{
printf (" ");
j++;
}
j = k;
while(j > 0)
{
printf ("*");
j--;
}
printf ("\n");
i++;
k--;
}
getch ( );
}

77. Print a triangle of stars like this :


If n = 3.
*
# include <stdio.h> ***
# include <conio.h> *****
void main ( )
{
int n,i=1,j,k,l=1;
printf ("Enter the value for n:");
scanf ("%d",&n);
k = n;
printf ("\n\n\n");

while (i <= n)
{
j= k-1;
while (j > 0)
{
printf (" ");
j--;
}
j=1;
46
Madhukar E
'C' Programs

while (j <= l)
{
printf ("*");
j++;
}
printf ("\n");
i++;
l += 2;
k--;
}
getch ( );
}
78. Print a triangle of stars like this :
If n = 3. *****
***
# include <stdio.h> *
# include <conio.h>
void main ( )
{
int n,i=1,j,k;
printf ("Enter no. of rows for Pyramid :");
scanf ("%d",&n);
k = n+n-1;
printf ("\n\n\n");
while (i <= n)
{
j=1;
while (j < i)
{
printf (" ");
j++;
}
j = k;
while (j > 0)
{
printf ("*");
j--;
}
printf ("\n");
i++;
k - = 2;
}
getch ( );
}
47
Madhukar E
'C' Programs

79. Print a triangle of stars like this :


If n = 3.
*
***
*****
# include <stdio.h> ***
# include <conio.h> *
void main ( )
{
int n,i=1,j,k,l=1;
printf ("\nEnter the value of n:");
scanf ("%d",&n);
printf ("\n");
k = n;
while (i <= n)
{
j= k-1;
while (j > 0)
{
printf (" ");
j--;
}

j=1;
while (j<= l)
{
printf ("*");
j++;
}
printf ("\n");
i++;
l += 2;
k--;
}

i= 1;
l -= 2;
while (i < n)
{
j=1;
while (j <= i)
48
Madhukar E
'C' Programs

{
printf (" ");
j++;
}
j = 1;
l -= 2;
while (j<= l)
{
printf ("*");
j++;
}
printf ("\n");
i++;
}
getch ( );
}

80. Print counting 1 – 10 using do.. while ( ) loop.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i;
i=1;
do
{
printf ("%4d",i);
i++;
}while (i <= 10);
getch( );
}

81. Show the actual concept of do…while ( ) loop.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i;
49
Madhukar E
'C' Programs

i=1;
do
{
printf ("%d",i);
i++;
}while (i > 100);
getch ( );
}

82. Enter values while choice is true, and print Sum & Average of those values.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i= 0;
float sum,avg;
char ch;
do
{
printf ("\nEnter the value to Add :");
scanf ("%d",&n);
fflush (stdin);
printf ("\n\n\t\tContinue ?(y/n) :");
scanf ("%c",&ch);
sum += n;
i++;
}while (ch= ='y' || ch= ='Y');
avg = sum/i;
printf ("\n\n\t\tSum = %.2f",sum);
printf ("\n\n\t\tAverage = %.2f",avg);
getch ( );
}

83. Enter values and sum,while value is not -1.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n, sum = 0;
char ch;
50
Madhukar E
'C' Programs

do
{
printf ("\nEnter value to Add (-1 for Exit) :");
scanf ("%d",&n);
if (n!= -1)
sum += n;
}while (n!= -1);
printf ("\n\n\t\tSum = %d ",sum);
getch ( );
}

84. Enter values and check for Even or Odd, while entered value is != -1.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n;
do
{
printf ("\n\nEnter any number (-1 for Exit) :");
scanf ("%d",&n);
if (n!= -1)
if (n%2 == 0)
printf ("\n\n\t\tEven");
else
printf ("\n\n\t\tOdd");
}while (n!= -1);
getch ( );
}

85. Enter values while not less than 0,print total values entered and
no. of values in different ranges.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i=0,j=0,k=0;
do
{
printf ("\nEnter value (less than 0 for Exit) :");
51
Madhukar E
'C' Programs

scanf ("%d",&n);
if(n >= 0)
{
if (n >= 0 && n <= 100) i++;
if (n >100 && n <= 500) j++;
if (n >500) k++;
}
} while(n >= 0);
printf ("\n\n\t\tTotal Values Entered = %d",i+j+k);
printf ("\n\n\t\tValues of Range 0-100 = %d",i);
printf ("\n\n\t\tValues of Range 101-500 = %d",j);
printf ("\n\n\t\tValues of above 501 = %d",k);
getch ( );
}

86. Enter a limit n, and print counting 1 to n .

# include <stdio.h>
# include <conio.h>
void main( )
{
int i,n;
clrscr ( );
printf ("\nEnter Limit :");
scanf ("%d",&n);
printf ("\n\n");
for (i=1;i<= n;i++)
{
printf (" %4d ",i);
}
getch ( );
}

87. Enter no. of terms n, and print first n odd no's.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i,n,a;
printf ("\nEnter no. of terms :");
scanf ("%d",&n);
52
Madhukar E
'C' Programs

printf ("\n\n");
for(i=1,a=1;a <= n;i++)
{
if (i%2!= 0)
{
printf (" %d ",i);
a++;
}
}
getch ( );
}

88. Enter a limit n, print and sum all Even no's till n.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i,n,sum=0;
clrscr ( );
printf ("\nEnter Limit :");
scanf ("%d",&n);
printf ("\n\n");
i=1;
for ( ;i <= n; )
{
if (i%2 = = 0)
{
sum += i;
printf ("%4d",i);
}
i++;
}
printf ("\n\n\n\t\tTotal of even no's = %d",sum);
getch ( );
}

89. Enter a limit, and print inverse counting from the limit to 1.

# include <stdio.h>
# include <conio.h>
void main ( )
53
Madhukar E
'C' Programs

{
int i;
clrscr ( );
printf ("\nEnter Limit :");
scanf ("%d",&i);
printf ("\n\n");
for ( ; i ;i--)
printf ("%4d",i);
getch ( );
}

90. Show the use of Comma operator in for ( ; ; ) loop.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i,n;
clrscr ( );
printf ("\n\n");
for (i=1,n=20 ; i <= n ; i++,n--)
{
printf ("\n\n\t\t%d",i);
printf ("\t%d",n);
}
getch ( );
}

91. Enter a limit in alphabet, and print alphabets till that limit.

# include <stdio.h>
# include <conio.h>
void main ( )
{
char n,i;
clrscr ( );
printf ("\nEnter a Limit in Alphabats(a-z) :");
scanf ("%c",&n);
printf ("\n\n");
if (n >= 97 && n <= 122)
for(i = 'a';i <=n ;i++)
printf ("%3c",i);
54
Madhukar E
'C' Programs

else if (n >= 65 && n <= 90)


for (i = 'A';i <= n;i++)
printf ("%3c",i);
else
printf ("!Entered character is not an Alphabet.");
getch ( );
}

92. Show an Unterminated loop.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i;
i=1;
for ( ; ; )
{
printf ("%4d",i);
i++;
}
getch ( );
}

93. Enter a number and print it's table.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i,n,t;
printf ("\nEnter any Number :");
scanf ("%d",&n);
for (i = 1;i <= 10;i++)
{
t = n * i;
printf ("\n\n\t%d",t);
}
getch ( );
}

55
Madhukar E
'C' Programs

94. Enter n values, and find out maximum and Second maximum out of them.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,max,smax,v;
printf ("\nEnter How many values :");
scanf ("%d",&n);
printf ("\n\n");
for (int i =1;i <= n;i++)
{
printf ("Enter value :");
scanf ("%d",&v);
if (i = = 1)
smax = max = v;

if (v > max)
{
smax = max;
max = v;
}
else if (v > smax)
smax = v;
}
printf ("\n\n\t\tMaximum is %d.",max);
printf ("\n\n\t\tSecond Maximum is %d.",smax);
getch ( );
}

95. Print series of first n Prime numbers.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i,j,c,count;
printf ("\nEnter no. of Primes :");
scanf ("%d",&n);
printf ("\n\n");
for (i =1,count =1;count <= n;i++)
{
c = 0;
56
Madhukar E
'C' Programs

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


if (i%j = = 0)
{
c = 1;
break;
}
if (c = = 0)
{
printf ("%4d",i);
count++;
}
}
getch ( );
}

96. Print series of n Armstrong no's.

# include <stdio.h>
# include <conio.h>
main ( )
{
int k,n,r,s,count,i;
clrscr ( );
printf ("\nEnter any Number :");
scanf ("%d",&n);
for (i = 1,count = 0;count <= n && i < 32767;i++ )
{
k = i;
s = 0;
while(k > 0)
{
r = k %10;
s += r * r * r;
k = k/10;
}
if(s==i)
{
printf ("%4d",i);
count++;
}
}
getch ( );
}
57
Madhukar E
'C' Programs

97. Enter a limit n, and print tables till n.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i,n,t;
printf ("\nEnter any Number :");
scanf ("%d",&n);
printf ("\n\n");
for (i = 1; i <= n; i++)
{
for (int j = 1;j <= 10;j++)
{
t = i * j;
printf ("%5d",t);
}
printf ("\n\n");
}
getch ( );
}

98. Print the structure like this :


If n = 3. * * *
* *
# include <stdio.h> * * *
# include <conio.h>
void main ( )
{
int n,i,j;
printf ("\nEnter no. of rows :");
scanf ("%d",&n);
printf ("\n\n");
for (i = 1;i <= n;i++)
{
for (j =1;j <= n;j++)
if (i= =1 || i= =n)
printf ("*");
else if (j= =1 || j= =n)
printf ("*");
else
58
Madhukar E
'C' Programs

printf (" ");


printf ("\n");
}
getch ( );
}

99. Print the structure like this :


If n = 3 *
***
*****
# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i,j,k;
printf ("\nEnter no. of rows :");
scanf ("%d",&n);
printf ("\n\n");
k=1;
for (i =1;i <= n;i++)
{
for (j =1;j <= n-i;j++)
printf (" ");
for(j =1;j <= k;j++)
printf ("*");
printf ("\n");
k += 2;
}
getch ( );
}

100. Print the structure like this :


If n = 4 *
* *
* *
*******
# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,i,j,k;
printf ("\nEnter no. of rows :");
scanf ("%d",&n);
59
Madhukar E
'C' Programs

printf ("\n\n");
k =1;
for (i =1;i <= n;i++)
{
for (j =1;j <= n-i;j++)
printf (" ");
for (j =1;j <= k;j++)
if (j = =1 || j = =k)
printf ("*");
else if (i = =n)
printf ("*");
else
printf (" ");
printf ("\n");
k += 2;
}
getch ( );
}

101. Prin the structure like this :


If n = 3
1
2 3
4 5 6

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i,j,c=1,n;
printf ("Enter Limit :");
scanf ("%d",&n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf (" %4d",c);
c++;
}
printf ("\n\n");
}
getch ( );
}
60
Madhukar E
'C' Programs

102. Prin the structure like this :


If n = 5
1
2 3
4 5 6
7 8 9 1
2 3 4 5 6
# include <stdio.h>
# include <conio.h>
void main ( )
{

int i,j,c =1,n;


printf("\nEnter no. of rows :");
scanf("%d",&n);
printf("\n\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf (" %4d",c);
if (c = = 9)
c = 0;
c++;

}
printf ("\n\n");
}
getch ( );
}

103. Print the structure like this :


If n = 3
1
2 1 2
3 2 1 2 3
# include <stdio.h>
61
Madhukar E
'C' Programs

# include <conio.h>
void main ( )
{

int n,i,j,k;
printf ("\nEnter no. of rows :");
scanf ("%d",&n);
printf ("\n\n");
for (i =1;i <= n;i++)
{
for (j = 1;j <= n-i;j++)
printf (" ");
for (j = i;j >=1;j--)
printf ("%d",j);
for (j = 2;j <= i;j++)
printf ("%d",j);
printf ("\n");
}
getch ( );
}

104. Enter values through an unterminated loop, if value = 0 then, break.


Print sum and average of entered values.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int n,sum=0,i=0;
float avg;
while (1)
{
printf ("\nEnter value :");
scanf ("%d",&n);
if (n = = 0)
break;
sum += n;
i++;
}
avg = (float) sum/i;
printf ("\n\n\t\tSum = %d",sum);
printf ("\n\n\t\tAverage = %.2f",avg);
62
Madhukar E
'C' Programs

getch ( );
}

105. Enter any number and print it's table .

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i,n;
printf ("\nEnter no. for table :");
scanf ("%d",&n);
for (i =1;i<= n *10;i++)
{
if (i%n != 0)
continue;
printf ("\n\n\t%d",i);
}
getch ( );
}

106. Enter value for n. i.e. (n >0 and n <=100). Sum all the values from n to 100
through an unterminated loop.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int x,sum = 0;
printf ("\nEnter no. :");
scanf ("%d",&x);
while (2 > 1)
{
if (x > 0 && x <= 100)
sum += x;
else
break;
x++;
}
printf ("\n\n\t\tSum = %d",sum);
getch ( );
}

63
Madhukar E
'C' Programs

107. Enter a limit, print all Even no's till limit.

# include <stdio.h>
# include <conio.h>
void main ( )
{
int i,n;
printf ("\nEnter the limit :");
scanf ("%d",&n);
printf ("\n\nNow the Even Numbers till %d are :-\n\n\n",n);
for (i =1;i <= n;i++)
{
if (i %2!= 0)
continue;
printf ("%4d",i);
}
getch ( );
}

108. Enter a limit, print counting till limit, except those are divisible by 8.

# include <stdio.h>
# include <conio.h>
void main()
{
int i,n;
printf ("\nEnter Limit :");
scanf ("%d",&n);
printf ("\n\nCounting till %d, except divisibles of 8 :-\n",n);
for (i =1;i<= n;i++)
{
if (i %8 = = 0)
continue;
printf ("\n%d",i);
}
getch ( );
}

109. Check wether the entered character is Alphanumeric or not?

# include <stdio.h>
64
Madhukar E
'C' Programs

# include <conio.h>
# include <ctype.h>
void main ( )
{
char c;
printf ("Enter any Character :");
c = getchar ( );
if (isalnum(c))
printf ("\n\n\t\tEntered character is Alphanumaric.");
else
printf ("\n\n\t\tNot an Alphanumeric character.");
getch ( );
}

110. Check, is the entered character is Printable and ASCII character,


if yes then print the ASCII value of it.

# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void main ( )
{
char c;
printf ("Enter any Character :");
c = getche ( );
if (isprint(c))
if (isascii(c))
printf ("\n\n\t\tASCII value of %c is %d.",c,c);
else
printf ("\n\n\t\tNot an ASCII character.");
else
printf ("\n\n\t\tNot a Printable character.");
getch ( );
}
111. Check the entered character for Alphabet, Digit, and Other character.

# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void main ( )
{

char c;
65
Madhukar E
'C' Programs

printf ("Enter any Character :");


c = getchar ( );
if (isdigit(c))
printf ("\n\n\t\tIt is a Digit.");
else if (isalpha(c))
printf ("\n\n\t\tIt is an Alphabet.");
else
printf ("\n\n\t\tOther character.");
getch ( );
}

112. Press a key from keyboard and check wether it is a Control Key or not.

# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void main ( )
{
char c;
printf ("Enter any Character :");
c = getche ( );
if (iscntrl(c)!= 0)
printf ("\n\n\t\tIt is a Control key.");
else
printf ("\n\n\t\tNot a Control key.");
getch ( );
}

113. Enter a character, and print it in reverse case.

# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void main ( )
{
char c;
printf ("\nEnter any Alphabet :");
c = getchar ( );
printf ("\n\n\t\t");
if (islower(c))
putchar (toupper(c));
else if (isupper(c))
66
Madhukar E
'C' Programs

putchar (tolower(c));
else
printf ("\n\n\t\tEntered character is not an Alphabet.");
getch ( );
}

114. Enter any character and check, is it a Punctuation character or not ?

# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void main ( )
{
char c;
printf ("\nEnter any Character :");
c = getchar ( );
printf ("\n\n\t\t");
if (ispunct(c))
{
putchar (c);
printf (" is a punctuation character.");
}
else
printf ("\n\tEntered character is not a punctuation character.");
getch ( );
}

115. Enter any character and check is it a Space character or not.

# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void main ( )
{
char c;
printf ("Enter any character :");
c = getche ( );
printf ("\n\n\tIs the entered char. is a Space Char.? : ");
if (isspace(c))
putchar ('Y');
else
putchar ('N');
67
Madhukar E
'C' Programs

getch ( );
}

116. Enter any character and check for the Digit of Hexadecimal Number System.

# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void main ( )
{
char c;
printf ("Enter any Character :");
c = getchar ( );
if (isxdigit(c))
printf ("\n\tIt is a digit of Hexadecimal Number System.");
else
printf ("\n\tNot a digit of Hexadecimal Number System.");
getch ( );
}

117. Write a program for read and write an one dimensional integer Array.

# include<stdio.h>
# include<conio.h>
//#define N 10
//const N = 10
void main ( )
{
int i,x[10]; //int i,x[N];
for (i=0;i<10;i++) //for(i=0;i<N;i++)
{
printf("Enter the Number : ");
scanf("%d",&x[i]);
}
for (i=0;i<10;i++) //for(i=0;i<N;i++)
printf("%4d",x[i]);
getch ( );
}

118 Add two Integer arrays.


68
Madhukar E
'C' Programs

#include<stdio.h>
#include<conio.h>
void main ( )
{
int i,a[5],b[5],c[5];

printf("Reading First Array.....\n");


for(i=0;i<5;i++)
{
printf("\nEnter the Value : ");
scanf("%d",&a[i]);
}

printf("\n\nReading Second Array.....\n");


for(i=0;i<5;i++)
{
printf("\nEnter the Value : ");
scanf("%d",&b[i]);
}

printf("\n\nSum(a[i] + b[i]) =");


for(i=0;i<5;i++)
{
c[i]=a[i]+b[i];
printf("%4d",c[i]);
}
getch( );
}

119. Find out Maximum of an array.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],n,max=0,i;
printf("How many Values to Enter (1-10) :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d value :",i+1);
69
Madhukar E
'C' Programs

scanf("%d",&a[i]);
}
max = a[1];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
printf("Maximum Value = %d",max);
getch( );
}

120. Program for sorting through Bubble Sort or Selection Sort.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],n,i,j,t,max,imax;
printf("\nHow many values to insert (1-10) :");
scanf("%d",&n);
printf("\n\n");

/* INSERT AN ARRAY */

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

/* SORTING BY BUBBLE SORT */

for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(a[j] > a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
/*
/* SORTING BY SELECTION SORT */

for(i=0;i<n-1;i++)
{
70
Madhukar E
'C' Programs

max = a[0];
imax = 0;
for(j=1;j<n-i;j++)
if(max < a[j])
{
max = a[j];
imax = j;
}
a[imax] = a[n-i-1];
a[n-i-1] = max;
}
*/
/* PRINTING OF SORTED ARRAY */

printf("\n\n\t");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
getch( );
}

121. Program for Binary search.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],n,i,j,t,s,top,bottom,mid;
printf("\nenter the no. of values you want to insert (1-10) :");
scanf("%d",&n);
printf("\n\n");

/* INSERT AN ARRAY */

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

/* SORTING BY BUBBLE SORT */

for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(a[j] > a[j+1])
{
t=a[j];
71
Madhukar E
'C' Programs

a[j]=a[j+1];
a[j+1]=t;
}

/* PRINTING OF SORTED ARRAY */

printf("\n\n\t");
for(i=0;i<n;i++)
printf(" %d ",a[i]);

printf("\n\n\n\nEnter the no. you want to search.:");


scanf("%d",&s);

/* SEARCHING No. BY BINARY SEARCH */

top=0;
bottom=n-1;
while(top <= bottom)
{
mid=(top+bottom)/2;
if(s= =a[mid])
break;
else
if(a[mid] > s)
bottom=mid-1;
else
top=mid+1;
}

if(top <= bottom)


printf("\n\n\n\t\t\tFound at %d position.",mid+1);
else
printf("\n\n\n\t\t\t!Search not found");
getch( );
}
122. Program for Linear search and Replace.

#include<stdio.h>
#include<conio.h>
void main( )
{
int n,a[10],s,i,r,flag=0;
printf("How many values to insert (1-10) :");
scanf("%d",&n);
72
Madhukar E
'C' Programs

for(i=0;i<n;i++)
{
printf("Enter value :");
scanf("%d",&a[i]);
}
printf("\n\nEnter the value to search :");
scanf("%d",&s);
for(i=0;i<n;i++)
if(s= =a[i])
{
printf("\n\n\tThe value %d found on %d position ",s,i+1);
printf("\n\nReplace %d with : ",s);
scanf("%d",&r);
a[i] = r;
flag += 1;
}
if(flag= =0)
printf("\n\tValue not found");
printf("\n\nNow the new Array is :- ");
for(i=0;i<n;i++)
printf("%5d",a[i]);

getch( );
}

123. Program for Read and Print 2D array (Matrix)

#include<stdio.h>
#include<conio.h>
void main( )
{
int r,c,a[10][10],i,j;
printf("Enter the no. of rows :");
scanf("%d",&r);
printf("Enter the no. of columns :");
scanf("%d",&c);

/* * * * * * READ ARRAY (Row Wise) * * * * * */

for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
printf("Row[%d] Column[%d] :",i,j);
73
Madhukar E
'C' Programs

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

/*
* * * * * * READ ARRAY (Column Wise) * * * * *

for(i=0;i<c;i++)
for(j=0;j<r;j++)
{
printf("Row[%d] Column[%d] :",j,i);
scanf("%d",&a[j][i]);
}
*/

/* * * * * * PRINT ARRAY * * * * * */

printf("\n\nNow the array is :-\n\n");

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

getch( );
}

124 Program for Addition of two Matrices.

#include<stdio.h>
#include<conio.h>
void main( )
{
int r,c,a[10][10],b[10][10],s[10][10],i,j,k,l;
printf("Enter the no. of rows :");
scanf("%d",&r);
printf("Enter the no. of columns :");
scanf("%d",&c);

/* * * * * Read First Matrix * * * * */


74
Madhukar E
'C' Programs

printf("\n\nNow enter values for First Matrix A[%d][%d] :-\n\n",r,c);


for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
printf("Row[%d] Column[%d] :",i,j);
scanf("%d",&a[i][j]);
}

/* * * * * Read Second Matrix * * * * */

printf("\n\nNow enter the Second 2D array B[%d][%d] :-\n\n",r,c);


for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
printf("Row[%d] Column[%d] :",i,j);
scanf("%d",&b[i][j]);
}

/* * * * * Addition of Matrices * * * * */

for(i=0;i<r;i++)
for(j=0;j<c;j++)
s[i][j]=a[i][j] + b[i][j];

/* * * * * * Print all the Three Matrices * * * * * */

printf("\n\nNow the array is :-\n\n");

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%4d",a[i][j]);

printf("\t\t");

for(k=0;k<c;k++)
printf("%4d",b[i][k]);

printf("\t\t");

for(l=0;l<c;l++)
printf("%4d",s[i][l]);
75
Madhukar E
'C' Programs

printf("\n");
}

getch( );
}

125 Program for Matrix Multiplication.

#include<stdio.h>
#include<conio.h>
void main( )
{
int m,n,p,a[10][10],b[10][10],c[10][10],i,j,k,l;
printf("Enter the no. of rows for first Matrix :");
scanf("%d",&n);
printf("Enter the no. of columns for first Matrix :");
scanf("%d",&m);

/* READ FIRST MATRIX */

printf("\n\nNow enter the values for First Matrix A[%d][%d] :-\n\n",n,m);


for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
printf("Row[%d] Column[%d] :",i,j);
scanf("%d",&a[i][j]);
}

/* READ SECOND MATRIX */

printf("\n\nEnter the no. of columns for second Matrix :");


scanf("%d",&p);
printf("\n\nNow enter the values for Second Matrix B[%d][%d] :-\n\n",m,p);
for(i=0;i<m;i++)
for(j=0;j<p;j++)
{
printf("Row[%d] Column[%d] :",i,j);
scanf("%d",&b[i][j]);
}

/* MULTIPLYING FIRST MATRIX WITH SECOND */

76
Madhukar E
'C' Programs

for(i=0;i<n;i++)
for(j=0;j<p;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}

/* PRINT RESULTANT MATRIX */

printf("\n\nNow the Resultant Matrix is :-\n\n");

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

126 Program for Diagonals sum, of a Matrix.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10][10],i,j,fwsum=0,rwsum=0,n;
printf("Enter the no. of rows and columns for Matrix :");
scanf("%d",&n);

/* * * * * READ MATRIX * * * * */

printf("\n\nNow enter the Matrix A[%d][%d] :-\n\n",n,n);


for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("Row[%d] Column[%d] :",i,j);
scanf("%d",&a[i][j]);
}

/* * * * * PRINT MATRIX * * * * */
77
Madhukar E
'C' Programs

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

/* * * * * CALCULATING FORWARD DIAGONAL SUM * * * * */

for(i=0;i<n;i++)
fwsum += a[i][i];
printf("\n\nForward Diagonal Sum = %d",fwsum);

/* * * * * CALCULATING REVERSE DIAGONAL SUM * * * * */

for(i=0;i<n;i++)
rwsum += a[i][n-1-i];
printf("\n\nReverse Diagonal Sum = %d",rwsum);

getch();
}

127 Program for Matrix Transpose

#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10][10],b[10][10],i,j,r,c;
printf("Enter the no. of rows for Matrix :");
scanf("%d",&r);
printf("Enter the no. of columns for Matrix :");
scanf("%d",&c);

/* * * * * READ MATRIX * * * * */
78
Madhukar E
'C' Programs

printf("\n\nNow enter the Matrix A[%d][%d] :-\n\n",r,c);


for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
printf("Row[%d] Column[%d] :",i,j);
scanf("%d",&a[i][j]);
}

/* * * * * PRINT MATRIX * * * * */

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

/* * * * * TRANSPOSE MATRIX * * * * */

for(i=0;i<r;i++)
for(j=0;j<c;j++)
b[j][i] = a[i][j];

/* * * * * PRINT TRANSPOSED MATRIX * * * * */

printf("\n\n Now the Transposed Matrix is :-\n\n");


for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
printf("%5d",b[i][j]);
printf("\n\n");
}
getch( );
}

128 Program for Read and print Character Array.

#include<stdio.h>
#include<conio.h>
void main ( )
79
Madhukar E
'C' Programs

{
int i,n;
char c[20];

/*
* * * * * * FOR READING CHARACTER BY CHARACTER * * * * * *

printf("how many characters to enter (1-20):");


scanf ("%d",&n);
for ( i=0;i < n; i++)
{ fflush(stdin);
scanf("%c",&c[i]);
}
for ( i = 0; i < n; i++)
printf("%c",c[i]);

* * * * * * * FOR READING A COMPLETE STRING * * * * * * *

printf("Enter any String (max 20 char long) :");


gets(c); //scanf("%s",c);
printf("\n\n%s",c);

* * * * * FOR READING STRING USING getchar() * * * * *


*/
printf("Enter any String (max 20 char long) :");
for(i=0;(c[i] = getchar()) != '\n';i++);
c[i] = '\0';
for(i=0;putchar(c[i]) ;i++);

getch( );
}

129 Program for String Concatenation.

#include<stdio.h>
#include<conio.h>
void main ( )
{
int i,j=0,k=0;
char a[10],b[10],c[20];
clrscr( );
printf("Enter First String (max 10 char long):");
gets(a);
80
Madhukar E
'C' Programs

fflush(stdin);
printf("Enter Second String (max 10 char long):");
gets(b);
for ( i=0;a[i] != NULL;i++)
c[i] = a[i];

for ( ;b[j] != NULL;i++,j++)


c[i] = b[j];

c[i] = NULL;
printf("%s",c);
getch( );
}

130 Program for Copy a String and Change it's Case.

#include<stdio.h>
#include<conio.h>
void main ( )
{
int i,j=0,k=0;
char a[10],b[10],c[20];
clrscr( );
printf("\nEnter First String (max 10 char long):");
gets(a);
fflush(stdin);
printf("\n\nEnter Second String (max 10 char long):");
gets(b);

for ( i=0;a[i] != NULL;i++)


c[i] = a[i];

for ( ;b[j] != NULL;i++,j++)


c[i] = b[j];

c[i] = NULL;
printf("\n\nNow the Resultant String is :-");
printf("\n\n%s",c);
getch( );
}

131 Program for Compare Strings on basis of Length.


81
Madhukar E
'C' Programs

#include<stdio.h>
#include<conio.h>
void main( )
{
int j,i,c=0;
char a[20],b[20];
printf("Enter First String:-");
scanf("%s",a);
printf("Enter Second String:-");
scanf("%s",b);
i=0;
while(a[i]= =b[i] && a[i] != '\0' && b[i] != '\0')
i = i + 1;
if(a[i] = = '\0' && b[i] = = '\0')
printf("\n\nStrings are equal");
else
printf("\n\nStrings are not Equal");
for(i=0;a[i] != NULL;i++)
c += 1;
printf("\n\nLength of string 1 is %d",c);
c=0;
for(i=0;b[i] != NULL;i++)
c += 1;
printf("\n\nLength of string 2 is %d",c);
getch( );
}

132 Count Vowels in a String and Pad them by " * ".

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ( )
{
int i,c=0;
char a[20],b[20];
clrscr( );
printf("Enter any String :");
gets(a);
for ( i=0;a[i] != NULL;i++)
{
a[i] = toupper(a[i]);
82
Madhukar E
'C' Programs

if(a[i] = ='A' || a[i] = ='E' || a[i] = ='I' || a[i] = ='O' || a[i] = ='U')
{
a[i] = '*';
c++;
}
}
printf("\n\n%s",a);
printf("\n\nThere are %d Vowels in the string.",c);
getch( );
}

133 Program for Reverse a String.

#include<stdio.h>
#include<conio.h>
void main ( )
{
int i,j;
char a[20],b[20];
clrscr( );
printf("Enter any String :");
gets(a);
for ( i=0;a[i] != NULL;i++)
{ }
i--;
for (j=0;i >= 0;j++,i--)
b[i] = a[j];
b[j] = NULL;
printf("%s",b);
getch( );
}

134. Check a String for Palindrome.

#include<stdio.h>
#include<conio.h>
void main ( )
{
int i,j,c=0;
char a[20];
clrscr( );
printf("Enter any String (max 20 char long):");
83
Madhukar E
'C' Programs

gets(a);
for ( i=0;a[i] != NULL;i++)
{ }
i--;
for (j=0;i > j;j++,i--)
if ( a[j] != a[i])
c += 1;
if(c = = 0)
printf("\n\n\tPalindrome");
else
printf("\n\n\t\tNot a Paindrome");

getch( );
}

135. Count Frequency of Alphabets in a string.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ( )
{
int i,j,k,c[26];
char a[20];
clrscr( );
printf("Enter any String (max 20 char) :");
gets(a);
for(i=0;i<26;i++)
c[i] = 0;
for(i=0;a[i] != NULL;i++)
{
a[i] = toupper(a[i]);
k = a[i] - 65;
c[k] += 1;
}

for(i=0;i<26;i++)
if(c[i] != 0)
printf("\n%c = %d",i+65,c[i]);
getch ( );
}

84
Madhukar E
'C' Programs

136. Remove extra spaces from a string and change it into Proper case.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ( )
{
int i,j=0;
char a[30],b[20];
clrscr ( );
printf("Enter any String (max 20 char) :");
gets(a);
a[0] = toupper(a[0]);
for(j=0,i=j+1;a[j] != '\0';j++,i++)
{
if(a[j] ==' ' && a[i] !=' ')
a[i] = toupper(a[i]);
else
a[i] = tolower(a[i]);
}
printf("\n\n%s",a);
getch();
}

137. Program showing use of String.h functions.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ( )
{
int i,n,len1=0,len2=0;
char a[40],b[40],c[40];

/* * * * * * * READING A STRING * * * * * * */

printf("\NEnter First String (max 20 char long) :");


gets(a);
printf("\NEnter Second String (max 20 char long) :");
gets(b);

85
Madhukar E
'C' Programs

/* * * COPYING STRING TO ANOTHER ARRAY * * */

strcpy (c,a);

/* * * * * CALCULATING LENGTH OF STRINGS * * * * */

len1 = strlen(a);
len2 = strlen(b);
printf("\n\nLength of first String is %d",len1);
printf("\n\nLength of Second String is %d",len2);

/* * * * * * COMPAIRING BOTH STRINGS * * * * * */

i = strcmpi(a,b);
if(i == 0)
printf("\n\nBoth Strings are equal");
else
if(i > 0)
printf("\n\nSecond String is Alphabetically above String First");
else
printf("\n\nFirst String is Alphabetically above Second String");

/* * * * * * CONCATENATING BOTH STRINGS * * * * * */

strcat(a,b);
printf("\n\nConcatinating Second String with First :-");
printf("%s",a);

/* * * * * CHANGING CASE OF RESULTANT STRING * * * * */

// strlwr(a);
strupr(a);
printf("\n\nString after changing case :- %s",a);

printf("\n\n String C copied from A is : %s",c);

getch( );
}

138. Program for comparing strings on basis of Alphabetical Order.

#include<stdio.h>
86
Madhukar E
'C' Programs

#include<conio.h>
void main( )
{
int c,i;
char a[10],b[10];
printf("\n\nEnter First String:-");
scanf("%s",a);
printf("\n\nEnter Second String:-");
scanf("%s",b);
for(i=0;a[i]!=NULL || b[i]!=NULL;i++)
if(a[i] != b[i])
{
if(a[i] > b[i])
c = 1;
else
c = -1;
break;
}
else
c = 0;

if(c = = 0)
printf("\n\nBoth Strings are Equal");
else
if(c = = -1)
printf("\n\nFirst String is Albetically above Second String.");
else
printf("\n\nSecond String is Alphabetically above First String.");
getch( );
}

139. Program for Sorting Array of Strings.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
{
int i,j,n;
char a[10][20],t[20];
clrscr( );

87
Madhukar E
'C' Programs

/* * * * * READING ARRAY OF STRINGS * * * * */

printf("\nEnter how many Strings :-");


scanf("%d",&n);
for(i=0;i < n;i++)
{
fflush(stdin);
gets(a[i]);
}

/* * * * SORTING OF ARRAY BY BUBBLE SORT * * * */

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


for(j=0;j < n-i;j++)
if(strcmp(a[j],a[j+1]) > 0)
{
strcpy(t,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],t);
}

/* * * * PRINTING SORTED ARRAY OF STRINGS * * * */

printf("\n\nNow the sorted array of strings is :-\n\n");


for(i=0;i < n;i++)
printf("\n%s",a[i]);

getch( );
}

140. Addition using Function

#include<stdio.h>
#include<conio.h>
int add(int,int);
void main ( )
{
int a,b,c;
clrscr ( );
scanf("%d %d",&a,&b);
c = add(a,b);
printf("%d",c);
getch ( );
88
Madhukar E
'C' Programs

int add(int x,int y)


{
int z;
z = x + y;
return(z);
}

141. Include File

[Link]

#include<stdio.h>
#include<conio.h>
#include"[Link]"
void main ( )
{
int a,b,c;
a = read( );
b = read( );
c = add(a,b);
printf("%d",c);
getch( );
}

[Link]

int read( )
{
int x;
printf("Enter Value :");
scanf("%d",&x);
return x;
}
int add( int x,int y)
{
return x+y;
}

142. Area of Circle using Function


89
Madhukar E
'C' Programs

#include<stdio.h>
#include<conio.h>
float aofcircle(float);
void main ( )
{

float r,a;
clrscr( );
printf("Enter Radius :");
scanf("%f",&r);
a = aofcircle(r);
printf("%f",a);
getch();
}

float aofcircle(float r)
{
float ar;
float pi = 22/7;
ar = pi * r * r;
return ar;
}

143 Fibonacci Series using function

#include<stdio.h>
#include<conio.h>
void fibonacci(int n)
{
int a=0,b=0,c=1;
while(c <= n)
{
printf("%4d",c);
a = b;
b = c;
c = a+b;
}
}

90
Madhukar E
'C' Programs

void main ( )
{
int n;
clrscr ( );
printf("enter Limit for Fibonacci series :");
scanf("%d",&n);
fibonacci(n);
getch();
}

144 sum of series using function

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

void sumseries(int n)
{
int i=0,s=0;
while(i<n)
{
i++;
s += i;
printf("%d ",i);
}
printf("\n sum = %d",s);
}

void main ( )
{
int n;
clrscr( );
printf("Enter Limit :");
scanf("%d",&n);
sumseries(n);
getch();
}

145 Read & write matrix by function

#include<stdio.h>
#include<conio.h>
91
Madhukar E
'C' Programs

void readmat(int x[10][10],int r,int c)


{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
printf("Enter Value :");
scanf("%d",&x[i][j]);
}
}

void printmat(int x[10][10],int r,int c)


{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%4d",x[i][j]);
printf("\n");
}
}

void main ( )
{
int a[10][10],r,c;
clrscr( );
printf("Enter no. of Rows :");
scanf("%d",&r);
printf("Enter no. of Columns :");
scanf("%d",&c);
readmat(a,r,c);
printmat(a,r,c);
getch();
}

146 Using Register variable

#include<stdio.h>
#include<conio.h>
void main ( )
{
int a[10],n;
92
Madhukar E
'C' Programs

clrscr ( );
register int i;
for(i=0;i<1000;i++)
printf("%4d",i);
getch( );
}

147 Using Static variable

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

int fun ()
{
static int i = 10;
return i++;
}

void main ( )
{
int a,b;
clrscr ( );
a = fun( );
b = fun( );
printf("\nA = %d",a);
printf("\nB = %d",b);
getch( );
}

148 Using Global variable

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

int x;
int read ()
{
x = 15;
//scanf("%d",&x);
return(x);
}
int add(int x,int y)
93
Madhukar E
'C' Programs

{
return (x+y);
}

void main ( )
{
//int a,b,c; // By Default Auto Variable.
auto int a,b,c;
clrscr ( );
x = 1;
a = read( );
c = add(a,x);
printf("\nc = %d",c);
getch( );
}

149 Using Extern variable

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

void main ( )
{
clrscr( );
extern int a;
printf("Enter any value :");
scanf("%d",&a);
printf("\nA = %d",a);
getch( );
}
int a;

150 Add using pointers

#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,c,*p,*q,*r;
clrscr ( );
printf("Enter two values :");
scanf("%d %d",&a,&b);
94
Madhukar E
'C' Programs

p = &a;
q = &b;
r = &c;
*r = *p + *q;
printf("Addition is %d",c);
getch( );
}

151 Swap values by function using pointers

#include<stdio.h>
#include<conio.h>
void swap (int*,int*);
void main ()
{
int a,b;
clrscr ( );
printf("value A = ");
scanf("%d",&a);
printf("Value B = ");
scanf("%d",&b);
swap(&a,&b);
printf("\nA = %d",a);
printf("\nB = %d",b);
getch( );
}

void swap(int *a,int *b)


{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}

152 Two dimension array using pointers

#include<stdio.h>
#include<conio.h>
void main ()
{
int a[10][10],r,c,i,j;
95
Madhukar E
'C' Programs

printf("Enter Rows :");


scanf("%d",&r);
printf("Enter Columns :");
scanf("%d",&c);

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


for(j = 0;j < c;j++)
scanf("%d",(*(a+i)+j));

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


{
for(j=0;j<c;j++)
printf("%4d",*(*(a+i)+j));

printf("\n");
}
getch();
}

153 Dynamic mamory allocation

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

void main ()
{
int n;
char *p;
clrscr ( );
printf("Enter limit :");
scanf("%d",&n);
p = (char *)malloc(n * sizeof(char));
// p = (char *)malloc(10 * sizeof(char));
fflush(stdin);
gets(p);
for(int i=0;i<n;i++)
printf("%c",*p++);

getch();
}

96
Madhukar E
'C' Programs

154 Sorting of strings array using pointer

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

void main ( )
{
int i,j,n,ln;
char *p[10], *t;
printf("Enter no. of names to insert :");
scanf("%d",&n);
fflush(stdin);
t = (char *)malloc(80 * sizeof(char));
for(i = 0;i<n;i++)
{
gets(t);
ln = strlen(t)+1;
p[i] = (char *)malloc(ln * sizeof(char));
strcpy(p[i],t);
}
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(strcmpi(p[j],p[j+1]) > 0)
{
t = p[j];
p[j] = p[j+1];
p[j+1] = t;
}
for(i=0;i<n;i++)
printf("\n %s",p[i]);
getch();
}

155 Pointer to function

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

int pro (int(*)(int,int));


int add(int,int);
97
Madhukar E
'C' Programs

int sub(int,int);
void main ( )
{
int i,j;
clrscr( );
printf("\n\tAddition :-\n");
i = pro(add);
printf("\nA + B = %d",i);
printf("\n\tSubtraction :_\n");
j = pro(sub);
printf("A - B = %d",j);
getch( );
}

int add(int a, int b)


{
return a * b;
}

int sub(int a, int b)


{
return a+b;
}
int pro(int(*p)(int,int))
{
int a,b,c;
printf("Enter Value for A :");
scanf("%d",&a);
printf("Enter Value for B :");
scanf("%d",&b);

c = p(a,b);
return c;
}

156 Factorial using Recursive function

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

int fact(int);
void main ( )
{
98
Madhukar E
'C' Programs

int n,f;
clrscr( );
printf("Enter two values :");
scanf("%d",&n);
f = fact (n);
printf("\nFactorial of %d is %d",n,f);
getch();
}

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

157 Xn using Recursive function.

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

int power(int,int);
void main ( )
{
int x,n,e;
clrscr( );
printf("Enter Base values :");
scanf("%d",&x);
printf("Enter Exponent's values :");
scanf("%d",&n);

e = power (x,n);
printf("\nResult of %d to the power %d is %d",x,n,e);
getch();
}

int power(int x,int n)


{
if (n == 1)
return x;
else
return(x * power(x,n-1));
99
Madhukar E
'C' Programs

158 Structure

#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int h,e,m,tot;
}p;

void main ( )
{
//student p;
printf("Enter name :");
gets([Link]);
printf("Marks for Hindi :");
scanf("%d",&p.h);
printf("Marks for English :");
scanf("%d",&p.e);
printf("Marks for Maths :");
scanf("%d",&p.m);
[Link] = p.h + p.e + p.m;

printf("\n\nName is %s",[Link]);
printf("\nHindi : %d",p.h);
printf("\nEnglish : %d",p.e);
printf("\nMaths : %d",p.m);
printf("\nTotal : %d",[Link]);
getch( );
}

159 Array of structures

#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int h,e,m,tot;
};
100
Madhukar E
'C' Programs

void main ( )
{
student p[20];
int i,n;
clrscr ( );
printf("How many Records :");
scanf("%d",&n);

for(i=0;i<n;i++)
{
fflush(stdin);

printf("\n\nEnter name :");


gets(p[i].name);
printf("Marks for Hindi :");
scanf("%d",&p[i].h);
printf("Marks for English :");
scanf("%d",&p[i].e);
printf("Marks for Maths :");
scanf("%d",&p[i].m);
p[i].tot = p[i].h + p[i].e + p[i].m;
}

for(i=0;i<n;i++)
{
printf("\n\nName is %s",p[i].name);
printf("\nHindi : %d",p[i].h);
printf("\nEnglish : %d",p[i].e);
printf("\nMaths : %d",p[i].m);
printf("\nTotal : %d",p[i].tot);
}
getch( );
}

160 Pointer to Structure

#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int h,e,m,tot;
101
Madhukar E
'C' Programs

};

void read (struct student *x);


void print(struct student *x);
void main ( )
{
student p;
clrscr ( );
read (&p);
print (&p);
getch( );
}

void read (struct student *x)


{
printf("Enter name :");
gets(x -> name);
printf("Marks for Hindi :");
scanf("%d",&x -> h);
printf("Marks for English :");
scanf("%d",&x -> e);
printf("Marks for Maths :");
scanf("%d",&x -> m);
x -> tot = x -> h + x -> e + x -> m;
}

void print(struct student *x)


{
printf("\n\nName is %s",x -> name);
printf("\nHindi : %d",x -> h);
printf("\nEnglish : %d",x -> e);
printf("\nMaths : %d",x -> m);
printf("\nTotal : %d",x -> tot);
}

161 Stack

#include<stdio.h>
#include<conio.h>
#define N 10

typedef struct
{
102
Madhukar E
'C' Programs

int a[N];
int i;
}stack;

void main ( )
{
stack p;
int c,v,j;
p.i = -1; //Initialization of Stack
do
{
//clrscr( );
printf("\n1 for Push");
printf("\n2 for Pop");
printf("\n3 for Print");
printf("\n4 for Empty");
printf("\n5 for Exit");
printf("\n\nEnter your Choice :");
scanf("%d",&c);

switch (c)
{
case 1:
if(p.i == 9)
printf("! Stack is Full.");
else
{
p.i = p.i + 1;
printf("Enter value :");
scanf("%d",&v);
p.a[p.i] = v;
}
break;

case 2:
if(p.i == -1)
printf("\nNo more Pop \n");
else
{
v = p.a[p.i];
p.i--;
printf("\nValue Popped %d ",v);
}
break;
103
Madhukar E
'C' Programs

case 3:
for(j = 0; j <= p.i;j++)
printf("%4d",p.a[j]);
printf("\n");
break;

case 4:
p.i = -1;
break;

}
}while(c != 5);

getch( );
}

162 Queue

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

struct queue
{
int a[10],front,rear;
};

void main ( )
{
int c,v,i;
struct queue p;
[Link] = [Link] = 9;
do
{
printf("\n1 Insert");
printf("\n2 Delete");
printf("\n3 Display");
printf("\n4 Exit");
printf("\n\nEnter your Choice :");
scanf("%d",&c);

switch (c)
{
104
Madhukar E
'C' Programs

case 1:
if([Link] == 9)
[Link] = 0;
else
[Link] = [Link] + 1;

if([Link] == [Link])
printf("Queue is Full");
else
{
printf("Enter any value :");
scanf("%d",&v);
p.a[[Link]] = v;
}
break;

case 2:
if([Link] == [Link])
printf("queue is Empety");
else
{
if([Link] == 9)
[Link] = 0;
else
{
[Link] = [Link] + 1;
v = p.a[[Link]];
printf("\nValue Removed %d",v);
}
}
break;

case 3:
if([Link] == 9)
i = 0;
else
i = [Link] + 1;

for( ; ;i++)
{
printf("%4d",p.a[i]);
if(i == [Link])
break;

105
Madhukar E
'C' Programs

if(i == 9)
i = -1;

}
break;
}
}while(c != 4);
getch( );
}

163 Create a file & copy contents to another file

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ( )
{
FILE *fp,*p;
int c;
clrscr ( );
fp = fopen("file1","w+");
while((c = getw(stdin)) != EOF) //getc(stdin) For characters.
{
putw(c,fp); //putc(c,fp) For characters.
}
rewind (fp);
p = fopen("file2","w");
while((c = getw(fp)) != EOF) //getc(fp) For characters.
{
putw(c,p); //putc(c,fp) For characters.
}
fclose(fp);
fclose(p);
getch( );
}

164 Reverse the contents of a file

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

106
Madhukar E
'C' Programs

void main ( )
{
FILE *fp,*p;
int n;
char c;
clrscr ( );
fp = fopen("file1","w+");
while((c = getc(stdin)) != EOF)
{
putc(c,fp);
}
rewind (fp);
fseek(fp,0L,2);
fseek(fp,-1L,1);
while(1)
{
n = ftell(fp);
c = getc(fp);
putchar(c);
if (n == 0)
break;

if(c =='\n')
fseek(fp, -3L,1);
else
fseek(fp, -2l,1);
}
fclose(fp);
getch( );
}

165 Structure in file and update structure.

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

void main ( )
{
FILE *fp,*p;
struct a
{
char name[20];
float sal;
107
Madhukar E
'C' Programs

}r;

int n;
clrscr ( );
fp = fopen("file1","w+");

printf("Enter no of records :");


scanf("%d",&n);

for(int i=1;i<=n;i++)
{
fflush(stdin);
gets([Link]);
scanf("%f",&[Link]);
fwrite(&r,sizeof(struct a),1,fp);
}
rewind(fp);

while(1)
{
fread(&r,sizeof(struct a),1,fp);
if(feof(fp))
break;
fprintf(stdout,"%s %f \n",[Link],[Link]);
}
rewind(fp);
p = fopen("ss","w+");
while(1)
{
fread(&r,sizeof(struct a),1,fp);
if(feof(fp))
break;
[Link] = [Link] + ([Link] * 10 / 100);
fwrite(&r,sizeof(struct a),1,p);
}
fclose(fp);
rewind(p);
while (1)
{
fread(&r,sizeof(struct a),1,p);
if (feof(p))
break;
fprintf(stdout,"%s %f \n",[Link],[Link]);
}
108
Madhukar E
'C' Programs

fclose(p);
getch( );
}

109
Madhukar E

You might also like