Sei sulla pagina 1di 62

# RANGANATHAN POLYTECHNIC COLLEGE

L-Scheme

## Prepared by : A.Indhumathi Lect/ECE

1
LAB EXERCISES
PART - A
1. Write a C Program to calculate Simple and Compound interest
2. Write a C Program to swap two variables using (i) third variable and (ii) without using
a third variable.
3. Write a C Program to find the largest number between given three numbers.
4. Program to check whether the given string is palindrome or not.
5. Read a string, which consists of both lower case characters and upper case
characters. Convert the lowercase character into upper case and vice versa. Display
the new string.
6. Program to prepare the total marks for N students by reading the Regno,, Name,
Mark1 to Mark6 by using array of structures. .
7. Write a function to calculate the sum and average of given three numbers. Write a
main function to call the above function
8. Using pointers, find the length of the given string.
9. Write a program to print the address of a variable and increase the content by 5 and
print the new value.
PART B
1. Read an integer number. Find the number of digits and sum of all individual digits
and also print the above number in reverse order.
2. Using Switch Case Statement, print the given number into equivalent Word. ( For
example if the input is 3, then the output should be THREE)
3. Write a program to find the factorial of a given number (i) Without recursion (ii)
With recursion
4. Write a program to arrange the given N names in alphabetical order.
5. Write a program to read a string S1 from the terminal. Again read a string S2 from
the terminal and check the given string S2 in the string S1. If it does, remove string
S2 from the string S1 and print the updated string S1. ( For example S1 =
Concatenate and S2 = cat , then the final result should be Conenate
6. Program to read ten values to an array variable. Use pointers to locate and display
each value.
7. Reverse the following using pointers ( i) String ( ii) N integer numbers stored in any
array.
8. Write a C program to print the abbreviation of an Organization Name. ( For example
if the input is BHARAT HEAVY ELECTRONICS LIMITED , then the output should
be BHEL.)
9. Program to copy contents of one file to another file. Also find the number of
characters, lines and words in the above file.

2
OUTPUT

1000

12

## Enter the time period in years

Simple interest is

Rs240.00

Compound interest is

Rs254.40

3
EX.No.:1
SIMPLE AND COMPOUND INTEREST
DATE:

AIM:

ALGORITHM:

## 1. Read principal (p), rate of interest (r) and number of years(n)

2. Calculate simple interest (SI) and compound interest (CI) using the formula

simple = P * N * R/100;

compound=P * pow(1+R/100,N)-P;

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

clrscr();

scanf("%f",&P);

scanf("%f",&R);

## printf("Enter the time period in years \n");

4
scanf("%f",&N);

simple = P * N * R/100;

compound=P * pow(1+R/100,N)-P;

printf("\n");

getch();

}
PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:

5
OUTPUT

56

## after swapping a and b without using third variable is 5 6

6
EX.No.:2
SWAP TWO VARIABLES
DATE:

AIM:

To write a C Program to swap two variables using (i) third variable and (ii) without using
a third variable.
ALGORITHM:

2. Swap two variables with third variable using the formula
temp =x;
x=y;
y=temp;
3. Swap without third variable using formula
a=a+b;
b=a-b;
a=a-b;
4. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int a,b,c;

7
c=a;

a=b;

b=c;

a=a+b;

b=a-b;

a=a-b;

getch();

}
PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:
8
OUTPUT

## the largest of given three number is:9

9
EX.No.:3
LARGEST NUMBER BETWEEN GIVEN THREE NUMBERS
DATE:

AIM:

To write a C Program to find the largest number between given three numbers.

ALGORITHM:

1. Read values a, b, c
2. Compare a with b, c. if true then a is big else c is big.
3. Compare b with c. if true b is big
4. Else c is big
5. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int a,b,c,big;

if (a>b)

if (a>c)

big=a;
10
else

big=b;

else

if(b>c)

big=b;

else

big=c;

getch();

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:

11
OUTPUT

enter a string

malayalam

enter a string

tamil

## the given string is not a palindrome

12
EX.No.:4
STRING IS PALINDROME OR NOT
DATE:

AIM:

## To write a C Program to check whether the given string is palindrome or not.

ALGORITHM:

2. Copies string a to b
3. Reverses string b
4. Compares if original and reverse string are same
5. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

clrscr();

char s[20],s1[20];

int len,i,j;

## printf("enter a string \n");

gets(s);

strcpy(s1,s);

