Sei sulla pagina 1di 167

Serial Topic Page

Number Number
1. SEQUENTAL CONTROL PROGRAMS 8-20
-Program to add two numbers.
-Program to calculate total marks and percentage of
student in five subjects.
-Program to covert distance from kilometre to
meter.
-Program to convert temp.from F to C.
-Program to convert temp.from C to F.
-Program to calculate area and perimeter of
rectangle.
-Program to calculate area and circumference of a
circle.
-Program to swap two number using third variable.
-Program to swap two number without using third
variable.
-Program to show the use of width option with
scanf().
-Program to show the use of flag and width option
with printf().
-Program to input and display a character using
getch(),getche().getchar(),putch() and putchar().

2. SELCTION CONTROL PROGRAMS 20-38


-Program to check number is positive using simple if.
-Program to check number is positive or negative
using if-else.
-Program to find largest of 2 numbers.
-Program to find smallest of 2 numbers.
-Program to check wheather number is even or odd.
-Program to check wheather year is leap or not.
-Program to find greatestof three(nested if else)
-Program to input cost price and selling price of an
item and check whether profit,loss,no profit,no loss.
-Program to find smallest of three numbers(nested if
else).
-Program to check number is positive,negative or
zero(else if ladder).
-Program to calculate valu of y,when value of x is
input by user(else if ladder).
-Program to check roots of quadratic equation are
real,equal or imaginary(else if ladder).
-Program to convert an uppercase alphabet to
lowercase and vice versa(else if ladder).
-Program to check whether entered character is
uppercase,lowercase, digit or special symbol.
-Program to convet hourly wage of a person in a
week (else if ladder).
-Program to find largest of two numbers(ternary
operator).
-Program to find smallest of two numbers(ternary
operator).
-Program to find largest of three numbers (ternary
operator).
-Program to check whether a number is even or odd
(ternary operator).
-Program to check ehether number is
positive,negative or zero(ternary operator).
-Program to check whether year is leap or
not(ternary operator).
-Program to display bytes occupied by different
data types using sizeof operator.
3. ITERATION PRoGRAMS 38-67
-Program to print “hello” 5 times(while loop.)
-Program to display n natural numbers and their
sum(while loop).
-Program to diplay first n natural even numbers
and their sum(while loop).
-Program to display first n natural odd numbers
and their sum(while loop).
-Program to calculate percentage and grade of n
number of students(while loop).
-Program to print “hello”5 times(do while loop).
-Program to print “hello” 5 times(for loop).
-Program to calculate factorial of a number(for
loop).
-Program to calculate sum of a digit of a number
(while loop).
-Program to calculate sum of first and last digi of a
number.
-Program to print a numeric pattern(nested for
loop).
-Program to reverse list using decrement(for loop).
-Program to calculate reverse of a number (while
loop).
-Program to determine whether a number is
palindrome or not.
-Program to print table of a n umber.
-Program to display Fibonacci series.
-Program to print all ASCII values and their
equivalent character.
-Program to print Armstrong numbers.
-Program to how the use of nested for loop.
-Program to find sum of series 12+12+….n2.
-Program to find sum of series 1/1+1/2+1/3……1/n.
-Program to print a pattern with*.
-Program to find sum of series 1/1!+1/2!.....1/n!.
-Program to display all three digit in well ordered
numbers less than 200.
-Program to show the use of break statement(for
loop).
-Program to show the use of break statement(nested
for loop).
-Program to show the use of continue statement.
-Program to show the use of continue(nested for
loop).
-Program to display all prime numbers starting
from 1 to 500.
-Program to convert decimal number into binary
form.

4. SWITCH PROGRAM 67-71


-Program to print day of a week when day number
is input by the user.
-A menu-driven program to add,subtract,multiply
and divide 2 numbers.
5. GOTO PROGRAM 71-73
-Program to show the use of goto
statement(forward jump).
-Program to calculate square root of a positive
number using goto statement(backward jump).
6. FUNCTION PROGRAM 74-86
-Program to add 2 numbers(using function).
-Program to determine whether number is even or
odd(using function).
-Program to find factorial of a number(using
function).
-Program to print a message in which function does
not take any argument and does not return any
value.
-Program to find largest of two numbers(using
function)
-Program to find average of first five natural
numbers(using function)
-Program to find largest of two numbers(using
function)
-Program to convert upper case alphabet to lower
case and vice versa(using functions)
-Program to swap two numbers(using callby value
method)
-Program to swap two numbers(using callby
reference)
-Program to calculate sum and difference of two
number(using callby reference)
-Program to find factorial of an number(recursion)
-Program to find sum of first n natural
number(recurision)
-Program to display Fibonacci series(using
recursion)
-Program to find sum of digits of number(using
recursion)
-Program to find HCF and LCM of two
numbers(recursion)

7. ARRAYS 86-130
►1D ARRAY
-Program to input elements to array and then
display on screen
-Program to input elements in array and then
calculate sum and average
-Program to reverse an array
-Program to input elements in an array and then
count the number of positive, negative and zero
element
-Program to inter change corresponding elements of
two equal size array
-Program to find max element from an array
-Program to find min element from an array
-Program to search an element from an array using
linear search
-Program to search an element from an array using
binary search
-Program to sort the elements of array using bubble
sorting.
-Program to insert an elements in an unsorted array
at a specific postion
-Program to delete an element from an unsorted
array
-Program to find the largest and second largest
element from an array.
-Program to add a pointer to an integer.
-Program to subtract a pointer from an integer.
-Program to subtract 2 pointers
-Program to compare 2 pointers
-Program to pass individual aaray elements to a
fuction using call by value method.
-Program to pass individual array elements to a
fuction using call by reference method.
-Program to pass entire array to a function using
call by reference method.
-Program to show the use of array of pointers to
integers.
►2D ARRAY
-Program to add 2 matrix.
-Program to subtract 2 matrices.
Program to pass 2D array to a function.-Program to
input elementys of a 2D and then display them in a
matrix format.
-Program to find product of matrices.
-Program to calculate transpose of a matrix
-Program to calculate sum of square of diagional
elements of a matrix.
-Program to determine a square matrix is
symmetric or not
-Program to display upper triangle and lower
triangle of a square matrix.

8. STRINGS 130-154
-Program to input a string from user using scanf().
-Program to input string from the user gets() and
display using puts().
-Program to pass a string to a function and also
display ACSII value of string character.
-Program to calculate length of string (using strlen)
-Program to to calculate length of string with using
strlen()
-Program to copy one string to another
string(strcpy)
- Program to copy one string to another string
without using (strcpy)
-Program to compare two strings using(strcmp)
- Program to compare two strings without
using(strcmp)
-Program to concatenate two strings using (strcat)
- Program to concatenate two strings without using
(strcat)
-Program to reverse a string using(strrev)
- Program to reverse a string without using(strrev)
-Program to check whether the string is palindrome
or not
-Program to convert a sting into lower case using
(strlwr)
-Program to convert a sting into lower case without
using (strlwr)
-Program to convert a sting into upper case using
(strupr)
-Program to convert a sting into upper case without
using (strupr)
-Program to demonstrate 2D array of strings
-Program to demonstrate array of a pointer to
string
-Program to sort string alphabetically
-Program to count number of lower case,upper case
alphabet,digits,space and special symbols in strings

9. STRUCTURE AND UNIONS 154-175


-To input and display the roll number,name and
marks obtained by two students
-To print the assigned values to members of the
structure
-Program that demonstrate initialization of the
structure variables.
-Program that demonstrates structure assignment
operation.
-To show structure members are stored in
contiguous memory location.
-To input data of n students and display the details
of students with higest percentage of marks
-To show how structures are initialized.
-To show how to access elements of nested structure
-To access the member of structure in which we
have pointer to a structure
-To show how members of structre are accessed
using a pointer to a structure
-Consider a structure fraction that consist of
numerator and denominator.Make a function that
accepts two fractions and returns their product.
-To show the passing of complete structure by call
by reference method
-To show how to initialize the members of the union

10. DATA FILES 175-178

A. SEQUENTIAL CONTROL PROGRAMS


1. Program to add two numbers.
#include<stdio.h>
#include<conio.h>
void main ()
{ int a,b,result;
clrscr();
printf("\nEnter first number");
scanf("%d",&a);
printf("\nEnter second number");
scanf("%d",&b);
result=a+b;
printf("\nSum=%d",result);
}

Output:
Enter first number 60
Enter second number 80
Sum=140

2. Program to calculate total marks and percentage of a student in five


subjects.
#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c,d,e,result;
float percentage;
clrscr();
printf("\nEnter marks in first subject");
scanf("%d",&a);
printf("\nEnter marks in second subject");
scanf("%d",&b);
printf("\nEnter marks in third subject");
scanf("%d",&c);
printf("\nEnter marks in fourth subject");
scanf("%d",&d);
printf("\nEnter marks in fifth subject");
scanf("%d",&e);
result=a+b+c+d+e;
printf("\nTotal=%d",result);
percentage=(a+b+c+d+e)/500.0*100;
printf("\nPercentage=%f",percentage);
}

Output:
Enter marks in first subject 50
Enter marks in second subject 70
Enter marks in third subject 60
Enter marks in fourth subject 80
Enter marks in fifth subject 95
Total=355
Percentage=71.000000

3. Program to convert distance from kilometre to meter.


#include<stdio.h>
#include<conio.h>
void main()
{ float km,m;
clrscr();
printf("\nEnter distance in kilometer");
scanf("%f",&km);
m=km*1000;
printf("\nDistance in meters is %f",m);
}

Output:
Enter distance in kilometer 50
Distance in meters is 50000.000000

4.Program to calculate total salary of an employee.


#include<stdio.h>
#include<conio.h>
void main()
{ int bs;
float da,hra,ts;
clrscr();
printf("\Enter the basic salary of the employee");
scanf("%d",&bs);
da=0.2*bs;
printf("\n%f",da);
hra=0.3*bs;
printf("\n%f",hra);
ts=bs+da+hra;
printf("\nTotal salary=%f",ts);
}

