0% found this document useful (0 votes)
22 views

c lab manual i it an

This document is a lab record for the C Programming course at Madurai Kamaraj University College, detailing various programming exercises for first-year B.Sc. (Information Technology) students. It includes a list of programs covering fundamental concepts such as data types, arithmetic operations, control structures, and functions. Each program is accompanied by code snippets and expected outputs for practical understanding.

Uploaded by

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

c lab manual i it an

This document is a lab record for the C Programming course at Madurai Kamaraj University College, detailing various programming exercises for first-year B.Sc. (Information Technology) students. It includes a list of programs covering fundamental concepts such as data types, arithmetic operations, control structures, and functions. Each program is accompanied by code snippets and expected outputs for practical understanding.

Uploaded by

SUKUMAR M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

MADURAI KAMARAJ UNIVERSITY COLLEGE

ALAGAR KOVIL MAIN ROAD, MADURAI – 625 002

DEPARTMENT OF COMPUTER SCIENCE

BACHELOR OF SCIENCE (INFORMATION TECHNOLOGY)

FIRST YEAR

C PROGRAMMING LAB

STUDENT LAB RECORD

STUDENT NAME :

REGISTER NO :
MADURAI KAMARAJ UNIVERSITY COLLEGE
ALAGAR KOVIL MAIN ROAD, MADURAI – 625 002

DEPARTMENT OF COMPUTER SCIENCE

BONAFIDE
CERTIFICATE
Register Number: ______________________________

Certified that this is a Bonafide Record of work done by


_________________________________________ of I year / I Semester of B.Sc
(Information Technology) of this institution on C PROGRAMMING
LABORATORY during the year 2024-2025.

Submitted for the University Practical Examination held on ____________________

Internal Examiner External


Examiner

TABLE OF CONTENTS
1 Program to print a message "Hello World" on the screen
2 Program to Display Multiple Statements
3 Program to Initialize int, char, float data types
4 Program to accept the values of int, float, char data types and display them.
5 Program to accept characters and display them.
6 Program to accept 2 numbers and compute all arithmetic operations*/
7 Program to accept radius & calculate area, diameter and circumference of circle*/
8 Program to accept two numbers & swap the values using third variable*/
9 Program to accept two numbers & swap the values without using third variable*/
10 Program to accept marks and obtain total and percentage of marks*/
11 Program to evaluate the expressions*/
12 Program to check whether student is passed or failed*/
13 Program to check whether given number is even or odd*/
14 Program to find the largest of three numbers*/
15 Program to illustrate concept of else-if ladder to select color
16 Program to print words corresponding numbers below 9
17 Program to check number is positive, negative or zero*/
18 Program to check whether the given numbers are equal*/
19 Program to check whether the given character is vowel or consonant */
20 Program to calculate square of numbers whose least significant digit is 5.
21 Program to obtain electric bill as per meter reading*/
22 Program to check small,capital,digit or special*/
23 Program to check whether given letter is vowel or consonant*/
24 Program to calculate Arithmetic Operations depending on operator.
25 Program to print Multiplication Table without using looping statements*/
26 Program to Print 1 to 10 numbers*/
27 Program to display the largest of given numbers*/
28 Program to Calculate sum of 10 natural numbers*/
29 Program to Display numbers in the following format.
30 Program to print Multiplication Table using do..while statement*/
31 Program to print Fibonacci Series
32 Program to calculate factorial of given number*/
Program to take an array of 10 integers and accept values into it. Sort the array in
33 descending order.
34 Program to convert lowercase to uppercase and uppercase to lowercase.
35 Program to Concatenate of 2 Strings
36 Program to Illustrate the Concept of Pointers*/
37 Program to demonstrate how to use structure
38 Program to illustrate concept of unions.
39 Program to illustrate the concept of call by value
40 Program to find factorial of given number using recursion.
41 Program to illustrate fgets and fputs function.

1. To print a message "Hello World" on the screen


/*Program to print a message "Hello World" */
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf("Hello World");
}
Output:
Hello World