strrev(s1);
13
if(strcmp(s,s1)==0)

else

getch();

}
PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:

14
OUTPUT

cOmPuTeR

## the string after converting to lower case computer

15
EX.No.:5
CONVERSION OF STRING TO UPPER OR LOWER CASES
DATE:
AIM:

To write a C program to read a string, which consists of both lower case characters and upper
case characters and to convert the lowercase character into upper case and vice versa. Display
the new string.
ALGORITHM:

## 1. Initialize the variables

2. Enter one sentence
3. Se t count=i
4. Convert lower case into upper case and vice versa
5. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

clrscr();

char s[20];

printf("\n enter a string with upper and lower case letters \n");

gets(s);

getch();

16
}

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:

17
OUTPUT

121

Anit

100

100

100

100

100

100

## 121 anit 600

18
EX.No.:6
MARK LIST USING ARRAY OF STRUCTURES
DATE:

AIM:

To write a C program to prepare the total marks for N students by reading the Reg. no.,
Name, Mark1 to Mark6 by using array of structures.
ALGORITHM:

## 1. Initialize the variables using struct keyword

2. Enter no. of students name and 6 marks
3. Calculate total and average
4. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

struct student

int regno;

char name[20];

int mark[5];

int total;

};

void main()

clrscr();

19
int n;

## printf("\n enter the number of students \n");

scanf("%d",&n);

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

printf("\n enter the register number,name and six marks one by one\n");

scanf("%d%s",&s[i].regno,s[i].name);

for(int j=0;j<6;j++)

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:
20
OUTPUT

10 20 30

## the average of three numbers is 20.000000

21
EX.No.:7
SUM AND AVRAGE USING FUNCTIONS
DATE:
AIM:

To write a C program to calculate the sum and average of given three numbers by using
functions.
ALGORITHM:

## 1. Assign sub function

2. Enter three numbers
3. Calculate sum and average by calling sub function
4. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

float x,y,z;

void sumavg(float,float,float);

## printf("\n enter the values of x,y,z\n");

scanf("%f%f%f",&x,&y,&z);

sumavg(x,y,z);

getch();

22
{

sum=a+b+c;

avg=sum/3;

printf("\n");

}
PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:
23
OUTPUT

enter a string

moon light

## the length of the string is 10

24
EX.No.:8
LENGTH OF THE GIVEN STRING BY USING POINTERS
DATE:

AIM:

To write a C program find the length of the given string by using pointers.

ALGORITHM:

## 1. Enter the string

2. Assign pointer=array
3. Calculate length by increase the pointer value
4. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int i=0;

char *p;

char name[20];

p=name;

gets(name);

while(*p!='\0')