Output:
Enter the basic salary of the employee 5000
1000.000000
1500.000000
Total salary=7500.000000

5. Program to convert temperature from F˚ to C˚.


#include<stdio.h>
#include<conio.h>
void main()
{ float c,f;
clrscr();
printf("\nEnter temp in farenheit");
scanf("%f",&f);
c=5/0.9*(f-32);
printf("\nTemp in centigrade=%f",c);
}
Output:
Enter temp in farenheit 59
Temp in centigrade=150.000000

6.Program to convert temperature from C˚ to F˚.


#include<stdio.h>
#include<conio.h>
void main()
{ float f,c;
clrscr();
printf("\nEnter temp in centigrade");
scanf("%f",&c);
f=9.0/5*(c+32);
printf("\nTemp in ferenheit=%f",f);
}

Output:
Enter temp in centigrade 90
Temp in ferenheit=219.600006

7.Program to calculate area , perimeter of a rectangle.


#include<stdio.h>
#include<conio.h>
void main()
{ int l,b,area,perimeter;
clrscr();
printf("\nEnter length");
scanf("%d",&l);
printf("\Enter breadth");
scanf("%d",&b);
area=l*b;
printf("\nArea=%d",area);
perimeter=2*(l+b);
printf("\nPerimeter=%d",perimeter);
}

Output:
Enter length 60
Enter breadth 20
Area=1200
Perimeter=160

8.Program to calculate area, circumference of a circle.


#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main ()
{ float r,area,circumference;
clrscr();
printf("\nEnter radius");
scanf("%f",&r);
area=PI*r*r;
printf("\Area=%f",area);
circumference=2*PI*r;
printf("\nCircumference=%f",circumference);
}

Output:
Enter radius 67
Area=14095.459961
Circumference=420.760010

9.Program to swap number using third variable.


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nEnter the value of a");
scanf("%d",&a);
printf("\nEnter the value of b");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("\na=%d",a);
printf("\nb=%d",b);
}

Output:
Enter the value of a6
Enter the value of b7
a=7
b=6

10.Program to swap number without using third variable.


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nEnter the value of a");
scanf("%d",&a);
printf("\nEnter the value of b");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("\na=%d",a);
printf("\nb=%d",b);
}

Output:
Enter the value of a 40
Enter the value of b 79
a=79
b=40

11.Program to calculate simple interest.


#include<stdio.h>
#include<conio.h>
void main()
{ float p,r,t,si;
clrscr();
printf("\nEnter principal");
scanf("%f",&p);
printf("\nEnter rate");
scanf("%f",&r);
printf("\nEnter time");
scanf("%f",&t);
si=(p*r*t)/100;
printf("\nSi=%f",si);
}

Output:
Enter principal 500
Enter rate 150
Enter time 40
Si=30000.000000

12.Program to convert decimal integer into octal and hexadecimal.


#include<stdio.h>
#include<conio.h>
void main()
{
int d;
clrscr();
printf("\nEnter a decimal integer");
scanf("%d",&d);
printf("\nOctal representation=%o",d);
printf("\nHexadecimal representation=%x",d);
}

Output:
Enter a decimal integer 3.44
Octal representation=3
Hexadecimal representation=3

13.Program to add two hexadecimal number.


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
printf("enter two numbers: ");
scanf("%x%x",&a,&b);
sum=a+b;
printf("\n sum= %x",sum);
}

Output:
enter two numbers: 56
78

sum= ce

14.Program to show the use of width option with scanf().


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nEnter the value of a,b,c");
scanf("%2d%2d%2d",&a,&b,&c);
printf("a=%d,b=%d,c=%d",a,b,c);
}

Output:
Enter the value of a,b,c
6
6
5
a=6,b=6,c=5

15.Program to show the use of the flag and width option with printf().
#include<stdio.h>
#include<conio.h>
void main()
{ int a=4;
clrscr();
printf(" Value of a: %03d");
}

Output:
Value of a: 004

16.Program to input and display a character using


getch(),getche(),getchar(),putch() and putchar().
#include<stdio.h>
#include<conio.h>
void main()
{
char value,ch;
clrscr();
printf("\nEnter a character:");
value=getch();
printf("\nEntered value is:%c",value);
printf("\nEnter a character:");
value=getche();
printf("\nEntered value is %c",value);
printf("\nEnter a character:");
printf("\nEnter a character:");
#include<stdio.h>
#include<conio.h>
void main()
{
char value,ch;
clrscr();
printf("\nEnter a character:");
value=getch();
printf("\nEntered value is:%c",value);
printf("\nEnter a character:");
value=getche();
printf("\nEntered value is %c",value);
printf("\nEnter a character:");
printf("\nEnter a character:");
value=getchar();
printf("\nEntered value is:%c",value);
printf("\nEnter a character:");
value =putch(ch);
printf("\nEntered value is:%c",value);
printf("\nEnter a character:");
value=putchar(ch);
printf("\nEntered value is:%c",value);
}

Output:
Enter a character:
Entered value is:a
Enter a character:b
Entered value is b
Enter a character:
Enter a character:c
Entered value is:c
Enter a character:*
Entered value is:*
Enter a character:*•
Entered value is:*

B.SELECTION CONTROL STATEMENTS

1.Program to check numbers is positive using simple if statement.


#include<stdio.h>
#include<conio.h>
void main()
{ int a;
clrscr();
printf("\nEnter a number");
scanf("%d",&a);
if(a>=0)
printf("%d is positive number");
}

Output:
Enter a number: 4
4 is positive number

2.Program to check number is positive or negative using if-else.


#include<stdio.h>
#include<conio.h>
void main()
{ int a;
clrscr();
printf("\nEnter a number");
scanf("%d",&a);
if(a>=0)
printf("%d is positive number");
else
printf("%d is negative number");
}

Output:
Enter a number -6
-6 is negative number

3.Program to find largest of two number.


#include<stdio.h>
#include<conio.h>
void main()
{ int a,b;
clrscr();
printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
if (a>b)
printf("%d is largest",a);
else
printf("%d is largest",b);
}

Output:
Enter two numbers 50 98
98 is largest

4.Program to find smallest of two numbers.


#include <stdio.h>
#include<conio.h>
void main()
{ int a,b;
clrscr();
printf("\nEnter two numbers");
scanf("%d,%d,",&a,&b);
if(a<b)
printf("%d is smallest",a);
else
printf("%d is smallest",b);
}

Output:
Enter two numbers 50 69
50 is smallest
5.Program to check whether number is even or odd.
#include <stdio.h>
#include<conio.h>
void main()
{ int a;
clrscr();
printf("\nEnter one number");
scanf("%d",&a);
if (a%2==0)
printf("even number");
else
printf("odd number");
}

Output:
Enter one number 70
even number

6. Program to whether year is leap or not leap.


#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("enter a year");
scanf("%d",&year);
if(year%400==0)
printf("year is leap");
else if(year%100==0)
printf("year is not leap");
else if(year%4==0)
printf("year is leap");
else
printf("year is not leap");

Output:
enter a year 2016
year is leap

7. Program to find greatest of three numbers using nested if else.


#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c;
clrscr();
printf("\nEnter three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{ if(a>c)
printf("%d is greatest",a);
else
printf("%d is greatest",c);
}
else
{ if(b>c)
printf("%d is largest",b);
else
printf("%d is largest",c);
}
}

Output:
Enter three numbers 50 70 90
90 is largest

8.Program to input cost price and selling price of an item and then determine
whether profit,lovss or no profit,no loss.
#include<stdio.h>
#include<conio.h>
void main()
{ int cp,sp,amt;
clrscr();
printf("\nEnter cost price");
scanf("%d",&cp);
printf("\nEnter selling price");
scanf("%d",&sp);
if(sp>cp)
{
amt=sp-cp;
printf("\nProfit=%d",amt);
}
else if(cp>sp)
{
amt=cp-sp;
printf("\nLoss=%d",amt);
}
else
{ printf("\No profit no loss");
}
}

Output:
Enter cost price 350
Enter selling price 500
Profit=150

9.Program to find smallest of three numbers using nested if-else.


#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c;
clrscr();
printf("\nEnter three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a<b)
{
if (a<c)
printf("%d is smallest",a);
else
printf("%d is smallest",b);
}
else
{
if (b<c)
printf("%d is smallest",b);

else
printf("%d is smallest",c);
}
}
Output:

Enter three numbers6 7 8


6 is smallest

10.Program to check number is positive, negitive or zero using nested if -else.


#include<stdio.h>
#include<conio.h>
void main()
{ int a;
clrscr();
printf("\nenter a number");
scanf("%d",&a);
if ((a>0))
printf("%d is positive number");
else
printf("%d is negitive number");
}

Output:
Enter a number -39
-39 is negitive number

11.Program to find greatest of three numbers using else if ladder.


#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c;
clrscr();
printf("\nEnter first number");
scanf("%d",&a);
printf("\nEnter second number");
scanf("%d",&b);
printf("\nEnter third number");
scanf("%d",&c);
if((a>b) && (a>c))
printf("%d is largest",a);
else if(b>c)
printf("%d is largest",b);
else
printf("%d is largest ",c);
}

Output:
Enter first number 50
Enter second number 99
Enter third number 103
103 is largest

12.Program to calculate value of y when value of x is input by the user.


#include<stdio.h>
#include<conio.h>
void main ()
{ int x,y;
clrscr();
printf("\nEnter the value of x");
scanf("%d",&x);
if(x>0)
{ y=2*x;
printf("\nValue %d",y);
}
else if (x==0)
{ y=1;
printf("\nValue %d",y);
}
else
{ y=-2*x;
printf("\nValue %d",y);
}
}

Output:
Enter the value of x 33
Value 66

13.Program to check whether roots of quadric equation are