2. To Display Multiple Statements


/*Program to print Name and Address*/
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf("Name: Sachin Tendulkar");
printf("\nQualification: Degree");
printf("\nAddress: Mumbai")
printf("\nWork: Cricket Player");
}

Output:
Name: Sachin Tendulkar
Qualification: Degree
Address: Mumbai
Work: Cricket Player

3. To Initialize int, char, float data types


/*Program to initialize int, char, float data types*/
#include<stdio.h>
#include<conio.h>
main()
{
int n=78;
float j=3.0;
char x='y';
clrscr();
printf("Integer=%d\tFloat Value=%f\tCharacter=%c",n,j,x);
}
Output:
Integer=78 Float Value=3.0 Character=y

4. To accept the values of int, float, char data types and display
them.
/*Program to accept values of int, char, float data types
Display them in the order of reading*/
#include<stdio.h>
#include<conio.h>
main()
{
char x;
int num;
float j;
clrscr();
/*Accept the values for data types from
user*/ printf("Enter Character: ");
scanf("%c",&x);

printf("Enter Integer Value: ");


scanf("%d",&num);
printf("Enter Float Value: ");
scanf("%f",&j);
/*Display the accepted values*/
printf("Integer=%d\tFloat Value=%f\tCharacter=%c",num,j,x);
}
Output:
Enter Character: a (Enter)
Enter Integer Value: 20 (Enter)
Enter Float Value: 100 (Enter)
Integer=20 Float Value=100.0 Character=a
5. : Program to accept characters and display them.
/*Program to accept characters and display*/
#include<stdio.h>
#include<conio.h>
main()
{
char x,y,z;
clrscr();
printf("Enter 1st character: ");
x = getchar();
printf("Enter 2nd character: ");
y = getche();
printf("\nEnter 3rd character: ");
z = getch();
printf("\nFirst character is ");
putchar(x);
printf("\nSecond character is ");
putch(y);
printf("\nThird character is ");
putchar(z);
}
Output:
Enter 1st character: k
Enter 2nd character: l
Enter 3rd character:
First character is k
Second character is l
Third character is ;

6. Input two numbers and compute all arithmetic operations


/*Program to accept 2 numbers and compute all arithmetic
operations*/
#include<stdio.h>
#include<conio.h>
main()
{
int num1,num2;
clrscr();
/*Accept two numbers from user*/
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
/*Display values for arithmetic operators*/
printf("Sum of 2 numbers: %d",num1+num2); printf("\
nDifference of 2 numbers: %d", num1-num2); printf("\
nProduct of 2 numbers: %d", num1*num2); printf("\
nQuotient for %d/%d: %d", num1,num2,num1/num2);
printf("\nRemainder for %d/%d: %d",
num1,num2,num1%num2);
}
Output:
Enter first number: 52 (Enter)
Enter second number: 12 (Enter)
Sum of 2 numbers: 64
Difference of 2 numbers: 40
Product of 2 numbers: 624
Quotient for 52/12: 4
Remainder for 52/12: 4

7. Input radius, compute area, diameter, & circumference of the


circle and display them.
/*Program to accept radius & calculate area, diameter and
circumference of circle*/
#include<stdio.h>
#include<conio.h>
main()
{
/*Declare the variables*/
int radius,diameter;
float area,circumference;
const float PI = 3.14; /*set variable PI to
constant*/ clrscr();
/*Accept the value of radius*/
printf("Enter circle radius: ");

scanf("%d",&radius);
/*Compute the area, diameter and circumference*/
diameter = 2*radius;
area = PI*radius*radius;
circumference = 2*PI*radius;
/*Display the results*/
printf("Area of circle = %.2f",area);
printf("\nDiameter of circle = %d",diameter);
printf("\nCircumference of circle = %.2f",circumference);
getch();
}
Output:
Enter circle radius: 5 (Enter)
Area of circle = 78.50
Diameter of circle = 10
Circumference of circle = 31.40

8. Swapping the values of two variables using third variable