25
{

i++;

p++;

printf("\n");

getch();

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

26
RESULT:

OUTPUT

10

## the address of variable a is -12

27
EX.No.:9
PRINT THE ADDRESS OF A VARIABLE
DATE:

AIM:
To write a C program to print the address of a variable and increase the content by 5 and
print the new value.
ALGORITHM:

## 1. Set address for pointer as 1000

2. Display the pointer value
3. Increase the value by ptr=ptr+5;
4. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int a;

int *p;

p=&a;

scanf("%d", &a);

printf("\n");

28
*p=*p+5;

printf("\n");

getch();

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

29
RESULT:

OUTPUT

## enter the integer number12345

given integer=12345

reversed order=54321

## number of digits given 0 is 5

30
EX.No.:10
SUM AND REVERSE ORDER OF AN INTEGER
DATE:

AIM:
To write a C program to read an integer number and to Find the number of digits and sum
of all individual digits and also print the above number in reverse order.
ALGORITHM:

## 1. Enter the integer number

2. Assign b=a%10; a=a/10; to reverse the number
3. Call the sub function to find its sum of digits
4. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

int m,n,k=1;

int count=0,sum=0;

clrscr();

scanf("%d",&n);

while(k!=0)

31
{

count=count+1;

m=n%10;

k=n/10;

n=k;

printf("%d",m);

sum=sum+m;

m=m/10;

getch();

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:
32
OUTPUT

## enter the integer number between 1 to 6

FIVE

33
EX.No.:11
SWITCH CASE STATEMENT
DATE:

AIM:

To write a C program to print the given number into equivalent Word using Switch- Case
Statement.

ALGORITHM:

## 1. Enter the number

2. Lets switch from 0 to 9
3. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{ int n;

clrscr();

## printf("\n enter the integer number between 1 to 6\n");

scanf("%d",&n);

switch(n)

{ case 1: printf("ONE\n");

break;

case 2: printf("TWO\n");

break;

34
case 3: printf("THREE\n");

break;

case 4: printf("FOUR\n");

break;

case 5: printf("FIVE\n");

break;

case 6: printf("SIX\n");

getch();

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:

35
OUTPUT

## the factorial of a number 5 using recursion is 120

36
EX.No.:12
FACTORIAL OF A GIVEN NUMBER
DATE:
AIM:

To write a C program to find the factorial of a given number (i) Without recursion (ii)
With recursion
ALGORITHM:

## 1. Read a number to calculate factorial

2. Determine factorial loop
3. Fact =fact*c;
4. Display the result

PROGRAM:

## (i) FACTORIAL OF GIVEN NUMBER WITHOUT RECURSION

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int fact(int);

int n, choice,f;

scanf("%d",&n);

f=fact(n);

getch();

37
int fact(int n)

int f;

if(n==0)

return(1);

else

f=n*fact(n-1);

return(f);

## (ii) FACTORIAL OF GIVEN NUMBER WITH RECURSION

#include<stdio.h>

#include<conio.h>

void main()

{
clrscr();

int fact(int);

int n,choice;

## printf("\n\nenter the number to find the factorial\n\n");

scanf("%d",&n);

int f=1;

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

f=f*i;

getch();

38
}

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:

39
OUTPUT

peacock

lotus

tiger

sacrifice

peace

## the sorted names are

lotus

peace

peacock

sacrifice

tiger

40
EX.No.:13
SORTING IN ALPHABETICAL ORDER
DATE:

AIM:

ALGORITHM:

## 1. Read the number of persons

2. Read the names from user
3. Compare the name by using formula

strcpy(temp,name[i]);

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

strcpy(name[j],temp);

## 4. Display the result

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

clrscr();

char name[20][30],temp[30];

int n,v,i,j;

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

## printf("give the names one by one \n\n");

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

gets(name[i]);

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

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

v=strcmp(name[i],name[j]);

if(v>0)

strcpy(temp,name[i]);

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

strcpy(name[j],temp);

## printf("\nthe sorted names are \n\n");

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

puts(name[i]);

getch();

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.

42
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:
43
OUTPUT:

enter string1concatenate

## enter string to be replaced in string1:cat

string cat has been deleted from string concatenate and replaced string is: conenate

44
EX.No.:14
STRING HANDLNG
DATE:

AIM:

To write a C program to read a string S1 from the terminal. Again read a string S2 from
the terminal and check the given string S2 in the string S1. If it does, remove string S2 from the
string S1 and print the updated string S1.
ALGORITHM:

## 1. Get the string from char

2. Length of str starting from substarting
3. Length of substring
4. Number of bytes to move
5. Display the output

PROGRAM:

int len1,len2,i,x,y,flag=0;

char str1[50],str2[50],str3[50];

clrscr();

gets(str1);

## printf("\n\n enter string to be replaced in string1:");

gets(str2);

strcpy(str3,str1);

len1=strlen(str1);

len2=strlen(str2);

45
for(i=0;(len2<=(len1-i));i++)

for(x=i,y=0;y<=len2&&str1[x]==str2[y];y++,x++);

if(len2<=y)

flag=1;

break;

if(flag==0)

## printf("\n\n%s string2 is not present in string1 %s it cannot be deleted",str2,str1);

else

for(;(i+len2+1)<=len1;i++)

str1[i]=str1[len2+i];

str1[i]='\0';

if(flag==1)

printf("\n\nstring %s has been deleted from string%s and replaced string is: %s,str2,str3,str1);

getch();

46
PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:

47
OUTPUT

## enter 10 integer values one by one

10

20

40

60

90

30

50

65

75

49

the output is

10

20

40

60

90

30

50

65

75

49

48
EX.No.:15
DISPLAY VALUE BY USIING POINTERS
DATE:

AIM:

To write a C Program to read ten values to an array variable by using pointers to locate
and display each value.
ALGORITHM:

## 1. Declare & initialize an integer array

2. Declare & initialize float array
3. Initialize pointers
4. Iterate or loop the arrays and display the content
5. Display the output

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

clrscr();

int a[10];

int *p;

p=&a[0];

## printf("enter 10 integer values one by one\n");

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

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

## printf("the output is\n");

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

printf("%d\n",*p);

p++;

getch();

}
PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:
50
OUTPUT

i) REVERSE STRING

## Reversed string:thgil noom

51
EX.No.:16
REVERSE STRING AND INTEGER USING POINTERS
DATE:

AIM:

To write a C program to Reverse the following using pointers ( i) String ( ii) N integer
numbers stored in any array.
i) POINTERS USING STRING

ALGORITHM:

## 1. Declare and initialize an integer array

3. Find the length of string
4. Reverse the string
5. Display the output

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char str[80];

char rev[80];

int i;

int count;

char *p;

clrscr();
52
printf("\nenter the string");

gets(str);

count=strlen(str);

p=rev+strlen(str)-1;

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

*(p-i)=*(str+i);

rev[count]='\0';

printf("Original string:%s\n",str);

printf("\nReversed string:%s\n",rev);

getch();

## ii) POINTERS USING NUMBERS

ALGORITHM:

2. Reverse the numbers
3. Initialize pointers
4. Display the output

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void main()

{
53
int a[20],*ptr1,*ptr2,n,t;

clrscr();

printf("\nenter n");

scanf("%d",&n);

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

for(ptr1=a;ptr1<=a+n-1;ptr1++)

scanf("%d",ptr1);

ptr1=a;

ptr2=a+n-1;

while(ptr1<ptr2)

t=*ptr1;

*ptr1=*ptr2;

*ptr2=t;

ptr1++;

ptr2--;

## printf("\n the resultant array is \n");

for(ptr1=a;ptr1<=a+n-1;ptr1++)

printf("%d\n",*ptr1);

}
54
getch();

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:

55
OUTPUT

## the abbreviation of light emitting diode is led

56
EX.No.:17
ABBREVIATION OF AN ORGANIZATION NAME
DATE:

AIM:

ALGORITHM:

## 1. Declare and initialize an integer array

3. Find the length of string
4. Find out the space and dot in program
5. Display the output

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

clrscr();

char name[40];

char abname[15];

int i,len,k=1,j=0;

## printf("\n enter name");

gets(name);

len=strlen(name);

57
for(i=0;i<len;i++)

abname[k]=name[j];

if(name[i]==' ')

k=k+1;

j=i;

abname[k]=name[j];

j++;

}abname[k+1]='\0';

getch();

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

58
RESULT:

OUTPUT

## There are characters in two 20

59
EX.No.:18
FILE HANDLING
DATE:

AIM:

To write a C Program to copy contents of one file to another file and also find the number
of characters, lines and words in the above file.
ALGORITHM:

## 1. Read the contents in a string

2. Find the number of characters and words
3. Find the number of lines
4. Display the output

PROGRAM:

#include <stdio.h>
#include <stdlib.h>

void main()
{
FILE *fp1, *fp2, *fopen();
int c ;
char fname1[40], fname2[40] ;

nlines = 0 ;
nc = 0;

gets(fname1);

gets(fname2);

## fp1 = fopen( fname1, r );

fp2 = fopen( fname2, w ) ;
60
if ( fp1 == NULL )
{
printf(Cannot open %s for reading \n, fname1 );
exit(1);
}
else if ( fp2 == NULL )
{
printf(Cannot open %s for writing \n, fname2 );
exit(1);
}
else
{
c = getc(fp1) ;
while ( c != EOF)
{
putc( c, fp2);
c = getc( fp1 ) ;
}

fclose ( fp1 );
fclose ( fp2 );
printf(Files successfully copied \n);

## printf(Enter file name: );

gets( fname );

fp = fopen( fname, r );

if ( fp2 == NULL )
{
printf(Cannot open %s for reading \n, filename );
exit(1);
}

c = getc( fp2 ) ;
while ( c != EOF )
{
if ( c == \n )
nlines++ ;

nc++ ;
61
c = getc ( fp2 );
}

fclose( fp2 );

if ( nc != 0 )
{
printf(There are %d characters in %s \n, nc, fname );
printf(There are %d lines \n, nlines );
}
else
printf(File: %s is empty \n, fname );

}
}

PROCEDURE:

## 1. Go to my computer and open D drive.

2. Open TC and double click on bin.
3. Open the C compiler software from bin.
4. Type the program and press Alt +F9. The program is compiled and shows
success.
5. Press ctrl+F9 to execute the program. Correct the errors if any and repeat the
step 4.
6. Output is displayed.

RESULT:

62