real,equal,imaginary .
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{ int a,b,c,d;
float r1,r2;
clrscr();
printf("\nEnter the value of a");
scanf("%d",&a);
printf("\nEnter the value of b");
scanf("%d",&b);
printf("\nEnter the value of c");
scanf("%d",&c);
d=(b*b)-(4*a*c);
printf("d=%d",&d);
if(d>0)
{ r1=(-b+sqrt(d))/2*a;
r2=(-b+sqrt(d))/2*a;
printf("\nReal roots are r1=%f,r2=%f",r1,r2);
}
else if(d==0)
{ r1=-b/2.0*a;
r2=-b/2.0*a;
printf("\nEqual roots are r1=%f,r2=%f",r1,r2);
}
else
printf("\nImaginarey roots");
}

Output:
Enter the value of a 40
Enter the value of b 30
Enter the value of c 70
d=-18
Imaginarey roots

14.Program to convert uppercase to lowercase alphabet and vice versa.


#include<stdio.h>
#include<conio.h>
void main()
{
char alpha;
clrscr();
printf("\nEnter any character");
scanf("%c",&alpha);
if(alpha>='a'&&alpha<='z')
{ alpha=alpha-32;
printf("\nUpper case alphabet=%c",alpha);
}
else if(alpha>='A'&&alpha<='Z')
{ alpha=alpha+32;
printf("\nLower case alphabet=%c",alpha);
}
else
printf("\nIt is not an alphabet");
}

Output:
Enter any character d
Uppercase alphabet=D

15.Program to determine whether the entered character is uppercase,


lowercase alphabet, a digit or a special symbol.
#include<stdio.h>

#include<conio.h>

void main()

char ch;

clrscr();

printf("\nEnter a character");

scanf("%c",&ch);

if(ch>='A'&&ch<='Z')

printf("\nUpper case character");


else if(ch>='a'&&ch<='z')

printf("\nLower case character");

else if(ch>='0'&&ch<='9')

printf("\nDigit");

else

printf("\nSpecial symbol");

Output:
Enter a character 7

Digit

16. To find largest of two numbers.


#include<stdio.h>
#include<conio.h>
void main ()
{ int a,b;
clrscr();
printf("\nEnter first number");
scanf("%d",&a);
printf("\nEnter second number");
scanf("%d",&b);
(a>b)?printf("%d is largest",a):printf("%d is largest",b);
}
OUTPUT:

Enter first number 50


Enter second number 33
50 is largest

17.To find smallest of two numbers using


#include<stdio.h>
void main()
{ int a,b;
clrscr();
printf("\nEnter first number");
scanf("%d",&a);
printf("\nEnter second number");
scanf("%d",&b);
(a<b)?printf("%d is smallest",a):printf("%d is smallest",b);
}

Output:

Enter first number 99


Enter second number 76
76 is smallest

18.To find largest of three numbers.


#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c;
clrscr();
printf("\nEnter three numbers");
scanf("%d%d%d",&a,&b,&c);
((a>b)&&(a>c))?printf("%d is largest",a):(b>c)?printf("%d is largest",b):printf("%d is
largest",c);
}

Output:
Enter three numbers 100 50 22
100 is largest

19.To find smallest of three numbers.


#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c;
clrscr();
printf("\Enter three numbers");
scanf("%d%d%d",&a,&b,&c);
((a<b)&&(a,c))?printf("%d is smallest",a):(b<c)?printf("%d is smallest",b):printf("%d
is smallest",c);

Output:
Enter three numbers 70 65 33
33 is smallest

20. To check whether number is even or odd.


#include<stdio.h>
#include<conio.h>
void main()
{ int n;
clrscr();
printf("\nEnter a number");
scanf("%d",&n);
(n%2==0)?printf("%d is even",n):printf("%d is odd",n);
}

Output:
Enter a number 73
73 is odd

21.To check whether the number is positive,negative or zero.


#include<stdio.h>
#include<conio.h>
void main()
{ int a;
clrscr();
printf("\nEnter a number");
scanf("%d",&a);
(a>0)?printf("%d is a positive number"):(a<0)?printf("%d is a negative number"):printf("%d
is equal to zero");
}

Output:
Enter a number 302
302 is a positive number

22.To check whether the year is leap or not.


#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter the value of year: ");
scanf("%d",&year);
if(year%400==0)
printf("Leap year");
else if(year%100==0)
printf("Not leap year");
else if (year%4==0)
printf("Leap year");
else
printf("Not leap year");
}
OUTPUT
Enter the value of year: 2009
Not leap year

23.To display bytes of memory occupied by different data types using sizeof
operator.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Memory occupied by int=%d",sizeof (int));
printf("\nMemory occupied by float=%d",sizeof (float));
printf("\nMemory occupied by char=%d",sizeof (char));
printf("\nMemory occupied by double=%d",sizeof (double));
printf("\nMemory oocupied by short int=%d", sizeof (short int));
printf("\nMemory occupied by long int=%d",sizeof (long int));
printf("\nMemory occupied by long double=%d",sizeof (long double));
printf("\nMemory occupied by unsigned char=%d",sizeof (unsigned char));
}
OUTPUT
Memory occupied by int=2
Memory occupied by float=4
Memory occupied by char=1
Memory occupied by double=8
Memory oocupied by short int=2
Memory occupied by long int=4
Memory occupied by long double=10
Memory occupied by unsigned char=1

C. ITERATION PROGRAMS

1.Print hello five times using while loop.


#include<stdio.h>
#include<conio.h>
void main()
{ int i=1;
clrscr();
while(i<=5)
{
printf("\nHello");
i++;
}
}

Output
Hello
Hello
Hello
Hello
Hello

2.To display first n natural numbers and their sum.


#include<stdio.h>
void main ()
{ int i=1,n,sum=0;
clrscr();
printf("\nEnter the value of n till u want to display natural numbers");
scanf("%d",&n);
printf("\n %d natural numbers are \n",n);
while(i<=n)
{ printf("\n%d",i);
sum=sum+i;
i++;
}
printf("\n sum=%d",sum);
}

Output:
Enter the value of n till u want to display natural numbers 20
20 natural numbers are
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sum=210

3.To display first n natural even numbers and thier sum.


#include<stdio.h>
#include<conio.h>
void main ()
{ int i=2,n,sum=0;
clrscr();
printf("\nEnter the value of n till which you want to display even natural numbers");
scanf("%d",&n);
printf("\n%d even natural numbers are \n",n);
while(i<=2*n)
{ printf("\n%d",i);
sum=sum+i;
i=i+2;
}
printf("\n sum=%d",sum);
}

Output:
Enter the value of n till which you want to display even natural numbers 10
10 even natural numbers are
2
4
6
8
10
12
14
16
18
20
sum=110

4.To display first n natural odd number and their sum.


#include<stdio.h>
#include<conio.h>
void main()
{ int n;
clrscr();
printf("\nEnter a number");
scanf("%d",&n);
(n%2==0)?printf("%dis even",n):printf("%d is odd",n);
}

Output:
Enter a number 77
77 is odd

5. To calculate percentage and grade of n number of students.


#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,m5,total,i,n;
float percentage;
clrscr();
printf("\nEnter numbers of students in class");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter marks of first subject");
scanf("%d",&m1);
printf("\nEnter marks of second subject");
scanf("%d",&m2);
printf("\nEnter marks of third subject");
scanf("%d",&m3);
printf("\nEnter marks of fourth subject");
scanf("%d",&m4);
printf("\nEnter marks of fifth subject");
scanf("%d",&m5);
total=m1+m2+m3+m4+m5;
printf("\nTotal=%d",total);
percentage=(total/500.0)*100;
printf("\nPercentage=%f",percentage);
if(percentage>80)
printf("\nA grade");
else if(percentage>=60&&percentage<80)
printf("\nB garde");
else if(percentage>=40&&percentage<60)
printf("\nC grade");
else
printf("\nFail");
}

OUTPUT
Enter marks of first subject91
Enter marks of second subject92

Enter marks of third subject93

Enter marks of fourth subject94

Enter marks of fifth subject95

Total=465
Percentage=93.000000
A grade
Enter marks of first subject96

Enter marks of second subject97

Enter marks of third subject98

Enter marks of fourth subject99


Enter marks of fifth subject100

Total=490
Percentage=98.000000
A grade

6. To print hello five times using do while loop.


#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
do
{ printf("\nHello");
i++;
} while(i<=5);
getch();
}

output:
Hello
Hello
Hello
Hello
Hello

7.To print hello five times using for loop.


#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=5;i++)
{ printf("\nHello");
}
}

output:
Hello
Hello
Hello
Hello
Hello

8. To display factorial of a number.


#include<stdio.h>
#include<conio.h>
void main()
{ int i,n;
long int fact=1;
clrscr();
printf("\n enter a number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ fact=fact*i;
}
printf("\n factorial of %d=%ld",n,fact);
}

Output:
enter a number 30
factorial of 30=1409286144

9. To calculate sum of a digit of a number using while loop.


#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n;
clrscr();
printf("\n enter the value of n till which you want to display natural numbers: ");
scanf("%d",&n);
printf("\n %d natural numbers \n",n);
do
{
printf("\n %d ",i);
i++;
}while(i<=n);
}

OUTPUT
enter the value of n till which you want to display natural numbers: 4

4 natural numbers
1
2
3
4

11. To print numeric pattern.