/*Program to accept two numbers & swap the values*/
#include<stdio.h>
#include<conio.h>
main()
{
/*Declare the variables*/
int num1,num2,temp;
clrscr();
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Numbers before swapping: %d
%d",num1,num2); /*swapping the values of
variables*/ temp = num1;

num1 = num2;
num2 = temp;
printf("Numbers after swapping: %d %d",num1,num2);
getch();
}
Output:
Enter first number: 6
Enter second number: 5
Numbers before swapping: 6 5
Numbers after swapping: 5 6

9. Program for swapping the values of two variables without


using third variable
/*Program to accept two numbers & swap the values*/
#include<stdio.h>
#include<conio.h>
main()
{
/*Declare the variables*/
int num1,num2;
clrscr();
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Numbers before swapping: %d
%d",num1,num2); /*swapping the values of
variables*/ num1 = num1 + num2;
num2 = num1 – num2;
num1 = num1 – num2;
printf("Numbers after swapping: %d
%d",num1,num2); getch();
}
Output:
Enter first number: 6
Enter second number: 5
Numbers before swapping: 6 5
Numbers after swapping: 5 6

10. Program to calculate total marks and percentage of a


student for 5 subjects where marks of each subject should be
greater than minimum pass marks (Ex: 35).
/*Program to accept marks and obtain total and percentage of marks*/
#include<stdio.h>
#include<conio.h>
main()
{
int sub1,sub2,sub3,sub4,sub5,sum;
long int studno;
float total=500,percentage;
clrscr();
printf("Enter Student Number: ");
scanf("%ld",&studno);
printf("Enter SUBJECT1 marks: ");
scanf("%d",&sub1);
printf("Enter SUBJECT2 marks: ");
scanf("%d",&sub2);
printf("Enter SUBJECT3 marks: ");
scanf("%d",&sub3);
printf("Enter SUBJECT4 marks: ");
scanf("%d",&sub4);
printf("Enter SUBJECT5 marks: ");
scanf("%d",&sub5);
sum=sub1+sub2+sub3+sub4+sub5;
percentage=(sum/total)*100;
printf("=============RESULT=============\n");
printf("STUDENT NUMBER: %ld",studno);
printf("\nTOTAL MARKS OBTAINED FOR 500: %d",sum);
printf("\nPERCENTAGE: %.2f",percentage);
getch();
}
Output:
Enter Student Number: 1220610113
Enter SUBJECT1 marks: 90
Enter SUBJECT2 marks: 91
Enter SUBJECT3 marks: 95
Enter SUBJECT4 marks: 93
Enter SUBJECT5 marks: 89
=============RESULT=============
STUDENT NUMBER: 1220610113
TOTAL MARKS OBTAINED FOR 500: 458
PERCENTAGE: 91.60

11. To implement the concept of evaluating the expressions


/*Program to evaluate the expressions*/
#include<stdio.h>
main()
{
int a=9,b=13,c=3;
float x,y,z;
x = a-b/3.0+c*2-1;
y = a-(float)b/(3+c)*(2-1);
z = a-((float)b/(3+c)*2)-1;
printf("x = %f\t\ty = %f\t\tz =
%f",x,y,z); getch();
}
Output:
x = 9.666667 y = 6.833333 z = 3.666667

12. : To check whether student is passed or failed.


/*Program to check whether student is passed or failed*/
#include<stdio.h>
#include<conio.h>
main()
{
int marks;
clrscr();
printf("Enter student marks: ");
scanf("%d",&marks);
if(marks>50)
printf("Student Passed");
if(marks<50)
printf("Student Failed");
getch();
}

Output:
(1) Enter student
marks: 55
Student Passed
(2) Enter student
marks: 40
Student Failed

13. : Program to check whether given number is even or odd


/*Program to check whether given number is even or odd*/
#include<stdio.h>
#include<conio.h>
main()
{
int num;
clrscr();
printf("Enter number: ");
scanf("%d",&num);
if(num%2 == 0)
printf("%d is even number",num);
else
printf("%d is odd number",num);
getch();
}

Output:
(1) Enter
number: 53
53 is odd
number
(2) Enter student
marks: 42 42 is
even number

14. : Program to find the largest of three numbers


/*Program to find the largest of three numbers*/
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter the three values: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is largest",a);
else
if(b>a && b>c)
printf("%d is largest",b);
else
printf("%d is largest",c);

getch();
}

Output:
Enter number: 5 6 7
7 is largest

15. : Program to illustrate concept of else-if ladder to select color


/*Program to select color*/
#include<stdio.h>
#include<conio.h>
main()
{
int n;
clrscr();
printf("Enter any number between 1 & 4 to select color: \n");
scanf("%d",&n);
if(n==1)
{
printf("You selected Red color");
}
else if(n==2)
{
printf("You selected Green color");
}
else if(n==3)
{
printf("You selected yellow color");
}
else if(n==4)
{
printf("You selected Blue color");
}
else
{
printf("No color selected");
}
getch();
}
Output:
(1)Enter any value between 1 & 4 to
select color: 4 You selected Blue Color
(2)Enter any value between 1 & 4 to
select color: 1 You selected Red Color
(3)Enter any value between 1 & 4 to
select color: 5 No color selected

16: Program to print words corresponding numbers below 9


/*Print words corresponding numbers below 9*/
#include<stdio.h>
#include<conio.h>
main()
{
int n;
clrscr();
printf("Enter a number(0-9): ");
scanf("%d",&n);
switch(n)
{
case 0: printf("Zero");
break;
case 1: printf("One");
break;
case 2: printf("Two");
break;
case 3: printf("Three");
break;
case 4: printf("Four");
break;
case 5: printf("Five");
break;
case 6: printf("Six");
break;
case 7: printf("Seven");
break;
case 8: printf("Eight");
break;
case 9: printf("Nine");
break;
default: printf("More than 9");
}
getch();
}
Output:
Enter a number (0-9): 3
Three
Enter a number (0-9): 10
More than 9

17. To check whether number is +ve, -ve or zero


/*Program to check number is positive, negative or zero*/
#include<stdio.h>
#include<conio.h>
main()
{
int n;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
if(n>0)
printf("Number is Positive");
if(n<0)

printf("Number is Negative");
if(n==0)
printf("Number is Zero");
}
Output:
Enter a number: -2
Number is Negative
Enter a number: 0
Number is Zero
Enter a number: 6
Number is Positive

18. To check two numbers are equal


/*Program to check whether the given numbers are equal*/
#include<stdio.h>
#include<conio.h>
main()
{
int num1,num2;
clrscr();
printf("Enter 2 numbers: ");
scanf("%d %d",&num1,&num2);
if(num1==num2)
printf("Both the numbers are
equal"); getch();
}
Output:
Enter 2 numbers: 6 6
Both the numbers are equal
Enter 2 numbers: 3 2

19. Check whether given character is vowel or consonant.


/*Program to check whether the given character is vowel or consonant */
#include<stdio.h>
#include<conio.h>
main()
{
char x;
clrscr();
printf("Enter letter: ");
x=getchar();
if(x=='a'||x=='A'||x=='e'||x=='E'||x=='i'||x=='I'||x=='o'||
x=='O'
||x=='u'||x=='U')
printf("The character %c is a vowel",x);

else
printf("The character %c is a
consonant",x); getch();
}
Output:
Enter letter: p
The character p is a consonant
Enter letter: a
The character a is a vowel

20. Program to calculate square of numbers whose least


significant digit is 5.
/*Program to calculate square of numbers whose LSD is 5 */
#include<stdio.h>
#include<conio.h>
main()
{
int s,d;
clrscr();
printf("Enter a Number: ");
scanf("%d",&s);
d=s%10;
if(d==5)
{
s=s/10;
printf("Square = %d %d",s*s++,d*d);
}
else
printf("\nInvalid Number");
}
Output:
Enter a Number: 25
Square = 625
Enter a Number: 32
Invalid Number

21. To obtain the electric bill as per the charges below


Rates (In
No of Units Consumed Rs.)
500 and above 5.50
200-500 3.50
100-200 2.50
Less than 100 1.50
/*Program to obtain electric bill as per meter
reading*/ #include<stdio.h>
#include<conio.h>
main()
{
int initial,final,consumed;
float total;
clrscr();
printf("Initial & Final Readings:
"); scanf("%d
%d",&initial,&final); consumed =
final -initial; if(consumed>500)
total=consumed*5.50;
else if(consumed>=200 && consumed<=500)
total=consumed*3.50;
else if(consumed>=100 && consumed<=199)
total=consumed*2.50;
else if(consumed<100)
total=consumed*1.50;
printf("Total bill for %d units is
%f",consumed,total); getch();
}
Output:
Initial & Final Readings: 1200 1500
Total bill for 300 units is
1050.000000

Initial & Final Readings: 800 900


Total bill for 100 units is
250.000000

22. To check whether letter is small, capital, digit or special


symbol.
/*Program to check small,capital,digit or special*/
#include<stdio.h>
#include<conio.h>
main()
{
char x;
clrscr();
printf("Enter character: ");
scanf("%c",&x);
if(x>='a' && x<='z')
printf("Small letter");
else if(x>='A' && x<='Z')
printf("Capital letter");
else if(x>='0' && x<='9')
printf("Digit");
else
printf("Special Symbol");
getch();
}
Output:
Enter character: *
Special Symbol
Enter character: T
Capital letter
Enter character: a
Small letter
Enter character: 6
Digit

23: Program to check whether a letter is vowel or consonant


/*Program to check whether given letter is vowel or consonant*/
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
clrscr();
printf("Enter Character: ");
ch = getch();
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': printf("Character %c is
Vowel",ch); break;
default: printf("Character %c is Consonant",ch);
}
getch();
}

Output:
Enter Character: a
Character a is Vowel
Enter Character: B
Character B is Consonant

24: Program to calculate Arithmetic Operations depending on


operator.
/*Print words corresponding numbers below 9*/
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
char ch;
clrscr();
printf("Enter any arithmetic operator: ");
scanf("%c",&ch);
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
switch(ch)
{
case '+': printf("\nThe sum is
%d",a+b); break;
case '-': printf("\nThe difference is %d",a-
b); break;
case '*': printf("\nThe product is
%d",a*b); break;
case '/': printf("\nThe quotient is
%d",a/b); break;
case '%': printf("\nThe remainder is %d",a
%b); break;
default: printf("Not Valid Operator");
}
getch();
}
Output:
Enter any arithmetic operator: *
Enter two numbers: 8 4
The product is 32

25. : To print Multiplication Table


/*Program to print Multiplication Table*/
#include<stdio.h>
#include<conio.h>
main()
{
int a,i=1;
clrscr();
printf("Enter the value of a: ");
scanf("%d",&a);
printf("\nMultiplication Table for %d\n",a);
printf("------------------------------- \n");
start:
printf("%d x %d = %d\n",a,i,a*i);
i=i+1;
if(i<=10)
goto start;
getch();
}
Output:
Enter the value of a: 5

Multiplication Table for 5


-------------------------------
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

26. To print 1 to 10 odd numbers.


/*Print 1 to 10 numbers*/
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;
clrscr();
while(i<=10) { Output:
printf("\n%d",i); 1
i+ = 2; 3
} 5
getch(); 7
} 9

27. To print largest of the given numbers.


/*Program to display the largest of given numbers*/
#include<stdio.h>
main()
{
int num;
int largenum=0;
clrscr();
printf("Enter a number (0 to stop): ");
scanf("%d",&num);
while(num!=0)
{
if(num>largenum)
largenum = num;
printf("Enter a number (0 to stop):
"); scanf("%d",&num);
}
printf("Largest number is
%d",largenum); getch();
}
Output:
Enter a number (0 to stop): 6
Enter a number (0 to stop): 9
Enter a number (0 to stop): 16
Enter a number (0 to stop): 98
Enter a number (0 to stop): 0
Largest number is 98

28. To calculate sum of 10 natural numbers.


/*Calculate sum of 10 natural numbers*/
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,sum=0;
clrscr();
do
{
sum + = i;
i++;
}
while(i<=10);
printf("\nThe sum of 10 natural numbers is: %d",sum);
getch();
}

Output:
The sum of 10 natural numbers is: 55

29. Display numbers in the following format.


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
main()
{
int i,j;
clrscr();
for ( i = 1; i <= 5 ; i++)
{
for ( j = 1 ; j <= i ; j ++) {
printf("%5d", j);
}
printf("\n");
}
getch();
}

30. To print Multiplication table.


/*Program to print Multiplication Table using do while loop*/
#include<stdio.h>
#define COLMAX 10
#define ROWMAX 10
main()
{
int row,column,y;
clrscr();
row=1;
printf(" MULTIPLICATION TABLE\n");
printf(" -----------------------------------------\n");
do
{
column=1;
do
{
y=row*column;
printf("%4d",y);
column = column+1;
}
while(column<=COLMAX);
printf("\n");
row = row+1;
}
while(row<=ROWMAX);
printf(" ----------------------------------------- \n");
getch();
}
Output:
MULTIPLICATION TABLE
-----------------------------------------
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

31. To print Fibonacci Series.


/*Program to print Fibonacci Series 0,1,1,2,3,5,8,13,...*/
#include<stdio.h>
main()
{
int n,n1=0,n2=1,n3,i=3;
clrscr();
printf("Enter number for series: ");
scanf("%d",&n);
printf("\nFibonacci Series: ");
printf("\n%3d%3d",n1,n2);
do
{
n3 = n1+n2;
printf("%3d",n3);
n1 = n2;
n2 = n3;
i++;
}
while(i<=n)
; getch();
}
Output:
Enter number for series: 10
Fibonacci Series:
0 1 1 2 3 5 8132134

32. To calculate factorial of any number


/*Program to calculate factorial of given number*/
#include<stdio.h>
main()
{
int i,n;
long int fact=1;
clrscr();
printf("Enter n value: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf("Factorial of %d is
%ld",n,fact); getch();
}
Output:
Enter n value: 8
Factorial of 8 is 40320

33. Take an array of 10 integers and accept values into it. Sort
the array in descending order.
#include<stdio.h>
#include<conio.h>
main()
{
int ar[10];
int i, j, temp;
clrscr();
for (i = 0 ; i < 10 ; i++)
{
printf("Enter number for [%d] element:
",i); scanf("%d", &ar[i]);
}
/* sort array in descending order
*/ for ( i = 0 ; i < 9 ; i++) {

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


{
if ( ar[i] > ar[j])

{
/* interchange */
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
} /* end of j loop
*/ } /* end of i loop */
/* display sorted array */
printf("\nSorted Numbers \n");
for ( i = 0 ; i < 10 ; i++)
{
printf("%d\n", ar[i]);
}
getch();
}
Output:
Enter number for [0] element: 35
Enter number for [1] element: 67
Enter number for [2] element: 98
Enter number for [3] element: 12
Enter number for [4] element: 6
Enter number for [5] element: 58
Enter number for [6] element: 49
Enter number for [7] element: 23
Enter number for [8] element: 71
Enter number for [9] element: 85
Sorted Numbers
6
12
23
35
49
58
67
71
85
98
34. : Program to convert lowercase to uppercase and uppercase
to lowercase.
#include<stdio.h>
#include<string.h>
main()
{
char name[100];
clrscr();
printf("Enter a string: ");
gets(name);
printf("\nUppercase String: ");
puts(strupr(name));
strlwr(name);
printf("\nLowercase String: ");
puts(name);
getch();
}
Output:
Enter a string: peter
Uppercase String: PETER
Lowercase String: peter

35. : Program to Concatenate of 2 Strings


#include<stdio.h>
#include<conio.h>
main()
{
char string1[30], string2[20];
int i, length=0, temp;
printf("Enter the Value of String1: \
n"); gets(string1);
printf("\nEnter the Value of String2: \
n"); gets(string2);
for(i=0; string1[i]!='\0'; i++)
length++;
temp = length;
for(i=0; string2[i]!='\0'; i++)
{
string1[temp] = string2[i];

temp++;
}
string1[temp] = '\0';
printf("\nThe concatenated string is:\n");
puts(string1);
getch();
}
Output:
Enter the Value of String1:
C Language
Enter the Value of String2:
is good
The concatenated string is:
C Language is good

36. : Program to illustrate the concept of pointers.


/*Program to Illustrate the Concept of Pointers*/
#include <stdio.h>
main()
{
int a = 10;
int *p;
p = &a;
clrscr();
printf("\nAddress of a: %u", &a);
printf("\nAddress of a: %u", p);
printf("\nAddress of p: %u", &p);
printf("\nValue of p: %d", p);
printf("\nValue of a: %d", a);
printf("\nValue of a: %d", *(&a));
printf("\nValue of a: %d", *p);
getch();
}
Output:

Address of a: 65494
Address of a: 65494
Address of p: 65496
Value of p: 65494
Value of a: 10
Value of a: 10
Value of a: 10

37. : Program to demonstrate how to use structure


main()
{
struct employee
{
int eno;
char ename[20];
char eadd[100];
float bs;
};
struct employee x;
int hra;
printf("Enter employee details: ");
scanf("%d",&x.eno);
gets(x.ename);
gets(x.eadd);
scanf("%f",&x.bs);

if(x.bs>15000)
hra = 1500;
else
hra = 1000;

printf("Details of employee\n");
printf("Employee Number: %d\n",x.eno);
printf("Employee Name: %s\n",x.ename);
printf("Employee Address: %s\n",x.eadd);
printf("Employee Basic Salary: %f\
n",x.bs); printf("Employee Salary: %f\
n",x.bs+hra);
}
38. : Program to illustrate concept of unions.
#include<stdio.h>
#include<conio.h>
main()
{
union number
{
int n1;
float n2;
char name[20];
};
union number x;
clrscr();
printf("\nEnter Name: ");
gets(x.name);
printf("Enter the value of n2: ");
scanf("%f", &x.n2);
printf("Enter the value of n1: ");
scanf("%d", &x.n1);
printf("\n\nValue of n1 = %d",x.n1);
printf("\nValue of n2 = %f",x.n2);
printf("\nName = %s",x.name);
getch();
}

Output:
Enter Name: Zaheer
Enter the value of n2: 3.23
Enter the value of n1: 6
Value of n1 = 6
Value of n2 = 3.218751
Name = ♠

39. : Program to illustrate the concept of call by value


#include <stdio.h>
swap (int, int);
main()
{
int a, b;
printf("\nEnter value of a & b:
"); scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a,
b); swap(a, b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
getch();
}
swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
Output:
Enter value of a & b: 2 3
Before swapping: 2 3
After swapping: 2 3

40. : Program to find factorial of given number using recursion.


#include <stdio.h>
long fact(int);
main()
{
int n;
long f;
printf("\nEnter number to find factorial: ");
scanf("%d", &n);
f = fact(n);
printf("\nFactorial: %ld", f);
getch();
}

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

41. : Program to illustrate fgets and fputs function.


#include<stdio.h>
main()
{
FILE *fp;
char s[80];
clrscr();
fp=fopen("strfile.txt","w");
printf("\nEnter a few lines of text:\n");
while(strlen(gets(s))>0)
{
fputs(s,fp);
fputs("\n",fp);
}
fclose(fp);
printf("Content in file:\n");
fp=fopen("strfile.txt","r");
while(!feof(fp))
{
puts(s);
fgets(s,80,fp);
}
fclose(fp);
getch();
}

You might also like