#include<stdio.h>
#include<conio.h>
void main ()
{
int i,j,n;
clrscr();
printf("Enter the value of n\n");
scanf("%d",&n);
printf("The pattern is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
}
OUTPUT
Enter the value of n
5
The pattern is
1
12
123
1234
12345

12.To display reverse list using decrement for loop.


#include <stdio.h>
void main()

int i;

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

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

return 0;

Output:
10

13.To calculate reverse of a number using while loop.


#include<stdio.h>
#include<conio.h>
void main ()
{
int i=5;
clrscr();
while(i>=1)
{
printf("\n%d",i);
i--;
}
}
OUTPUT
5
4
3
2
1

14.To check number is palindrome or not.


#include<stdio.h>
#include<conio.h>
void main ()
{
int n,n1,rem,rev=0;
clrscr();
printf("Enter value of n");
scanf("%d",&n);
n1=n;
while(n>0)
{
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}
printf("\nreverse=%d",rev);
if(rev==n1)
printf("\nNumbers are palindrome");
else
printf("\nNumbers are not palindrome");
}
OUTPUT
Enter value of n101

reverse=101
Numbers are palindrome

15.To print table of a number.


#include<stdio.h>
#include<conio.h>
void main ()
{ int i=1,n,table;
clrscr();
printf("\nEnter the number",n);
scanf("%d",&n);
printf("\nTable of %d\n",n);
for(i=1;i<=10;i++)
{
table=n*i;
printf("\n%d * %d=%d",n,i,table);
}
}
Output
Enter the number 3
Table of 3
3 * 1=3
3 * 2=6
3 * 3=9
3 * 4=12
3 * 5=15
3 * 6=18
3 * 7=21
3 * 8=24
3 * 9=27
3 * 10=30

16.To display Fibonacci series.


#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,i,n;
clrscr();
printf(" enter number of terms");
scanf("%d",&n);
printf("fabonic series is");
if(n==1)
printf("%d",a);
else
{
printf(" %d %d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf(" %d",c);
a=b;
b=c;
}
}
}
Output
enter number of terms 6
fabonic series is 0 1 1 2 3 5

17.To print ASCII values and their equivalent character.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
int i;
clrscr();
for(i=0;i<=255;i++)
{
printf("\nASCII values=%d Character=%c",i,i);
delay(50);
}
}
Output
ASCII values=231 Character=ç
ASCII values=232 Character=è
ASCII values=233 Character=é
ASCII values=234 Character=ê
ASCII values=235 Character=ë
ASCII values=236 Character=ì
ASCII values=237 Character=í
ASCII values=238 Character=î
ASCII values=239 Character=ï
ASCII values=240 Character=ð
ASCII values=241 Character=ñ
ASCII values=242 Character=ò
ASCII values=243 Character=ó
ASCII values=244 Character=ô
ASCII values=245 Character=õ
ASCII values=246 Character=ö
ASCII values=247 Character=÷
ASCII values=248 Character=ø
ASCII values=249 Character=ù
ASCII values=250 Character=ú
ASCII values=251 Character=û
ASCII values=252 Character=ü
ASCII values=253 Character=ý
ASCII values=254 Character=þ
ASCII values=255 Character=ÿ

18. To print Armstrong number.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,arm,n,d;
clrscr();
printf("\n Armstrong numbers are");
for(i=100;i<=900;i++)
{
arm=0;
n=i;
while(n>0)
{
d=n%10;
arm=arm+(d*d*d);
n=n/10;
}
if(arm==i)
printf("\n %d",i);
}
}
OUTPUT
Armstrong numbers are
153
370
371
407

19. Program to print sum of series 1/1!+2/2!+3/3!+.......n/n!


#include<stdio.h>
#include<conio.h>
void main ()
{
int i=1,j,n;
long int fact=1;
float sum=0.0;
clrscr();
printf("\nEnter a number");
scanf("%d",&n);
while(i<=n)
{
for(j=1;j<=i;j++)
{
fact=fact*j;
}
if(i<n)
printf("%d/%d!+",i,i);
else
printf("%d/%d!",i,i);
sum=sum+(float)i/fact;
i++;
}
printf("=%f",sum);
}
OUTPUT
Enter a number3
1/1!+2/2!+3/3!=2.250000

20. Program to print the series of 1(2)+2(2)+........n(2).


#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n,sum=0;
clrscr();
printf("\nEnter the value of n tell which you want to display natural numbers");
scanf("%d",&n);
printf("\n%d natural numbers are \n",n);
while(i<=n)
{
if(i==n)
printf("%d",i*i);
else
printf("%d+",i*i);
sum=sum+(i*i);
i++;
}
printf("=%d",sum);
}

OUTPUT

Enter the value of n tell which you want to display natural numbers8

8 natural numbers are


1+4+9+16+25+36+49+64=204

21. Program to print sum of series 1/1+1/2+1/3+.......+1/n.

#include<stdio.h>
#include<conio.h>
void main ()
{
int i=1,j,n;
long int fact=1;
float sum=0.0;
clrscr();
printf("\nEnter a number");
scanf("%d",&n);
while(i<=n)
{
for(j=1;j<=i;j++)
{
fact=fact*j;
}
if(i<n)
printf("%d/%d!+",i,i);
else
printf("%d/%d!",i,i);
sum=sum+(float)i/fact;
i++;
}
printf("=%f",sum);
}
OUTPUT
Enter a number3
1/1!+2/2!+3/3!=2.250000

22.To print pattern *.


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

getch();

Output:

*****

****

***
**

23.To display all 3 digits well ordered numbers less than 200.

#include<stdio.h>
#include<conio.h>
void main()
{ int i,n,a,rem1,rem2,rem3,j;
clrscr();
printf("\n 3digit well order no less than 200 are");
for(j=100;j<=200;j++)
{ a=j;
i=j;
rem1=i%10;
i=i/10;
rem2=i%10;
i=i/10;
rem3=i%10;
i=i/10;
if(rem1>rem2&&rem2>rem3)
printf("%d",a);
}
getch();
}

OUTPUT:
3 digit welloder no less than 200:
126
127
128
129
134
135
136
137
138
139
145
146
147
148
149
156
157
158
159
167
168
169
178
179
189

24.To show use of break statement in for loop.

#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
clrscr();
for(i=1;i<=3;i++)
{
if(i==2)
{
break;
}
printf("\n%d",i);
}
}
OUTPUT
1

25.To show use of break in nested for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf("\n%d%d",i,j);
}
}
}
OUTPUT
11
12
21
22

26.To show the use of continue statements.

#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
clrscr();
for(i=1;i<=3;i++)
{
if(i==2)
{
continue;
}
printf("\n%d",i);
}
}
OUTPUT
1
3

27.To print all prime numbers from 1 to 500.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int n,i;
clrscr();
printf("%d prime numbers are\n",n);
for(n=1;n<=500;n++)
{
i=2;
while(i<=n-1)
{
if(n%i==0)
{
break;
}
i++;
}

if(i==n)
{
printf("\n %d",n);
delay(50);
}
}

}
Output
353
359
367
373
379
383
389
397
401
409
419
421
431
433
439
443
449
457
461
463
467
479
487
491
499

28.To convet number decimal into binary form.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,d,bin=0,i=0;
clrscr();
printf(" \n enter a decimal number");
scanf("%d",&n);
while(n>0)
{
d=n%2;
bin=bin+d*pow(10,i);
n=n/2;
i++;
}
printf("\n Binary number=%d",bin);
}
OUTPUT
enter a decimal number 23

Binary number=10111

D. Switch Case Programs


1.To print day of week when day number is input by user.

#include<stdio.h>

#include<conio.h>

void main()

{ int day;

clrscr();
printf("\nEnter a day number between 1 and 7");

scanf("%d",&day);

switch(day)

{ case 1:printf("Monday");

break;

case 2:printf("Tuesday");

break;

case 3:printf("Wednesday");

break;

case 4:printf("Thursday");

break;

case 5:printf("Friday");

break;

case 6:printf("Saturday");

break;

case 7:printf("Sunday");

break;

default:printf("Invalid number");

}
Output:

Enter a day number between 1 and 7 6 \

Saturday

2. A menu driven program to perform addition , subtraction ,

multiplication , divide of two number.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
int a,b,choice;
float result;
clrscr();
printf("\nMenu");
printf("\n1Addition \n2Subtraction \n3Multiplication \n4Division \n5Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
result=a+b;
printf("\nSum of %d and %d=%f",a,b,result);
break;
case 2:printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
result=a-b;
printf("\nDifference between %d and %d=%f",a,b,result);
break;
case 3:printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
result=a*b;
printf("\nResult=%f",result);
break;
case 4:printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
if(b==0)
printf("Divide by zero error");
else
{
result=(float)a/b;
printf("\nDivision of %d and %d=%f",a,b,result);
break;
}
case 5:exit(0);
default:printf("\nInvalid choice");
}
}
OUTPUT:

Menu
1Addition
2Subtraction
3Multiplication
4Division
5Exit
Enter your choice: 4
Enter two numbers10 5

Division of 10 and 5=2.000000

E. Goto Statements

1.Program to show the use of goto statement (forward jump).

#include<stdio.h>

#include<conio.h>

void main()

int i;

clrscr();

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

if(i==2)

goto end;

else

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

}
end:printf("\nEnd of program");

Output:

End of program

2.Program to calculate square root of a positive number using goto

statement (backward jump).

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

int x;

float y;

clrscr();

input:printf("enter a positive number");

scanf("%d",&x);

if(x<0)

goto input;

else
{

y=sqrt(x);

printf("\nsquare root of %d=%f",x,y);

Output:

enter a positive number 3

square root of 3=1.732051

F. Function Programs.

1. Program to add two numbers .

#include<stdio.h>
#include<conio.h>
int calculate(int,int);
void main()
{
int a,b,sum;
clrscr();
printf(" \n enter two numbers");
scanf(" %d %d",&a,&b);
sum=calculate(a,b);
printf("\n sum of %d and %d=%d",a,b,sum);
}
int calculate(int x,int y)
{
int z;
z=x+y;
return(z);
}
OUTPUT:
enter two numbers 8 7

sum of 8 and 7=15

2. Program to determine whether a number is even or odd using function.

#include<stdio.h>

#include<conio.h>

void even_odd(int);

void main()

int n;

clrscr();

printf("Enter a number");

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

void even_odd(int n)

if(n%2==0)

printf("%d is even number",n);

else

printf("%d is odd number",n);

Output:

Enter a number7

7 is odd number

3. Program to print a message in which function does not take any

argument and does not return any value.

#include<stdio.h>

#include<conio.h>

void message1();

void message2();
void main()

clrscr();

message1();

message2();

message1();

void message1()

printf("\nHello");

void message2()

printf("\nBye");

Output:

Hello
Bye

Hello

4. Program to swap two numbers using call by value method.

#include<stdio.h>
#include<conio.h>
void swap (int,int);
void main()
{
int a,b;
clrscr();
printf("\n enter two numbers");
scanf("%d %d",&a,&b);
swap(a,b);
printf("\n values of variables after calling swap function a=%d,b=%d",a,b);
}
void swap(int a,int b)
{
int c;
c=a;
a=b;
b=c;
printf("\n values in swap function after swapping a=%d.b=%d",a,b);
}
Output:
enter two numbers 26 28
values in swap function after swapping a=28.b=26
values of variables after calling swap function a=26,b=28
5. Program to swap two numbers using call by reference method.

#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a,b;
clrscr();
printf("\n enter two numbers");
scanf("%d %d",&a,&b);
swap(&a,&b);
printf("\n values after calling swap function a=%d,b=%d", a,b);
}
void swap (int *x, int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
printf("\n values in swap function are %d %d",*x,*y);
}
Output:
enter two numbers 12 23
values in swap function are 23 12
values after calling swap function a=23,b=12
6. Program to calculate sum and difference of two number using

call by reference method ( returning multiple values from a

function).

#include<stdio.h>
#include<conio.h>
void operations (int,int,int*,int*);
void main()
{
int a,b,sum,diff;
clrscr();
printf(" enter two numbers");
scanf("%d %d",&a,&b);
operations(a,b,&sum,&diff);
printf("\n sum of %d and %d=%d",a,b,sum);
printf("\n diff of %d and %d =%d",a,b,diff);
}
void operations(int a,int b,int *s,int *d)
{
*s=a+b;
*d=a-b;
}
Output:
enter two numbers 3 2
sum of 3 and 2=5
diff of 3 and 2 =1

7. Program to find factorial of a number using recursion.


#include<stdio.h>
#include<conio.h>
long int fact_rec(int);
void main()
{
int n;
long int k;
clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
k=fact_rec(n);
printf("\n Factorial %d=%ld",n,k);
}
long int fact_rec(int n)
{
int i=1;
long int k=1;
for(i=1;i<=n;i++)
k*=i;
return(k);
}
Output:
Enter a number: 5
Factorial 5=120

8.Program to find sum of first n natural numbers using recursion.

#include<stdio.h>
#include<conio.h>
int natural(int);
void main()
{
int n,sum;
clrscr();
printf("\n enter value of n");
scanf("%d", &n);
sum=natural(n);
printf("\n sum of first %d natural numbers=%d", n,sum);
}
int natural (int n)
{
static int s;
if(n==0)
return(s);
else
{
s=s+n;
return(natural (n-1));
}
}

OUTPUT:
enter value of n 4
sum of first 4 natural numbers=10

9. Program to display Fibonacci series using recursion.

#include<stdio.h>
#include<conio.h>
int fibo(int);
void main()
{
int n,i;
clrscr();
printf("\n enter number of terms");
scanf("%d",&n);
printf("\n fibonacci series is");
for(i=1;i<=n;i++)
{
printf("\n %d", fibo(i));
}
}
int fibo(int a)
{
if(a==1)
return(0);
else if(a==2)
return(1);
else
return(fibo(a-1)+fibo(a-2));
}
Output:
enter number of terms 8
fibonacci series is
0
1
1
2
3
5
8
13
10. Program to find sum of digits of a number using recursion.

#include<stdio.h>
#include<conio.h>
int digit(int);
void main()
{
int n,sum;
clrscr();
printf("\n enter a number");
scanf("%d",&n);
sum=digit(n);
printf("\n sum of digits of %d =%d",n,sum);
}
int digit(int n)
{
int d;
static int s;
if(n==0)
return(s);
else
{
d=n%10;
s=s+d;
return(digit(n/10));
}
}
Output:
enter a number123
sum of digit of 123 =6

11. Program to find HCF and LCM of two numbers using

recursion.

#include<stdio.h>
#include<conio.h>
int calculate(int,int);
void main()
{
int a,b,hcf,lcm;
clrscr();
printf("\n enter two numbers");
scanf("%d %d",&a,&b);
hcf=calculate(a,b);
lcm=a*b/hcf;
printf("\n hcf=%d lcm=%d", hcf,lcm);
}
int calculate(int a, int b)
{
if(a%b==0)
return(b);
else
return(calculate(b,a%b));
}
Output:
enter two numbers 6 8
hcf=2 lcm=24

G. ARRAYS

1. Program to input elements of an array and then display them

on the screen.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n;
clrscr();
printf("\n Enter number of array elements ");
scanf("%d",&n);
printf("\n Enter %d elements \n",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
printf("\n Enter array elements are \n");
for(i=0;i<=n-1;i++)
printf("\n %d",a[i]);
}
Output:
Enter number of array elements 4
Enter 4 elements
22
88
99
55
Enter array elements are

22
88
99
55

2.Program to input array and then calculate sum and average.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n,sum=0;
float avg;
clrscr();
printf("\n Enter the number of array elements ");
scanf(" %d ",&n);
printf("\n enter %d elements ",n);
for(i=0;i<=n-1;i++)
{
scanf("%d", &a[i]);
sum=sum+a[i];
}
avg=(float)sum/n;
printf("\n sum=%d avg=%f", sum,avg);
}
Output:
Enter the number of array elements 3 2
enter 3 elements 4 5 6

sum=11 avg=3.666667

3.Program to reverse an array.

#include<stdio.h>

#include<conio.h>

void main()

int a[20],temp[20],i,j,n;

clrscr();

printf("Enter the numbers of elements in array?");

scanf("%d",&n);

printf("Enter %d numbers\n",n);

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

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

for(i=n-1,j=0;i>=0;i--,j++)

temp[j]=a[i];

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

a[i]=temp[i];

printf("Numbers in reverse order are\n");

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

printf("%d\t",a[i]);

getch();

Output:

Enter the numbers of elements in array? 4

Enter 4 numbers

10

20

30

40
Numbers in reverse order are 40 30 20 10

4.Program to input elements in array and then count the number

of positive, negative and zero elements in an array.

#include<stdio.h>

#include<conio.h>

void main()

int i,a[20],n;

int countn=0,countz=0,countp=0;

clrscr();

printf("Enter numbers");

scanf("%d",&n);

printf("Enter %d integer number\n",n);

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

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

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

if (a[i]==0)
countz++;

else if(a[i]>0)

countp++;

else

countn++;

printf("Out of %d number entered %d are +ve numbers\n",n,countp);

printf("Out of %d number entered %d are -ve numbers\n",n,countn);

printf("Out of %d number entered %d are zeros",n,countz);

getch();

Output:

Enter numbers 4

Enter 4 integer number

20

-33
70

Out of 4 number entered 2 are +ve numbers

Out of 4 number entered 1 are -ve numbers

Out of 4 number entered 1 are zeros

5.Program to interchange corresponding elements of two equal

sized array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],b[30],i,n,c;
clrscr();
printf("\n Enter the number of array elements ",n);
scanf("%d",&n);
printf("\n enter %d elements for first array\n ",n);
for(i=0;i<=n-1;i++)
{
scanf("%d", &a[i]);
}
printf("\n enter %d elements for second array \n",n);
for(i=0;i<=n-1;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<=n-1;i++)
{
c=a[i];
a[i]=b[i];
b[i]=c;
}
printf("\n elements of first array after interchange",n);
for(i=0;i<=n-1;i++)
printf("\n %d",a[i]);
printf("\n elements of second array after interchange",n);
for(i=0;i<=n-1;i++)
printf("\n %d",b[i]);
}

Output:
Enter the number of array elements 3

enter 3 elements for first array


11
12
13

enter 3 elements for second array


1
2
3
elements of first array after interchange
1
2
3
elements of second array after interchange
11
12
13

6.Program to find maximum elements from an array.

#include<stdio.h>

#include<conio.h>

void main()

{ int n,i,a[30],max,loc;

clrscr();

printf("\nEnter number of array elements");

scanf("%d",&n);

printf("\nEnter %d elements\n",n);

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

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

max=a[0];

loc=0;

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

if (a[i]>max)
{

max=a[i];

loc=i;

printf("\nMaximum element=%d found at location=%d",max,loc);

Output:

Enter number of array elements 4

Enter 4 elements

10

99

20

72

Maximum element=99 found at location=1

7.Program to find minimum elements from an array.


#include<stdio.h>

#include<conio.h>

void main()

{ int n,i,a[30],min,loc;

clrscr();

printf("\nEnter number of array elements");

scanf("%d",&n);

printf("\nEnter %d elements\n",n);

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

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

min=a[0];

loc=0;

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

if (a[i]<min)

min=a[i];
loc=i;

printf("\nMinimum element=%d found at location=%d",min,loc);

Output:

Enter number of array elements

Enter 5 elements

Minimum element=1 found at location=0


8.Program to search an elements from an array using linear

search.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[30], i, n, loc=-1, item;
clrscr();
printf("\n Enter number of element",n);
scanf("%d",&n);
printf("\n Enter %d elements",n);
for(i=0;i<n-1;i++)
scanf("%d",&a[i]);
printf("\n Enter element to be searched");
scanf("%d",&item);
for(i=0;i<=n-1;i++)
{
if(a[i]==item)
{
loc=i;
printf("\n %d found at location %d",item,loc);
break;
}
}
if(loc==-1)
printf("%d not found in list", item);
}
Output:
Enter number of element 5
Enter 5 elements
15 14 13 12

Enter element to be searched 14


14 found at location 1

9.Program to search an elements from an array using binary

search.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n,item, beg, end, mid,loc;
clrscr();
printf("Enter the number of array elements ");
scanf("%d",&n);
printf("\n enter %d elements ",n);
for(i=0;i<=n-1;i++)
scanf("%d", &a[i]);
printf("\n enter element to be searched ");
scanf("%d",&item);
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(a[mid]==item)
{
printf("\nitem is found at location %d",item,loc);
break;
}
else if(a[mid]<item)
beg=mid+1;
else
end=mid-1;
}
if(beg>end)
printf("\n element is not found");
}
Output:
Enter the number of array elements 3
enter 3 elements
12
34
56

enter element to be searched


34

item is found at location 34

10.Program to sort the elements of an array using bubble sorting.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[30],j,temp;
clrscr();
printf("\n Enter number of elements");
scanf("%d",&n);
printf("\n Enter %d element",n);
for(i=0;i<=n-1;i++)
scanf("%d", &a[i]);
for(i=0;i<=n-2;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n sorted list");
for(i=0;i<=n-1;i++)
printf("\n %d",a[i]);
}
Output:
Enter the number of array elements 3
enter 3 elements
12
34
56

enter element to be searched


34

item is found at location 34

11. Program to insert unsorted array at a specific postion.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[30],loc,item;
clrscr();
printf("\nEnter number of elements");
scanf("%d",&n);
printf("\nEnter %d elements",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
printf("\nEnter item to be inseted");
scanf("%d",&item);
printf("\nEnter location");
scanf("%d",&loc);
for(i=n-1;i>=loc;i--)
{
a[i+1]=a[i];
}
a[loc]=item;
n=n+1;
printf("\nNew array after insertion");
for (i=0;i<=n-1;i++)
printf("\n%d",a[i]);
}
Output:
Enter number of elements 4
Enter 4 elements
6
8
9
2
Enter item to be inseted 10
Enter location 3
New array after insertion
6
8
9
10
2

12.Program to delete an elements from an unsorted elements.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n,item,loc=-1;
clrscr();
printf("\nEnter Number of elements in an array");
scanf("%d",&n);
printf("\nEnter %d elements",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
printf("\nEnter item to be deleted");
scanf("%d",&item);
for(i=0;i<=n-1;i++)
{
if(a[i]==item)
loc=i;
}
if(loc==-1)
printf("item is not present in an array");
else
{
for(i=loc;i<n-1;i++)
a[i]=a[i+1];
n=n-1;
printf("\nNew list after delection");
for(i=0;i<=n-1;i++)
printf("\n %d",a[i]);
}
}

OUTPUT:
Enter Number of elements in an array 4

Enter 4 elements
12
34
56
78

Enter item to be deleted 34


New list after delection
12
56
78

13.Program to find the largest and second largest element from

an array.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[30],first,second;
clrscr();
printf("\nEnter number of elements");
scanf("%d",&n);
printf("\nEnter %d elements",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
first=a[0];
second=a[0];
for(i=1;i<=n-1;i++)
{
if(a[i]>first)
{
second=first;
first=a[i];
}
else if (a[i]>second)
second=a[i];
}
printf("\nFirst largest value=%d",first);
printf("\nSecond largest value=%d",second);
}
OUTPUT
Enter number of elements6
Enter 6 elements
9
10
15
99
153
44
First largest value=153
Second largest value=99

14.Program to add a pointer to an integer.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={10,20,30,40};
int *p;
clrscr();
p=&a[1];
p=p+1;
printf("\n %d",*p);
}
OUTPUT
30

15Program to subtract a pointer from an integer.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={10,20,30,40};
int *p;
clrscr();
p=&a[1];
p=p-1;
printf("\n %d",*p);
}
OUTPUT
10

16.Program to subtract two pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={10,20,30,40};
int *p,*k;
clrscr();
p=&a[1];
k=&a[3];
printf("\n %d",k-p);
}
OUTPUT
2

17. Program to compare two poniters.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={10,20,30,40};
int *p,*k;
clrscr();
p=&a[0];
k=a;
if(p==k)
printf("Both pointers printing at the same elements");
else
printf("\n Pointers are pointing at different element");
}
OUTPUT
Both pointers printing at the same elements
18.Program to pass individual array elements to a function using

call by value method.

#include<stdio.h>
#include<conio.h>
void display(int);
void main()
{
int a[3]={10,20,30};
int i;
clrscr();
for(i=0;i<=2;i++)
display (a[i]);
}
void display(int m)
{
printf("\n %d",m);
}

OUTPUT
10
20
30

19.Program to pass individual array elements to a function using

call by reference method.


#include<stdio.h>
#include<conio.h>
void display(int *);
void main()
{
int a[3]={10,20,30};
int i;
clrscr();
for(i=0;i<=2;i++)
display(&a[i]);
}
void display(int *m)
{
printf("\n %d",*m);
}

OUTPUT
10
20
30

20.Program to pass entire array to a function using call by

reference method.

#include<stdio.h>
#include<conio.h>
void display(int[],int);
void main()
{
int a[3]={10,20,30};
clrscr();
display(a,3);
}
void display(int a[], int n)
{
int i;
for(i=0;i<=n-1;i++)
printf("\n %d",a[i]);
}
OUTPUT
10
20
30

21. Program to show to show the use of array of pointers to

integers.

#include<stdio.h>
#include<conio.h>
void main()
{
int *p[3];
int m=5,n=7,k=3,i;
p[0]=&m;
p[1]=&n;
p[2]=&k;
printf("\n Values are \n");
for(i=0;i<=2;i++)
printf("\n %d",*p[i]);
}
OUTPUT

Values are

5
7
3

► 2D Array

1. Program to input elements of a 2D array and then display

them in a matrix format.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10] [10],m,n,i,j;
clrscr();
printf("\nEnter number of rows and columns");
scanf("%d%d",&m,&n);
printf("\nEnter elements \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nMatrix is \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}

OUTPUT
Enter number of rows and columns 2 2
Enter elements
a[0][0]=6
a[0][1]=5
a[1][0]=9
a[1][1]=3
Matrix is
6 5
9 3

2. Program to add two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10] [10],m,n,i,j,b[10] [10],c[10] [10];
clrscr();
printf("\nEnter number of rows and columns");
scanf("%d%d",&m,&n);
printf("\nEnter elements of first matrix \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter elements of second matrix \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nb[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
}
}

printf("\nSum is \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
}

OUTPUT
Enter elements of first matrix
a[0][0]=6
a[0][1]=3
a[1][0]=1
a[1][1]=2
Enter elements of second matrix
b[0][0]=6
b[0][1]=3
b[1][0]=5
b[1][1]=3
Sum is
12 6
6 5

3. Program to subtract two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10] [10],m,n,i,j,b[10] [10],c[10] [10];
clrscr();
printf("\nEnter number of rows and columns");
scanf("%d%d",&m,&n);
printf("\nEnter elements of first matrix \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter elements of second matrix \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\nb[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
}
}

printf("\nDifference is \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j]-b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
OUTPUT:
Enter elements of first matrix
a[0][0]=6
a[0][1]=3
a[1][0]=5
a[1][1]=9
Enter elements of second matrix
b[0][0]=4
b[0][1]=6
b[1][0]=3
b[1][1]=2
Difference is
2 -3
2 7

4. Program to pass 2D array to function.

#include<stdio.h>
#include<conio.h>
void display(int[][3],int ,int);
void main()
{
int a[2][3]={{10,20,30},
{40,50,60}
};
clrscr();
display(a,2,3);
}
void display(int a[][3],int m,int n)
{
int i,j;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}

OUTPUT
10 20 30
40 50 60

5. Program to find product of matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],m1,n1,m2,n2,i,j,k;
int c[10][10]={0};
clrscr();
printf("\nEnter number of rows and columns of first matrix");
scanf("%d%d",&m1,&n1);
printf("\nEnter number of rows and columns of second matrix");
scanf("%d%d",&m2,&n2);
if(n1!=m2)
printf("\nMultiplication is not possible");
else
{
printf("\nEnter elements of first matrix \n");
for(i=0;i<=m1-1;i++)
{
for(j=0;j<=n1-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter elements of second matrix \n");
for(i=0;i<=m2-1;i++)
{
for(j=0;j<=n2-1;j++)
{
printf("\nb[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<=m1-1;i++)
{
for(j=0;j<=n1-1;j++)
{
for(k=0;k<=n2-1;k++)
{
c[i][j]=c[i][j]+a[i][j]*b[i][j];
}
}
}
printf("\nMatrix is \n");
for(i=0;i<=m1-1;i++)
{
for(j=0;j<=n2-1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
}

OUTPUT
Enter elements of first matrix
a[0][0]=1
a[0][1]=2
a[1][0]=3
a[1][1]=4
Enter elements of second matrix
b[0][0]=1
b[0][1]=4
b[1][0]=3
b[1][1]=1
Matrix is
2 16
18 8

6. Program to calculate transpose of a matrix

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],t[10][10],m,n,i,j;
clrscr();
printf("\nEnter number of rows and columns");
scanf("%d%d",&m,&n);
printf("\nEnter elements of matrix \n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nTranspose of matrix is\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
t[i][j]=a[j][i];
printf("%d\t",t[i][j]);
}
printf("\n");
}
}

OUTPUT
Enter number of rows and columns 2 3
Enter elements of matrix
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
Transpose of matrix is
1 4
2 5
3 6

7. Program to calculate sum of square of diagonal elements of a

matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],n,i,j;
int sum=0;
clrscr();
printf("\nEnter size of matrix");
scanf("%d",&n);
printf("\nEnter elements \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i==j)
sum=sum+a[i][j]*a[i][j];
}
}
printf("\nSum=%d",sum);
}

OUTPUT
Enter size of matrix 3
Enter elements
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
Sum=107

8. Program to determine a square matrix is symmetric or not.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10][10],t[10][10],n,i,j,flag=-1;
clrscr();
printf("\nEnter size of matrix");
scanf("%d",&n);
printf("\nEnter elements of matrix \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("\na[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nTranspose of matrix is\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
t[i][j]=a[j][i];
printf("%d\t",t[i][j]);
}
printf("\n");
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(t[i][j]==a[i][j])
flag=0;
else
{
printf("\nNot a symmetric matrix");
exit(0);
}
}
}
if(flag==0)
printf("Symmetric matrix");
}

OUTPUT
Enter size of matrix 2
Enter elements of matrix
a[0][0]=6
a[0][1]=8
a[1][0]=4
a[1][1]=2
Transpose of matrix is
6 4
8 2
Not a symmetric matrix

9. Program to display upper triangle and lower triangle of a

square matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],n,i,j;
clrscr();
printf("\nEnter size of matrix");
scanf("%d",&n);
printf("\nEnter elements of matrix \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}

}
printf("Upper triangle is \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i<j)
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Lower triangle is ");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i>j)
printf("%d\t",a[i][j]);
}
printf("\n");
}
}

Output:
Enter size of matrix 3
Enter elements of matrix
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
Upper triangle is
2 3
6
Lower triangle is
4
7 8

H. Strings
1. Program to input a string from the user using scanf().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char name[20];

clrscr();

printf("Enter your name:");

scanf("%s",&name);

printf("\nHello %s",name);

getch();

Output:

Enter your name: harkirat

Hello harkirat
2. Program to input a string from user using gets() and display using

puts().

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
clrscr();
printf("\n enter your name");
gets(name);
puts("Hello");
puts(name);
}
OUTPUT
Enter your name Krishna
Hello
Krishna

3. Program to pass a string to a function and also display ASCII values

of string characters.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void display(char[]);
void main()
{
char str[20];
clrscr();
puts("Enter a string");
gets(str);
display(str);
}
void display(char str[])
{
int i;
for(i=0;str[i]!='\0';i++)
printf("\n Character=%c Ascii value=%d",str[i],str[i]);
}

OUTPUT
Enter a string
bca class

Character=b Ascii value=98


Character=c Ascii value=99
Character=a Ascii value=97
Character= Ascii value=32
Character=c Ascii value=99
Character=l Ascii value=108
Character=a Ascii value=97
Character=s Ascii value=115
Character=s Ascii value=115
4. Program to calculate length of a string using library

function strlen().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char str[20];

int z;

clrscr();

printf("enter a string:");

gets(str);

z=strlen(str);

printf("lenght of string:%d",z);

getch();

Output:

enter a string:hello
lenght of string:5

5. Program to calculate length of a string without using library

function strlen().

#include<stdio.h>

#include<conio.h>

#include<string.h>

int str_length(char[]);

void main()

char str[81];

int k;

clrscr();

printf("Enter a string:");

gets(str);

k=str_length(str);

printf("Length of string %s=%d",str,k);

getch();

int str_length(char s[])


{

int i=0;

while(s[i]!='\0')

i++;

return(i);

Output:

Enter a string: hello

Length of string hello=5

6. Program to copy one string into another string using library

function strcpy().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{
char source[20],target[20];

clrscr();

printf("\nEnter a string");

gets(source);

strcpy(target,source);

printf("\ntarget string =%s",target);

Output:

Enter a string hello

target string = hello

7. Program to copy one string without using library function

strcpy().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()
{

char source[20],target[20];

int i;

clrscr();

printf("\nEnter a string");

gets(source);

strcpy(target,source);

for(i=0;source[i]!='\0';i++)

target[i]=source[i];

target[i]='\0';

printf("\ntarget string =%s",target);

Output:

Enter a string hello

target string = hello


8. Program to compare two string using library fuction

strcmp().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char s1[20],s2[20];

clrscr();

printf("\nEnter first string");

gets(s1);

printf("\nEnter second string");

gets(s2);

if(strcmp(s1,s2)==0)

printf("Equal strings");

else

printf("unequal strings");

Output:
Enter first string hello

Enter second string everyone

unequal strings

10. Program to compare two string without using library

function strcmp().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int l1,l2,i,end,flag=0;

char s1[20],s2[20];

clrscr();

printf("\nEnter first string");

gets(s1);

printf("\nEnter second string");

gets(s2);

l1=strlen(s1);

l2=strlen(s2);

if(l1>l2)
end=l1;

else

end=l2;

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

if(s1[i]!=s2[i])

printf("unqual strings");

flag=1;

break;

if(flag==0)

printf("equal strings");

Output:

Enter first string hello

Enter second string hello

equal strings

11.Program to compare two strings using library function

strcmp().

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

#include<string.h>

void main()

char s1[81],s2[81];

clrscr();

printf("Enter the first string1:");

gets(s1);

printf("Enter the second string1:");

gets(s2);

if(strcmp(s1,s2)==0)

printf("strings %s and %s are equal",s1,s2);

else if(strcmp(s1,s2)>0)

printf("strings %s is greater than '%s'" ,s1,s2);

else

printf("strings %s is less than '%s'",s1,s2);

getch();

Output:

Enter the first string1: landslide

Enter the second string1: landscape

strings landslide is greater than ' landscape'

12.Progarm to concatenate two strings using library function using

strcat().
#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char s1[30]="Good";

char s2[20]="Morning";

clrscr();

strcat(s1,s2);

printf("\nConcatenate string=%s",s1);

Output:

Concatenate string=GoodMorning

13.Program to concatenate two strings without using strcat().


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[30],s2[30];
int i,l,j;
clrscr();
printf("\n Enter first string");
gets(s1);
printf("\n Enter second string");
gets(s2);
l=strlen(s1);
for(i=l,j=0;s2[j]!='\0';i++,j++)
s1[i]=s2[j];
s1[i]='\0';
printf(" Concatenated string=%s",s1);
}
OUTPUT
Enter first string Good

Enter second string Evening


Concatenated string= Good Evening

14.Program to reverse a string using strrev().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char a[80];

clrscr();

printf("\Enter a string please:");

gets(a);

strrev(a);
printf("\nString after reversing:%s",a);

getch();

Output:

Enter a string please:Welcome

String after reversing:emocleW

15. Program to reverse a given string without strrev().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void str_reverse(char[]);

void main()

char s[81];

clrscr();

printf("Enter a string:");

gets(s);

str_reverse(s);

printf("String after reversing;%s",s);

getch();

void str_reverse(char str[])

{
char s2[80];

int i,j,count=0;

for(i=0;str[i]!='\0';i++)

++count;

s2[i]=str[i];

for(i=count-1,j=0;i>=0;i--,j++)

str[j]=s2[i];

str[j]='\0';

Output:

Enter a string:Welcome

String after reversing: emocleW

16. Program to determine whether the string is palindrome or not.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\n Enter a string ");
gets(s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s2,s2)==0)
{
printf("strings are palindrome");
}
else
printf(" Stringa are not palindrome");
}
Output
Enter a string harkirat
strings are palindrome
*/

17. Program to convert string into lowercase using strlwr().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char str[20];

clrscr();

puts("Enter a string");

gets(str);

printf("String in lowercase is %s",strlwr(str));

Output:

Enter a string
HELLO EVERYONE

String in lowercase is : hello everyone

18.Program to convert the string without using strlwr().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int i;

char str[20];

clrscr();

puts("Enter a string");

gets(str);

for(i=0;str[i]!='\0';i++)

if(str[i]>='A'&&str[i]<='Z')

str[i]=str[i]+32;

printf("Lowercase string %s",str);

Output:

Enter a string

HELLO EVERYONE

Lowercase string hello everyone


19.Program to convert string to uppercase using strupr().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char str[20];

clrscr();

puts("Enter a string");

gets(str);

printf("String in uppercase is%s",strupr(str));

Output:

Enter a string

hello everyone

String in uppercase is HELLO EVERYONE

20.Program to convert a string to uppercase without using strupr().

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int i;
char str[20];

clrscr();

puts("Enter a string");

gets(str);

for(i=0;str[i]!='\0';i++)

if(str[i]>='a'&&str[i]<='z')

str[i]=str[i]-32;

printf("Uppercase string %s",str);

Output:

Enter a string

hello everyone

Uppercase string HELLO EVERYONE

21.Program to demonstrate array of pointers to strings.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char*name[]={"Keyboard","Mouse","Printer"};

clrscr();

int i;
for(i=0;i<=2;i++)

puts(name[i]);

Output:

Keyboard

Mouse

Printer

22.Program to sort string alphabetically.

#include<stdio.h>

#include<string.h>

#include<conio.h>

void str_sort(char[]);

void main()

char str[81];

clrscr();

printf("Enter the string:");

gets(str);

str_sort(str);

printf("string after sorting:%s",str);

getch();

Output:

Enter the string: welcome


string after sorting: ceelmow

23.Program to count number of lowercase alphabets, uppercase alphabets,

digits, space and special symbols entered in string.

#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
int i, u=0,l=0,d=0,s=0;
clrscr();
puts("Enter a string");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='A' && str[i]<='Z')
u++;
if(str[i]>='a' && str[i]<='z')
l++;
else if (str[i]>='0' && str[i]<='9')
d++;
else
s++;
}
printf("\n uppercase=%d \n lowercase=%d \n digit=%d \n special symbol=%d",
u,l,d,s);
}
OUTPUT
Enter a string
comp123&*$

uppercase=0
lowercase=4
digit=3
special symbol=4

H. STRUCTURE AND UNIONS

1. To input and display the roll number, name and marks

obtained by two students.

#include<stdio.h>

#include<conio.h>

struct student

int rollno;

char name[20];

float percentage;

};

void main()

{
struct student s1,s2;

clrscr();

printf("Enter details of 2 students\n");

printf("Roll number\tName\tPercentage");

scanf("%d%s%f",&s1.rollno,s1.name,&s1.percentage);

scanf("%d%s%f",&s2.rollno,s2.name,&s2.percentage);

printf("Values you enterd\n");

printf("%d\t%s\t%0.2f",s1.rollno,s1.name,s1.percentage);

printf("\n%d\t%s\t%0.2f",s2.rollno,s2.name,s2.percentage);

getch();

Output:

Enter details of 2 students

Roll number Name Percentage

2201 kunal 75.6

2202 kapil 82.7

Values you enterd

2201 kunal 75.60

2202 kapil 82.70

2. To print the assigned values to members of the structure.


#include<stdio.h>

#include<conio.h>

#include<string.h>

struct

char title[20];

int pages;

float price;

}book1;

void main()

clrscr();

strcpy(book1.title,"Learn C");

book1.pages=528;

book1.price=320.50;

printf("Book's title=%s",book1.title);

printf(",pages=%d",book1.pages);

printf(",price=%0.2f",book1.price);

getch();

}
Output:

Book's title=Learn C,pages=528,price=320.50

3. Program that demonstrates initialization of the structure va

#include<stdio.h>

#include<conio.h>

struct student

int rollno;

char name[20];

float percentage;

};

void main()

struct student s1={2201,"kunal",75.6};

clrscr();

printf("Student's Roll number=%d",s1.rollno);

printf(",name=%s",s1.name);

printf(",percentage=%0.2f",s1.percentage);

getch();

}
Output:

Student's Roll number=2201,name=kunal,percentage=75.60

4. Program that demonstrates structure assignment operation.

#include<stdio.h>

#include<conio.h>

#include<string.h>

struct book

char publisher[20];

char title[20];

int discount;

};

void main()

struct book b1={"Lakhanpal","Learn C",20};

struct book b2;

clrscr();

b2=b1;
strcpy(b2.title,"System software");

printf("Other book details are:");

printf("\nBook title=%s,",b2.title);

printf("\t Publisher=%s,",b2.publisher);

printf("\t Discount=%d",b2.discount);

getch();

Output:

Other book details are:

Book title=System software, Publisher=Lakhanpal, Discount=20

5. To show structure members are stored in contiguous

memory locations?

#include<stdio.h>

#include<conio.h>

struct student

int rollno;

char name[20];
float percentage;

};

void main()

struct student s1={2201,"kunal",75.6};

clrscr();

printf("\nAddress of rollno=%u",&s1.rollno);

printf("\nAddress of name=%u",&s1.name);

printf("\nAddress of percentage=%u",&s1.percentage);

getch();

Output

Address of rollno=65500

Address of name=65502

Address of percentage=65522

6. To input data of n students and display the details of student

with highest percentage of marks.

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

struct student

int rollno;

char name[20];

int marks[3];

};

void main()

struct student s[10];

int i,n,loc=0;

float large,percentage[10];

clrscr();

printf("how many students=");

scanf("%d",&n);

printf("enter details of %d students\n",n);

printf("roll number\tname\tmarks in 3 subjects\n");

printf("\n");

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

{
printf("enter details of %d students",i);

scanf("%d%s%d%d%d",&s[i].rollno,&s[i].name,&s[i].marks[0],&s[i].marks[1],&s[i]

.marks[2]);

percentage[i]=(float)((s[i].marks[0]+s[i].marks[1]+s[i].marks[2]))/3;

large=percentage[0];

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

if(large<percentage[i])

large=percentage[i];

loc=1;

printf("Details of students with higest percentage");

printf("\nRoll

no=%d,Name=%s,Percentage=%0.2f",s[loc].rollno,s[loc].name,percentage[loc]);

getch();

Output:

how many students=3


enter details of 3 students

roll number name marks in 3 subjects

enter details of 0 students 212 kunal 55 66 77

enter details of 1 students 213 tanwar 78 78 79

enter details of 2 students 214 radhika 66 77 76

Details of students with higest percentage

Roll no=213,Name=tanwar,Percentage=78.33

7. To show how structures are initialized.

#include<stdio.h>

#include<conio.h>

struct employee

int emp_code;

char emp_name[20];

int dept_code;

double salary;

};
void main()

struct employee emp[]={

{101,"Raj",5,21525.70},

{102,"Paul",6,17112.50},

{103,"Arun",4,1167.50}

};

int i;

clrscr();

printf("Employee Information");

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

printf("\nEmployee code:%d",emp[i].emp_code);

printf("\tName:%s",emp[i].emp_name);

printf("\tDepartment: %d",emp[i].dept_code);

printf("\tSalary:%0.2f",emp[i].salary);

getch();

Output
Employee Information

Employee code:101 Name:Raj Department: 5 Salary:21525.70

Employee code:102 Name:Paul Department: 6 Salary:17112.50

Employee code:103 Name:Arun Department: 4 Salary:1167.50

8. To show how to access elements of nested structure.

#include<stdio.h>

#include<conio.h>

struct date

int day;

int month;

int year;

};

struct birthdaylist

struct date DOB;

char name[25];

};

void main()
{

struct birthdaylist p[10];

int i,n;

clrscr();

printf("How many people=");

scanf("%d",&n);

printf("Enter details of people and their birthdays\n");

printf("Birth date(dd-mm-yyyy)\tName\n");

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

scanf("%d-%d-

%d%s",&p[i].DOB.day,&p[i].DOB.month,&p[i].DOB.year,p[i].name);

printf("Details of birthdate's is as follows");

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

printf("\nName of person=%s",p[i].name);

printf("\tDate of

birth=%d/%d/%d",p[i].DOB.day,p[i].DOB.month,p[i].DOB.year);

}
getch();

Output:

How many people= 2

Enter details of people and their birthdays

Birth date(dd-mm-yyyy) Name

25-05-1988 Anil

17-11-1978 Kapil

Details of birthdate's is as follows

Name of person=Anil Date of birth=25/5/1988

Name of person=Kapil Date of birth=17/11/1978

9. To access the member of structure in which we have po

#include<stdio.h>

#include<conio.h>

struct date

int dd;

int mm;
int yy;

}today;

void main()

struct date*ptr;

clrscr();

ptr=&today;

printf("Enter the date");

scanf("%d%d%d",&today.dd,&today.mm,&today.yy);

printf("Entered date is\n");

printf("%d/%d/%d",(*ptr).dd,(*ptr).mm,(*ptr).yy);

getch();

Enter the date 18 11 2016

Entered date is

18/11/2016
10. To show how members of structure are accessed using a

pointer to the structure.

#include<stdio.h>

#include<conio.h>

struct date

int dd;

int mm;

int yy;

};

struct birthdaylist

struct date DOB;

char name[25];

};

void main()

struct birthdaylist p1={10,5,1979,"atul"};


struct birthdaylist*p2=&p1;

clrscr();

printf("Name=%s",p2->name);

printf(",Date of birth =%d/%d/%d",p2->DOB.dd,p2->DOB.mm,p2->DOB.yy);

getch();

Output:

Name= Atul, Date of birth =10/5/1979

11. Consider a structure function that consists of numerator

and denominator. Make a function that accepts two fractions

and returns their product.

#include<stdio.h>

#include<conio.h>

typedef struct

int num;

int deno;

}fraction;

void mul_of_frac(fraction,fraction);
void main()

fraction f1={2,3};

fraction f2={4,5};

clrscr();

mul_of_frac(f1,f2);

getch();

void mul_of_frac(fraction fr1,fraction fr2)

fraction res;

res.num=fr1.num*fr2.num;

res.deno=fr1.deno*fr2.deno;

printf("On multipilcation %d/%d x %d/%d=",fr1.num,fr1.deno,fr2.num,fr2.deno);

printf("%d/%d",res.num,res.deno);

Output

On multiplication 2/3 x 4/5=8/15


12. To show the passing of complete structure by Call by

Reference method.

#include<stdio.h>

#include<conio.h>

struct book

char*title;

int pages;

int price;

};

void display(struct book*);

void main()

struct book b={"Learn Programming in C",448,225};

clrscr();

printf("Before call= %s, %d, %d",b.title,b.pages,b.price);

display(&b);

printf("\nAfter call= %s, %d, %d",b.title,b.pages,b.price);


getch();

void display(struct book*ptr)

ptr->pages=555;

ptr->price=295;

printf("\nIn fuction =%s, %d, %d",ptr->title,ptr->pages,ptr->price);

return;

Output

Before call= Learn Programming in C, 448, 225

In fuction =Learn Programming in C, 555, 295

After call= Learn Programming in C, 555, 295

I.DATA FILES

1.Program to read an information in file character by character


#include<stdio.h>
#include<conio.h>
void main()
{
FILE*fp;
char ch;
clrscr();
fp=fopen("file1.txt","r");
if(fp==NULL)
{
printf("Cannot open file");
exit(1);
}
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
fclose(fp);
}
Output:
Cannot open file.

2.Program to display contents of a file backwards


#include<stdio.h>
#include<conio.h>
void main()
{
FILE*fp;
int length,i,n;
char ch;
clrscr();
fp=fopen("file1.txt","r");
if(fp==NULL)
{
printf("Cannot open file");
exit(1);
}
fseek(fp,0,2);
length=ftell(fp);
printf("\nNumber of characters in file are:%d",length);
printf("\nContents of file backwards are:\n");
fseek(fp,-1,2);
for(i=length-1;i>=0;i--)
{
ch=fgetc(fp);
printf("%c",ch);
n=ftell(fp);
n=n-2;
fseek(fp,n-length,2);
}
fclose(fp);
}

3. Program to copyone file to another character by character


#include<stdio.h>
#include<conio.h>
void main()
{ FILE*fs,*ft;
char source[50],target[50],ch;
clrscr();
printf("\nEnter source filename:");
gets(source);
printf("\nEnter target filename:");
gets(target);
fs=fopen(source,"r");
if(fs==NULL)
{
printf("\nCannot open source file");
exit(1);
}
ft=fopen(target,"w");
if(ft==NULL)
{
printf("\nCannot open source file");
fclose(fs);
exit(1);
}

while((ch=fgetc(fs))!=EOF)

{
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
printf("\nFile copied");
}
4.Program to read and write in afile character by character
#include<stdio.h>
#include<conio.h>
void main()
{
FILE*fp;
char str[100];
char ch;
int i=0;
clrscr();
fp=fopen("file1.txt","w");
if(fp==NULL)
{
printf("Cannot open file");
exit(1);
}
printf("\nEnter text you want to insert into file");
gets(str);
while(str[i]!='\0')
{
fputc(str[i],fp);
i++;
}
printf("\nEnter text you want to insert into file:");
gets(str);
while(str[i]!='\0')
{
fputc(str[i],fp);
}
printf("\nContents written in file");
fclose(fp);
fp=fopen("file1.txt","r");
if(fp==NULL)
{
printf("Cannot open file");
exit(1);
}
printf("\nContents of file are:\n");
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
fclose(fp);
getch();
}

Potrebbero piacerti anche