Sie sind auf Seite 1von 62

COMPUTER PROGRAMMING LABORATORY MANUAL

Subject Code : 17CA05102

Regulations : Autonomous – R17

Class : I Semester

CHADALAWADA RAMANAMMA ENGINEERING COLLEGE


(AUTONOMOUS)

Chadalawada Nagar, Renigunta Road, Tirupati – 517 506

Department of Computer Science Engineering

Y.Saroja, Assistant Professor, CSE Department, CREC 1


CHADALAWADA RAMANAMMA ENGINEERING COLLEGE

(AUTONOMOUS)

Chadalawada Nagar, Renigunta Road, Tirupati – 517 506

Department of Computer Science Engineering


INDEX

S. No Name of the Experiment Page No

BASIC C PROGRAMS:
a) Practice DOS Commands
b) Write C program to
1 I. Find the Sum of three numbers. 6-11
II. Exchange (swap) of two numbers by using third variable.
III. Exchange (swap) of two numbers without using third variable.
IV. Print the size of all data types.
BASIC C PROGRAMS:

a) Write a C program to find the Priority and associativity of operators using


2 12-13
expressions. Take the expressions with different operators.

b) Write a C program to swap two numbers using bitwise operators.

CONTROL STATEMENTS:

a) Write a C program to find whether the given number is odd or even.

b) Write a C program to find the Maximum of two numbers.

c) Write a C program to find the Maximum of three numbers.

d) Write a C program to print ‘hello world’ without using semicolon.


3 13-18
e) Write a C program to find whether the given number is odd or even using
bitwise operator.

f) Write a C program to find the maximum of two numbers using Conditional


operator.

g) Write a program which takes two integers and one arithmetic operator from
the user, and performs the operation and then prints the result by using
switch-case.(Operators : +, -, *,/, %)

ITERATION STATEMENTS:
4 19-23
a) Write a C program to generate the required multiplication table.

Y.Saroja, Assistant Professor, CSE Department, CREC 2


b) Write a C program to find the Factorial of a given number.

c) Write a C program to check whether the given number is prime or not.

ITERATION STATEMENTS:

a) Write a C program to find the sum of the digits of a number.


5 24-26
b) Write a C program to find whether the given integer is a Palindrome or not.

c) Write a C program to generate Fibonacci numbers in the given range.

NESTED LOOPS:

a)Write a C program to print the following pattern.

6 22 27-30

333

b) Print multiplication tables up to the given table.

c) Write a C program to print Series of prime numbers in the given range.

NESTED LOOPS:

a) Write a C program to check given number is strong number or not.


7 31-32
b) Write a C program to evaluate the sum of the following series up to ‘n’
terms e x = 1 + x + x 2 /2! + x 3 /3! + x 4 /4! + --------

ARRAYS:

a) Write a C program to find the sum of positive and negative numbers in a


8 given set (Array) of numbers. 33-35
b) Write a C program to read two matrices and find

i) Sum. ii) Product and display the result in the matrix form.

ARRAYS:

a)Write a C program to read matrix and perform the following operations

i) Find the sum of Diagonal Elements of a matrix.


9 36-37
ii) Print Transpose of a matrix.

iii) Print sum of even and odd numbers in a given matrix.

Y.Saroja, Assistant Professor, CSE Department, CREC 3


STRINGS:

a) Write a C program to read two strings and perform the following operations
without using built-in string Library functions.

i) String length determination.

ii) Compare Two Strings.


10 iii) Concatenate Two Strings. 38-42

iv) String reversing

b) Write a C program to accept a line of characters and print the number of


Vowels, Consonants, blank spaces, digits and special characters.

c) Write a C program to read a set of strings and sort them in alphabetical


order.

POINTERS:

a) Write a C program to exchange two numbers using pointers.


11 43-44
b) Write a program to print the elements of an array in reverse order using
pointers.

FUNCTIONS:

a) Write a C program to illustrate the following types of functions

i) Function with no arguments and no return values


12 44-47
ii) Function with arguments and no return value

iii) Function without arguments and with return value

iv) Function with arguments and with return value

FUNCTIONS:

a) Write a C program to express a four digit number in words. For example


1546 should be written as one thousand five hundred and forty six
13 48-51
b) Write a C program using recursion for finding Factorial of a number

c) Write a C program to illustrate the Dynamic Memory allocation function


malloc( )

STRUCTURES:
14 52-54
a) Declare a structure time that has three fields hr, min, secs. Create two
structure variables start_time and end_time. Input there values from the user.

Y.Saroja, Assistant Professor, CSE Department, CREC 4


Then if start_time is not equal to end_time then display HELLO CREC on the
screen.

b) Write a C program to read student roll no, name and marks in six subjects
for n number of students and give class of each student by following the
required conditions.

c) Write a C program to demonstrate self referential structures.

FILES:

a) Write a program to create a file and write some text data on the file. Then
15 display the contents of the file and also print number of characters, number of 55-57
words, number of lines in the file.

b) Write a C program to merge two files.

FILES:

a) Write a C program to create a text file and write data on it, then display
every 5th character in that file.

16 b) Write a program to read student records into a file. Record consists of 58-62
rollno, name and marks of a student in six subjects and class. Class field is
empty initially. Compute the class of a student. The calculation of the class is
as per CREC rules. Write the first class, second class, third class and failed
students lists separately to another file.

Y.Saroja, Assistant Professor, CSE Department, CREC 5


EXPERIMENT-1
BASIC PROGRAMS

A) Practice DOS Commands necessary for design of C Programs.


DOS Commands:-
Internal Commands:
Dos Internal commands are always available to the operator and may be entered from any
Dos Prompt.
These commands are automatically loaded in to the computer.
Some of the internal commands are discussed below.
Date:
This command displays the current system date on the screen and also allows us to
change the date. The user can modify the date. Dates are accepted using the format
MM/DD/YY. Where MM stands for Month, DD stands for Date, and YY stands for Year.
Syntax: C: /> Date {enter key}
Time:
This command is used to display the current system time. The user can also modify
the time.
Syntax: C: /> time
Ver:
This command displays the current Dos version number. It has no additional
parameters.
Syntax: C: /> ver
Copy Con:
This command is used to create a new file.
Syntax: C: /> copy con file name
Type:
This type command is used to display the contents (data) of an existing file.
Syntax: C: / > type
Edit:
It is a menu driven text editor. It is used to edit the contents of an existing file. We
can also modify and add new data in an existing file.
Syntax: C: / > edit
Copy:
This command is used to copy the contents of a file to another file. This command
requires a source file name and target file name.
Syntax: C: / > copy source – file name target file name.
The above command copies the contents of source file name to target file name. It
target file name doesn’t exist it will create the file and copy the contents. If it is already
existed file it will over write it.

Ren: (or) Rename:


This command is used to rename a file. This command requires an old file name (or)
New file name. When invoked the comm. and changes old file to new.

Y.Saroja, Assistant Professor, CSE Department, CREC 6


Syntax: C: / > Ren old file name New file name

Del (or) Erase:


This command is used to delete a file from memory.
Syntax: C: / > del file name
Cls:
This command is used to clear the screen. It will erase all the characters in the
screen and displays the prompt in the first line of the screen. It has no parameters.
Syntax: C: / > cls
Md (or) Mkdir:
This command is used to create a directory (or ) sub directory.
Syntax: C: / > MD directory – name
CD (or) CHDIR:
This command is used to change (or) activate an existing directory.
Syntax: C: / >CD – Directory – name
RD (or) RMDIR:
This command is used to remove an existing directory. Before using this
command it is important to note that the directory which should be removed should not
contain any files (or) sub directories.
Syntax: C: /> RD directory – name

CD. . :
This command is used to come out from current directory.
Syntax: C: / >CD. .
CD\:
This command is used to come out from all sub directories at once.
Syntax: C : \ > CD\
Dir:
This command is used to display all files and sub – directories under the given directory.
It displays one file on a line with details like file name, extension, size in bytes, data and time
of file creator.
Syntax: C :/> DIR
DIR/P :
This command is used to display the list of files and sub directories page by page.
Syntax: C :/> Dir/P Enter.
DIR/W :
This command is used to display the list of files and sub – directories in a wide format
containing only file names and extensions placing file name side by side.
Syntax: C :/> Dir/w Enter.

Y.Saroja, Assistant Professor, CSE Department, CREC 7


B). Write a c program to
i)Find the sum of three numbers
Aim:- to write a program find the sum of three numbers
Program Description:-
In this program , we accept three numbers and find their sum.

Alorithm:
1. Start
2. Read a,b,c
3. Sum=a+b+c
4. Print ‘sum’
5. Stop

Source Code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum;
clrscr();
printf("\nEnter three numbers :");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
printf("\n Sum of %3d,%3d and %3d is=%4d",a,b,c,sum);
getch();
}

Input:-
Enter three numbers :24 32 65
OUTPUT:-
Sum of 24, 32 and 65 is= 121

Input:-
Enter three numbers :3 5 6
OUTPUT:-
Sum of 3, 5 and 6 is= 14

Input:-
Enter three numbers :1 2 3
OUTPUT:-
Sum of 1 , 2 and 3 is= 6

Input:-
Enter three numbers :4 6 2
OUTPUT:-
Sum of 4, 6 and 2 is= 12

Y.Saroja, Assistant Professor, CSE Department, CREC 8


ii) Write a c program to exchange(swap) of two numbers with using third variable.

ALORITHM
1. START
2. read x,y
3. temp=x
4. x=y
5. y=temp
6. print x,y
7. STOP

PROGRAM
#include <stdio.h>

int main()
{
int x, y, temp;

printf("Enter the value of x and y\n");


scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n",x,y);

temp = x;
x = y;
y = temp;

printf("After Swapping\nx = %d\ny = %d\n",x,y);

return 0;
}
O/P

Enter the value of x and y 4 5


Before swapping
X=4
Y=5
After swapping
X=5
Y=4

Y.Saroja, Assistant Professor, CSE Department, CREC 9


iii) Write a c program to exchange(swap) of two numbers without using third variable.
Aim:- to write program to exchange of two numbers
ALORITHM
1. START
2. read a,b
3. a=a+b;
4. b=a-b;
5. a=a-b;
6. print a,b
7. STOP

Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nEnter two numbers :");
scanf("%d%d",&a,&b);
printf("\n Before swapping, values are a=%3d and b=%3d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n After swapping, values are a=%3d and b=%3d",a,b);
getch();
}
Input:-
Enter two numbers :7 9
OUTPUT:-
Before swapping, values are a=7 and b=9
After swapping, values are a=9 and b=7

iv) Print the size of all data types.


Aim:- To write a program to read and print variable values of all data types of C language and also
find their sizes.

Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
long int l;
unsigned int ul;

Y.Saroja, Assistant Professor, CSE Department, CREC 10


float f;
double d;
char c;
clrscr();
printf("\nEnter a integer value :");
scanf("%d",&a);
printf("\nEnter an unsigned value :");
scanf("%u",&ul);
printf("\nEnter a long integer value :");
scanf("%ld",&l);
printf("\nEnter a float value :");
scanf("%f",&f);
printf("\nEnter a double value :");
scanf("%lf",&d);
flushall();
printf("\nEnter a character :");
c=getchar();//scanf("%c",&c);
printf("\n Given values are:\n");
printf("\n Integer =%4d and its size=%4d",a,sizeof(a));
printf("\n Unsigned Integer =%4u and its size=%4d",ul,sizeof(ul));
printf("\n Long Integer =%ld and its size=%4d",l,sizeof(l));
printf("\n Float =%6.2f and its size=%4d",f,sizeof(f));
printf("\n Double =%lf and its size=%4d",d,sizeof(d));
printf("\n Character =%c and its size=%4d",c,sizeof(c));
getch();
}

Input:-
Enter a integer value :6325
Enter an unsigned value :-958
Enter a long integer value :263547
Enter a float value :36.2541
Enter a double value :23.65874
Enter a character :k

OUTPUT:-
Given values are:
Integer =6325 and its size= 2
Unsigned Integer =64578 and its size=2
Long Integer =263547 and its size=4
Float = 36.25 and its size= 4
Double =23.658740 and its size=8
Character =k and its size=1

Y.Saroja, Assistant Professor, CSE Department, CREC 11


EXPERIMENT-2
BASIC C PROGRAMS
A) Write a C program to find the Priority and associativity of operators using expressions. Take the
expressions with different operators.
ALGORITHM
1. START
2. a = 20, b = 10, c = 15,d = 5
3. e = (a + b) * c / d
4. print e
5. e = ((a + b) * c) / d
6. print e
7. e = (a + b) * (c / d)
8. print e
9. e = a + (b * c) / d
10. print e
11. STOP

PROGRAM
#include <stdio.h>
main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;

e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );

e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );

e = (a + b) * (c / d); // (30) * (15/5)


printf("Value of (a + b) * (c / d) is : %d\n", e );

e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}
OUTPUT:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50

Y.Saroja, Assistant Professor, CSE Department, CREC 12


B) Write a C program to swap two numbers using bitwise operators.
ALORITHM
1. START
2. i = 65,k = 120
3. Print i,k
4. i = i ^ k;
5. k = i ^ k;
6. i = i ^ k;
7. Print i,k
8. STOP
PROGRAM
#include <stdio.h>
int main()
{
int i = 65;
int k = 120;
printf(" value of i=%d k=%d before swapping", i, k);
i = i ^ k;
k = i ^ k;
i = i ^ k;
printf("value of i=%d k=%d after swapping", i, k);
return 0;
}
O/P:
Value of i=65 k=120 before swapping
Value of i=120 k=65

Value of i=6 k=10 before swapping


Value of i=10 k=6

Value of i=5 k=20 before swapping


Value of i=20 k=5

Value of i=651 k=122 before swapping


Value of i=122 k=651

EXPERIMENT-3
CONTROL STATEMENTS
A) Write a C program to find whether the given number is odd or even.
ALGORITHM
1. START
2. Read n
3. if(n%2==0) then step4 else step5
4. print “Given number is even number”

Y.Saroja, Assistant Professor, CSE Department, CREC 13


5. print “Given number is odd number”
6. STOP

PROGRAM
#include<stdio.h>
main()
{
int n;
clrscr();
printf(“Enter the number:”);
scanf(“%d”,&n);
if(n%2==0)
printf(“\nGiven number is even number”);
else
printf(“\nGiven number is odd number:”);
getch();
}
OUTPUT
Enter the number:5
Given number is odd number

Enter the number :6


Given number is even number

Enter the number:7


Given number is odd number

Enter the number :10


Given number is even number

B) Write a C program to find the Maximum of two numbers.


ALGORITHM
1. START
2. READ a,b
3. if(a>b) then step4 else step5
4. First number is greater than Second number
5. Second number is greater than First number
6. STOP

PROGRAM
#include<stdio.h>
main()
{
int a,b;
clrscr();

Y.Saroja, Assistant Professor, CSE Department, CREC 14


printf(“Enter the two number:”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“\nFirst number is greater than Second number”);
else
printf(“\n Second number is greater than First number”);
getch();
}
OUTPUT
Enter the two number : 5 4
First number is greater than Second number

Enter the two number : 4 5


Second number is greater than First number

Enter the two number : 9 7


First number is greater than Second number
Enter the two number : 7 9
Second number is greater than First number
C) Write a C program to find the Maximum of three numbers.
ALGORITHM
1. START
2. READ a,b,c
3. if((a>b)&&(a>c)) then step4 else step5
4. First number is greater number
5. if((b>a)&&(b>c)) then step6 else step7
6. Second number is greater number
7. if((c>a)&&(c>b)) then step8 else step9
8. Third number is greater number
9. STOP
PROGRAM

#include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf(“Enter the three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
printf(“\n First number is greater number”);
else
if((b>a)&&(b>c))
printf(“\n Second number is greater number”);
else

Y.Saroja, Assistant Professor, CSE Department, CREC 15


if((c>a)&&(c>b))
printf(“\n Third number is greater number”);
}
OUTPUT
Enter the three numbers : 7 9 10
Third number is greater number

Enter the three numbers : 17 19 10


Second number is greater number

Enter the three numbers : 77 9 10


First number is greater number

Enter the three numbers : 7 2 0


First number is greater number

D) Write a C program to print ‘hello world’ without using semicolon.


PROGRAM
#include <stdio.h>
int main() {
/* printf - prints given data */
if (printf("Hello World\n")) {
// empty if statement
}
return 0;
}
OUTPUT
Hello World
E) Write a C program to find whether the given number is odd or even using bitwise operator.
ALGORITHM
1. START
2. READ num
3. if(num & 1) then step4 else step5
4. print num is odd
5. print num is even
6. STOP
PROGRAM
#include <stdio.h>

void main()
{
int num;

// Input a number from user


printf("Enter any number: ");

Y.Saroja, Assistant Professor, CSE Department, CREC 16


scanf("%d", &num);

if(num & 1)
{
printf("%d is odd.", num);
}
else
{
printf("%d is even.", num);
}

OUTPUT

Enter the number:5


Given number is odd number

Enter the number :6


Given number is even number

Enter the number:7


Given number is odd number

Enter the number :10


Given number is even number

F) Write a C program to find the maximum of two numbers using Conditional operator.
ALGORITHM
1. START
2. READ a,b
3. C=( a>b)?a:b
4. Print c
5. STOP

PROGRAM
#include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf(“Enter the two numbers:”)
scanf(“%d%d”,&a,&b);
c=( a>b)?a:b;
printf(“\n%d is the maximum number “,c);
getch();

Y.Saroja, Assistant Professor, CSE Department, CREC 17


}
OUTPUT
Enter the two number 6 7
7 is the maximum number

Enter the two number 7 9


9 is the maximum number

Enter the two number 9 10


10 is the maximum number

Enter the two number 10 8


10 is the maximum number

G) Write a program which takes two integers and one arithmetic operator from the user, and
performs the operation and then prints the result by using switch-case. (Operators : +, -, *, /, %)
ALGORITHM
1. START
2. READ a,b,op
3. Switch(op)
a)case op=='+':
i)print ‘a+b’;
ii)break;
b) case op=='-':
i)print ‘a-b’;
ii)break;
c) case op=='*':
i)print ‘a*b’;
ii)break;
d) case op=='/':
i)print ‘a/b’;
ii)break;
e) case op=='%':
i)print ‘a%b’;
ii)break;
4. STOP

PROGRAM
#include<stdio.h>
main()
{
int a,b;
char op;
clrscr();
printf("Enter the two numbers:");

Y.Saroja, Assistant Professor, CSE Department, CREC 18


scanf("%d%d",&a,&b);
printf("\nEnter the operator:");
scanf("%c",&op);
switch(op)
{
case '+':printf("\nAdding two numbers:%d",a+b);
break;
case '-':printf("\nSubstracting two numbers:%d",a-b);
break;
case '*':printf("\nmultiplying two numbers:%d",a*b);
break;
case '/':printf("\nDividing two numbers:%d",a/b);
break;
case '%':printf("\nModulous operation:%d",a%b);
break;
}
getch();
}
OUTPUT
Enter the two numbers:6 3
Enter the operator: +
Adding two numbers:9

Enter the two numbers:6 3


Enter the operator: -
Substracting two numbers:3

Enter the two numbers:6 3


Enter the operator: *
multiplying two numbers:18

Enter the two numbers:6 3


Enter the operator: /
Dividing two numbers:2

EXPERIMENT-4
ITERATION STATEMENTS
A) Write a C program to generate the required multiplication table.
ALGORITHM
1. START
2. READ n
3. i=1
4. repeat until i<=10 step 5 otherwise step6
5. print n*i

Y.Saroja, Assistant Professor, CSE Department, CREC 19


6. STOP

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("enter n values");
scanf("%d",&n);
printf("the multiplication table of %dis:\n",n);
for(i=1;i<=10;i++)
{
printf("%d*%d=%d\n",n,i,n*i);
}
getch();
}
/**********************OUTPUT***********
enter n values 5
the multiplication table of 5is:5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

/**********************OUTPUT***********
enter n values 4
the multiplication table of 4is:
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40

Y.Saroja, Assistant Professor, CSE Department, CREC 20


/**********************OUTPUT***********
enter n values 3
the multiplication table of 3is:
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

B) Write a C program to find the Factorial of a given number.

ALGORITHM
1. START
2. READ n
3. f=1,i=1
4. Repeat until i<=n step5 otherwise step6
5. a)f=f*I
b)i=i+1

6. print ‘f’
7. STOP

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int n,f=1,i=1;
clrscr();
printf("\n Enter a number:");
scanf("%d",&n);
while(i<=n)
{
f*=i;
i++;
}

Y.Saroja, Assistant Professor, CSE Department, CREC 21


printf("Factorial of %3d is = %5d",n,f);
getch();
}
Input
Enter a number:6
OUTPUT
Factorial of 6 is = 720

Input
Enter a number:5
OUTPUT
Factorial of 5 is = 120

Input
Enter a number:7
OUTPUT
Factorial of 7 is = 5040

Input
Enter a number:8
OUTPUT
Factorial of 8 is = 40320

C) Write a C program to check whether the given number is prime or not.


ALGORITHM
1. START
2. READ n
3. i=1
4. Repeat until i<=n step5 otherwise step6
5. a) if(n%i==0) then step 5b) else step6
b)k=k+1
6. i=i+1
7. if(k==2)then step8 else step9
8. print “Given number is a prime number”
9. print “Given number is not a prime number”
10. STOP

PROGRAM
#include<stdio.h>

Y.Saroja, Assistant Professor, CSE Department, CREC 22


#include<conio.h>
void main()
{
int n,i,k=0;
clrscr();
printf("\n Enter a number :");
scanf("%d",&n);
i=1;
while(i<=n)
{
if(n%i==0)
k++;
i++;
}
if(k==2)
printf("\n Given number is a prime number");
else
printf("\n Given number is not a prime number");
}
Input:
Enter a number : 6

OUTPUT:
Given number is not a prime number

Input:
Enter a number :11
OUTPUT:
Given number is a prime number

Input:
Enter a number : 9

OUTPUT:
Given number is not a prime number

Input:
Enter a number :13
OUTPUT:
Given number is a prime number

Y.Saroja, Assistant Professor, CSE Department, CREC 23


EXPERIMENT-5
ITERATION STATEMENTS
A) Write a C program to find the sum of the digits of a number.

ALGORITHM
1. START
2. READ n
3. Repeat until n>0 step4 otherwise step5
4.
a) r=n%10;
b) s=s+r;
c)n=n/10;
5. Print ‘s’
6. STOP

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n ,r ,s=0;
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("\n Sum of the digits is = %4d",s);
getch();
}

Input:
Enter the number : 256
OUTPUT:
Sum of the digits is = 13
Input:
Enter the number : 113
OUTPUT:
Sum of the digits is = 5
Input:
Enter the number : 222
OUTPUT:
Sum of the digits is = 6

Y.Saroja, Assistant Professor, CSE Department, CREC 24


B) Write a C program to find whether the given integer is a Palindrome or not.

ALGORITHM
1. START
2. READ n
3. k=n
4. Repeat until n>0 step5 otherwise step6
5. a)r=n%10;
b)s=(s*10)+r;
c)n=n/10;
6. if(k==s)then step7 else step8
7. print ‘Given number is Palindrome’
8. print ‘Given number is not Palindrome’
9. STOP

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0,k;
clrscr();
printf("\n Enter a number:");
scanf("%d",&n);
k=n;
while(n>0)
{
r=n%10;
s=(s*10)+r;
n=n/10;
}
if(k==s)
printf("\n Given number is Palindrome");
else
printf("\n Given number is not a palindrome");
}
Input:
Enter a number:125

OUTPUT:
Given number is not a palindrome
Input:
Enter a number:121

Y.Saroja, Assistant Professor, CSE Department, CREC 25


OUTPUT:
Given number is a palindrome

Input:
Enter a number:454

OUTPUT:
Given number is a palindrome
Input:
Enter a number:456

OUTPUT:
Given number is not a palindrome

C) Write a C program to generate Fibonacci numbers in the given range.

ALGORITHM
1. START
2. READ n
3. t1=0,t2=1,nextterm=0
4. print t1,t2
5. nextterm=t1+t2
6. Repeat until nextterm<=n step7 otherwise step8
7. a)print nextterm
b)t1 = t2;
c)t2 = nextTerm;
d)nextTerm = t1 + t2;

8. STOP

PROGRAM
#include <stdio.h>
int main()
{
int t1 = 0, t2 = 1, nextTerm = 0, n;

printf("Enter a positive number: ");


scanf("%d", &n);

// displays the first two terms which is always 0 and 1


printf("Fibonacci Series: %d, %d, ", t1, t2);

nextTerm = t1 + t2;

while(nextTerm <= n)
{
printf("%d, ",nextTerm);

Y.Saroja, Assistant Professor, CSE Department, CREC 26


t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

OUTPUT
Enter a positive integer: 100
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
Enter a positive integer: 7
Fibonacci Series: 0, 1, 1, 2, 3, 5,
Enter a positive integer: 9
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8,
Enter a positive integer: 13
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13,

EXPERIMENT-6
NESTED LOOPS

A) Write a C program to print the following pattern.


1
22
3 3 3

ALGORITHM
1. START
2. READ rows
3. i=1
4. Repeat until i<=rows step5 otherwise step6
5. a)J=1
b)Repeat until j<=i step5c otherwise step6d
c) i)Print i
ii) j=j+1
d)i=i+1
6. STOP

PROGRAM
#include <stdio.h>
int main()
{
int rows, i, j, number= 1;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i <= rows; i++)
{
for(j=1; j <= i; ++j)

Y.Saroja, Assistant Professor, CSE Department, CREC 27


{
printf("%d ", i);

printf("\n");
}

return 0;
}
OUTPUT
Enter number of rows:3
1
22
3 3 3

Enter number of rows:4


1
22
3 3 3
4 4 4 4
Enter number of rows:5
1
22
3 3 3
4 4 4 4
Enter number of rows:6
1
22
3 3 3
4 4 44
5 5 5 55
6 6 6 6 66

B) print multiplication tables upto the given table.

ALGORITHM
1. START
2. READ N
3. i=1
4. Repeat until i<=n step5 otherwise step6
5. a)J=1
b)Repeat until j<=10 step5c otherwise step5d
c) i)print i, j, i * j
ii)j=j+1

Y.Saroja, Assistant Professor, CSE Department, CREC 28


d)i=i+1
6. STOP

PROGRAM
#include<stdio.h>
int main () {
int i, j, n;
printf("Enter the value for n:");
scanf("%d", &n);

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


printf("Multiplicaton table for %d\n", i);
for (j = 1; j <= 10; j++) {
/* multiplication OUTPUT */
printf("%2d X %2d = %3d\n", i, j, i * j);
}
printf("\n");
}
return 0;
}
OUTPUT

Enter the value for n:4


1 x 1=1 2 x 1 =2 3 x 1=3 4 x 1=4
1 x 2=2 2 x 2=4 3 x 2=6 4 x 2=8
1 x 3=3 2 x 3=6 3 x 3=9 4 x 3=12
1 x 4=4 2 x 4=8 3 x 4=12 4 x 4=16
1 x 5=5 2 x 5=10 3 x 5=15 4 x 5=20
1 x 6=6 2 x 6=12 3 x 6=18 4 x 6=24
1 x 7=7 2 x 7=14 3 x 7=21 4 x 7=28
1 x 8=8 2 x 8=16 3 x 8=24 4 x 8=32
1 x 9=9 2 x 9=18 3 x 9=27 4 x 9=36
1 x 10=10 2 x 10=20 3 x 10=30 4 x 10=40

C) Write a C program to print Series of prime numbers in the given range.


ALGORITHM
1. START
2. READ n
3. i=1,k=0
4. Repeat until i<=n step5 otherwise step6
5. a)k=0
b) j=2
c) Repeat until j<n step5cA otherwise step d

Y.Saroja, Assistant Professor, CSE Department, CREC 29


A) if(i%j==0)then step Ai
i) k=1
B)j=j+1
d) if(k==1) then step5di
i) print i
e)i=i+1
6. STOP
PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k=0;
clrscr();
printf("Enter n value upto where prime numbers to be found: ");
scanf("%d",&n);
i=2;
while(i<=n)
{
k=0;
j=2;
while(j<n)
{

if(i%j==0)
k=1;
j++;
}
if(k==1)
printf(“%4d” , i);
i++;
}
}

Input :-
Enter n value upto where prime numbers to be found: 10

OUTPUT:
Required prime numbers are :
2 3 5 7

Y.Saroja, Assistant Professor, CSE Department, CREC 30


EXPERIMENT-7
NESTED LOOPS

A) Write a C program to check given number is strong number or not.

A number is called strong number if sum of the factorial of its digit is equal to number itself.
For example: 145 since
1! + 4! + 5! = 1 + 24 + 120 = 145

ALGORITHM
1. START
2. READ num
3. sum=0,temp=num
4. Repeat until num!=0 step5 otherwise step6
5. A. i=1,f=1
B. r=num%10
C. Repeat until i<=r stepD otherwise stepE
D. a) f=f*i
b) i=i+1
E. sum=sum+f;
F. num=num/10;
6. if(sum==temp) then step7 else step8
7. print ‘temp’ is a strong number
8. print ‘temp’ is a strong number
9. STOP

PROGRAM
#include<stdio.h>
int main(){
int num,i,f,r,sum=0,temp;

printf("Enter a number: ");


scanf("%d",&num);

temp=num;
while(num){
i=1,f=1;
r=num%10;

while(i<=r){
f=f*i;
i++;
}
sum=sum+f;

Y.Saroja, Assistant Professor, CSE Department, CREC 31


num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);

return 0;
}

OUTPUT:
Enter a number: 145
145 is a strong number
Enter a number: 40585
40585 is a strong number

B) Write a C program to evaluate the sum of the following series up to ‘n’ terms
ex = 1 + x + x2/2! + x3 /3! + x4 /4! + --------

ALGORITHM
1. START
2. READ x,n
3. sum=1.0, m=1, p=x, i=2
4. Repeat until i<=n step5 otherwise step6
5. A) s=(float)p/m;
B) sum=sum+s;
C) m=m*i;
D) p=x*p;
6. print “sum”
7. STOP

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
float sum=1.0,s;
int n,i,x,p,m=1;
clrscr();
printf("\n Enter x value :");
scanf("%d",&x);
printf("\n Enter n value :");
scanf("%d",&n);
p=x;

Y.Saroja, Assistant Professor, CSE Department, CREC 32


for(i=2;i<=n;i++)
{
s=(float)p/m;
sum=sum+s;
m=m*i;
p=x*p;
}
printf("\n Sum of the given series is :%6.4f",sum);
getch();
}

Input:-
Enter x value :2
Enter n value :3

OUTPUT:-
Sum of the given series is :5.0000

EXPERIMENT-8
ARRAYS

A) Write a C program to find the sum of positive and negative numbers in a given set(Array) of
numbers.

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,sp=0,sn=0,i;
clrscr();
printf("\n Enter size of the array :");
scanf("%d",&n);
printf("\n Enter the values of array :");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
if(a[i]<0) // negative
sn=sn+a[i];
else // positive
sp=sp+a[i];
}

Y.Saroja, Assistant Professor, CSE Department, CREC 33


printf("\n In given numbers,sum of positive values :%d",sp);
printf("\n In given numbers,sum of negative values :%d",sn);
}
Input:-
Enter size of the array:9

Enter the values of array :


8 -5 4 2 -7 -23 65 -32 24

OUTPUT:-
In given numbers, sum of positive values : 103
In given numbers, sum of negative values :-67

B) Write a C program to read two matrices and find


i) Sum.
ii) Product and display the result in the matrix form.
PROGRAM

#include<stdio.h>
#include<conio.h>
void input(int a[8][8],int r,int c);
void OUTPUT(int a[8][8],int r,int c);
void add(int a[8][8],int b[8][8],int s[8][8],int r,int c);
void multiply(int a[8][8],int b[8][8],int s[8][8],int r,int c,int p);
void main()
{
int a[8][8],b[8][8],s[8][8],r1,c1,r2,c2,ch;
clrscr();
printf("\n Enter rows and columns of first matrix:");
scanf("%d%d",&r1,&c1);
printf("\n Enter rows and columns of second matrix:");
scanf("%d%d",&r2,&c2);
printf("\n Enter the elements of first matrix:");
input(a,r1,c1);
printf("\n Enter the elements of second matrix:");
input(b,r2,c2);
printf("\n Elements of first matrix are:");
OUTPUT(a,r1,c1);
printf("\n Elements of second matrix are:");
OUTPUT(b,r2,c2);

if((r1==r2)&&(c1==c2))
{
add(a,b,s,r1,c1);

Y.Saroja, Assistant Professor, CSE Department, CREC 34


printf("\n Sum of the two matrices is:");
OUTPUT(s,r1,c1);
}
else
{
printf("\n Matrix addition is not possible");
}
if(c1==r2)
{
multiply(a,b,s,r1,c2,c1);
printf("\n Product of the two matrices is:");
OUTPUT(s,r1,c2);
}
else
{
printf("\n Matrix multiplication is not possible");
}

getch();
}
void input(int a[8][8],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
void OUTPUT(int a[8][8],int r,int c)
{
int i,j;
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%4d",a[i][j]);
}
printf("\n ");
}
}
void add(int a[8][8],int b[8][8],int s[8][8],int r,int c)
{
int i,j;
for(i=0;i<r;i++)

Y.Saroja, Assistant Professor, CSE Department, CREC 35


for(j=0;j<c;j++)
s[i][j]=a[i][j]+b[i][j];
}
void multiply(int a[8][8],int b[8][8],int s[8][8],int r,int c,int p)
{
int i,j,k;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
s[i][j]=0;
for(k=0;k<p;k++)
s[i][j]=s[i][j]+(a[i][k]*b[k][j]);
}
}

Input
Enter rows and columns of first matrix:3 3
Enter rows and columns of second matrix:3 3
Enter the elements of first matrix: 5 2 6 9 8 7 1 2 1
Enter the elements of second matrix:6 4 7 8 4 1 3 2 5
OUTPUT
Elements of first matrix are:
5 2 6
9 8 7
1 2 1
Elements of second matrix are:
6 4 7
8 4 1
3 2 5
Sum of the two matrices is:
11 6 13
17 12 8
4 4 6
Product of the two matrices is:
64 40 67
139 82 106
25 14 14

EXPERIMENT-9
ARRAYS
A) Write a C program to read matrix and perform the following operations
i) Find the sum of Diagonal Elements of a matrix.
ii) Print Transpose of a matrix.
iii) Print sum of even and odd numbers in a given matrix.

Y.Saroja, Assistant Professor, CSE Department, CREC 36


PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[8][8],s=0,se=0,so=0,n,i,j;
clrscr();
printf("\n Enter the order of the matrix :");
scanf("%d",&n);
printf("\n Enter the elemens of the matrix :");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(a[i][j]%2==0) // even number
se=se+a[i][j];
else
so=so+a[i][j];
if( (i==j) || (i+j)==(n+1) ) //diagonal elements
s=s+a[i][j];
}
printf("\n Sum of all diagonal elements is :%d",s);
printf("\n Sum of even numbers in matrix is :%d",se);
printf("\n Sum of odd numbers in matrix is :%d",so);
printf("\n Transpose of the given matrix is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d",a[j][i]);
printf("\n");
}
getch();
}

Input:-
Enter the order of the matrix :3
Enter the elemens of the matrix :
5 2 6
8 1 9
4 3 2

Y.Saroja, Assistant Professor, CSE Department, CREC 37


OUTPUT:-
Sum of all diagonal elements is :18
Sum of even numbers in matrix is :22
Sum of odd numbers in matrix is :18
Transpose of the given matrix is:
5 8 4
2 1 3
6 9 2

EXPERIMENT-10
STRINGS

A) Write a C program to read two strings and perform the following operations without
using built-in string Library functions.
i) String length determination.
ii) Compare Two Strings.
iii) Concatenate Two Strings.
iv) String reversing

PROGRAM
#include<stdio.h>
#include<conio.h>
int stringlength(char *s);
int stringcompare(char *s1,char *s2,int ,int)
void stringconcatenate(char *s1,char *s2,int,int);
void stringreverse(char *s,int);
void main()
{
char str1[20],str2[30];
int n,i,k,l1,l2;
clrscr();
printf("\n Enter the first string:");
scanf("%s",str1);
printf("\n Enter the second string: ");
scanf("%s",str2);
l1=stringlength(str1);
printf("\n Length of first string is=%d",l1);
l2=stringlength(str2);
printf("\n Length of Second string is=%d",l2);
k=stringcompare(str1,str2,l1,l2);
if(k==1)
printf("\n Two strings are equal\n");

Y.Saroja, Assistant Professor, CSE Department, CREC 38


else
{
printf("\n Two strings are not equal");
printf("\n After concatenating two strings, resultant string is:");
stringconcatenate(str1,str2,l1,l2);
}
printf("\n Reverse of first string is=");
stringreverse(str1,l1);
printf("\n Reverse of second string is=");
stringreverse(str2,l2);
getch();
}
int stringlength(char *str)
{
int k;
for(k=0;str[k]!='\0';k++);
return(k);
}
int stringcompare(char *s1,char *s2,int l1,int l2)
{
int i,t=0;
if(l1==l2)
{
for(i=0;i<l1;i++)
if(s1[i]!=s2[i])
t=1;
if(t==0)
return(1);
else
return(0);
}
else
return(0);
}

void stringconcatenate(char *s1,char *s2,int l1,int l2)


{
int i;
char str[60];
for(i=0;i<l1;i++)
str[i]=s1[i];
str[l1]=' ';
l1++;
for(i=0;i<l2;i++)
str[l1+i]=s2[i];

Y.Saroja, Assistant Professor, CSE Department, CREC 39


printf("%s",str);
}
void stringreverse(char *s,int l)
{
int i;
for(i=l-1;i>=0;i--)
printf("%c",s[i]);
}
Input:-

Enter the first string : krishna


Enter the second string : kishore

OUTPUT:-

Length of first string is=7


Length of Second string is=7
Two strings are not equal
After concatenating two strings, resultant string is :krishna kishore
Reverse of first string is=anhsirk
Reverse of second string is=erohsik

Input:-
Enter the first string:balu
Enter the second string: balu
OUTPUT:-
Length of first string is=4
Length of Second string is=4
Reverse of first string is=ulab
Reverse of second string is=ulab

B) Write a C program to accept a line of characters and print the number of Vowels, Consonants,
blank spaces, digits and special characters.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char line[80];
int i,vow=0,con=0,digit=0,spch=0,spaces=0;
clrscr();
printf("\n Enter a line of text :");
scanf("%[^\n]",line);

Y.Saroja, Assistant Professor, CSE Department, CREC 40


i=0;
while(line[i]!='\0')
{
line[i]=tolower(line[i]);
if(isalpha(line[i])!=0)
{
if(line[i]=='a'||line[i]=='e'||line[i]=='i'||line[i]=='o'||line[i]=='u')
vow++;
else
con++;
}
else if(isdigit(line[i])!=0)
digit++;
else if(line[i]==' ' || line[i]=='\t')
spaces++;
else
spch++;
i++;
}
printf("\n In the given line of text :\n");
printf("\n No. of vowels is=%d",vow);
printf("\n No. of consonants is=%d",con);
printf("\n No. of digits is=%d",digit);
printf("\n No. of spaces is=%d",spaces);
printf("\n No. of special characters is=%d",spch);
getch();
}

Input:-
Enter a line of text : This is 21st century #!

OUTPUT:-
In the given line of text :
No. of vowels is=4
No. of consonants is=11
No. of digits is=2
No. of spaces is=4
No. of special characters is=2

Y.Saroja, Assistant Professor, CSE Department, CREC 41


C) Write a C program to read a set of strings and sort them in alphabetical order.
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[15][20],temp[20];
int i,j,n;
clrscr();
printf("\n Enter number of strings :");
scanf("%d",&n);
printf("\n Enter the strings:\n");
for(i=1;i<=n;i++)
scanf("%s",str[i]);
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
printf("\n After sorting strings are :\n");
for(i=1;i<=n;i++)
printf("%s\n",str[i]);
getch();
}
Input:-
Enter number of strings :6
Enter the strings
ramu
krishna
lakshman
balaji
deepak
abhiram
OUTPUT:-
After sorting strings are :
abhiram
balaji
deepak
krishna
lakshman
ramu

Y.Saroja, Assistant Professor, CSE Department, CREC 42


EXPERIMENT-11
POINTERS
A) Write a C program to exchange two numbers using pointers.

PROGRAM
#include<stdio.h>
#include<conio.h>
void exchange(int *,int *);
void main()
{
int a,b;
clrscr();
printf("\n Enter two numbers :");
scanf("%d%d",&a,&b);
printf("\n Before exchanging ,a=%d and b=%d",a,b);
exchange(&a,&b);
printf("\n After exchanging ,a=%d and b=%d",a,b);
getch();
}
void exchange(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Input:-
Enter two numbers :5 9

OUTPUT:-
Before exchanging ,a=5 and b=9
After exchanging ,a=9 and b=5

B) Write a program to print the elements of an array in reverse order using pointers.

PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAX 30

void main() {
int size, i, arr[MAX];
int *ptr;
clrscr();

Y.Saroja, Assistant Professor, CSE Department, CREC 43


ptr = &arr[0];

printf("\nEnter the size of array : ");


scanf("%d", &size);

printf("\nEnter %d integers into array:", size);


for (i = 0; i < size; i++) {
scanf("%d", ptr);
ptr++;
}

ptr = &arr[size - 1];

printf("\nElements of array in reverse order are :");

for (i = size - 1; i >= 0; i--) {


printf("\nElement%d is: %d ", i, *ptr);
ptr--;
}

getch();
}
OUTPUT:
Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 3 is : 44
Element 2 is : 33
Element 1 is : 22
Element 0 is : 11
EXPERIMENT-12
FUNTIONS

A) Write a C program to illustrate the following types of functions


i) Function with no arguments and no return values

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;

Y.Saroja, Assistant Professor, CSE Department, CREC 44


void sum();
clrscr();
printf("enter a, b values:");
scanf("%d%d", &a, &b);
c= a+b;
printf("\n the sum of two numbers in main function is : %d", c);
sum();
getch();
}
void sum()
{
int x,y,z;
printf("\n enter x,y values for sum function:");
scanf("%d%d", &x,&y);
z=x+y;
printf("\nthe sum of two numbers in sum function is:%d", z);
}

OUTPUT
enter a, b values 12 13
the sum of two numbers in main function is : 25
enter x,y values for sum function: 23 23
the sum of two numbers in sum function is:46

enter a, b values 45 45
the sum of two numbers in main function is : 90
enter x,y values for sum function: 35 35
the sum of two numbers in sum function is:70

enter a, b values 452 452


the sum of two numbers in main function is : 904
enter x,y values for sum function: 352 352
the sum of two numbers in sum function is:704

ii) Function with arguments and no return value

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
void sum(int,int);

Y.Saroja, Assistant Professor, CSE Department, CREC 45


clrscr();
printf("enter a, b values:");
scanf("%d%d", &a, &b);
c= a+b;
printf("\n the sum of two numbers in main function is : %d", c);
sum(a,b);
getch();
}
void sum(int x, int y)
{
int z;
z=x+y;
printf("\nthe sum of two numbers in sum function is: %d", z);
}

OUTPUT
enter a, b values: 30 20
the sum of two numbers in main function is : 50
the sum of two numbers in sum function is: 50

enter a, b values: 40 20
the sum of two numbers in main function is : 60
the sum of two numbers in sum function is: 60

enter a, b values: 4000 2000


the sum of two numbers in main function is : 6000
the sum of two numbers in sum function is: 6000

iii) Function without arguments and with return value

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
int sum();
clrscr();
printf("enter a, b values:");
scanf("%d%d", &a, &b);
c= a+b;
printf("\n the sum of two numbers in main function is : %d", c);
d=sum();
printf("\nthe sum of two numbers from sum function is:%d", d);
getch();

Y.Saroja, Assistant Professor, CSE Department, CREC 46


}
int sum()
{
int x,y,z;
printf("\nenter x,y values in sum function: ");
scanf("%d%d",&x,&y);
z=x+y;
return(z);
}
OUTPUT:
enter a, b values: 21 21
the sum of two numbers in main function is : 42
enter x,y values in sum function: 65 65
the sum of two numbers from sum function is: 130

enter a, b values: 90 10
the sum of two numbers in main function is : 100
enter x,y values in sum function: 120 30
the sum of two numbers from sum function is: 150

enter a, b values: 140 100


the sum of two numbers in main function is : 240
enter x,y values in sum function: 120 130
the sum of two numbers from sum function is: 250

iv) Function with arguments and with return value

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
int sum(int,int);
clrscr();
printf("enter a, b values:");
scanf("%d%d", &a, &b);
c= a+b;
printf("\n the sum of two numbers in main function is : %d", c);
d=sum(a,b);
printf("\nthe sum of two numbers from sum function is:%d", d);
getch();
}
int sum(int x, int y)
{

Y.Saroja, Assistant Professor, CSE Department, CREC 47


int z;
z=x+y;
return(z);
}
OUTPUT
enter a, b values: 15 15
the sum of two numbers in main function is : 30
the sum of two numbers from sum function is: 30

enter a, b values: 150 150


the sum of two numbers in main function is : 300
the sum of two numbers from sum function is: 300

enter a, b values: 115 115


the sum of two numbers in main function is : 230
the sum of two numbers from sum function is: 230

EXPERIMENT-13
FUNTIONS

A) Write a C program to express a four digit number in words. For example 1546 should be
written as one thousand five hundred and forty six
PROGRAM
#include<stdio.h>
#include<conio.h>
void printword(int);
void printtensword(int);
void printteenword(int);

void main()
{
int n,r,i,th,hun,ten,unit;
clrscr();
printf("\n Enter a four digit number:");
scanf("%d",&n);
th=n/1000;
hun=(n%1000)/100;
ten=(n%100)/10;
unit=n%10;
if(n==0)
printf(" Zero ");
if(th>0)
{
printword(th);

Y.Saroja, Assistant Professor, CSE Department, CREC 48


printf("thousand");
}
if(hun>0)
{
printword(hun);
printf(" hundred and ");
}
if(ten>1 || unit==0)
printtensword(ten);
if(ten==1)
printteenword(unit);// it depends on unit place
if(unit>0 && ten>1)
printword(unit);
getch();
}
void printword(int ch)
{
switch(ch)
{
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;
}
}

void printtensword(int ch)


{
switch(ch)
{
case 2: printf(" Twenty ");

Y.Saroja, Assistant Professor, CSE Department, CREC 49


break;
case 3: printf(" Thrirty ");
break;
case 4: printf(" Forty ");
break;
case 5: printf(" Fifty ");
break;
case 6: printf(" Sixty ");
break;
case 7: printf(" Seventy ");
break;
case 8: printf(" Eighty ");
break;
case 9: printf(" Ninty ");
break;
}
}
void printteenword(int ch)
{
switch(ch)
{
case 1: printf(" Eleven ");
break;
case 2: printf(" Twelve ");
break;
case 3: printf(" Thrirteen ");
break;
case 4: printf(" Forteen ");
break;
case 5: printf(" Fifteen ");
break;
case 6: printf(" Sixteen ");
break;
case 7: printf(" Seventeen ");
break;
case 8: printf(" Eighteen ");
break;
case 9: printf(" Ninteen ");
break;
}
}
Input:-
Enter a four digit number:3265
OUTPUT:-
Three thousand Two hundred and Sixty Five

Y.Saroja, Assistant Professor, CSE Department, CREC 50


B) Write a C program using recursion for finding Factorial of a number
Program
#include<stdio.h>
#include<conio.h>
int rfact(int n)
{
if(n==0 || n==1)
return 1;
else
return(n*rfact(n-1));
}

void main()
{
int n,f;
clrscr();
printf("\n Enter a number:");
scanf("%d",&n);
f= rfact(n);
printf("Factorial of %3d is=%d ",n,f);
getch();
}

Input:-
Enter a number:5

OUTPUT:-
Factorial of 5 is=120
C) Write a C program to illustrate the Dynamic Memory allocation function malloc( )

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *a,n,i;
clrscr();
printf("\n enter size of the an array:");
scanf("%d",&n);
a=(int *)malloc(5*sizeof(int));
if(a==NULL)
{
printf("cant allocate memory address");
exit(0);
}

Y.Saroja, Assistant Professor, CSE Department, CREC 51


printf("allocated memory address are:\n");
for(i=0;i<n;i++)
printf("%u\t",(a+i));
printf("\nenter an array elements:\n");
for(i=0;i<n;i++)
scanf("%d",(a+i));
printf("array elements are\n");
for(i=0;i<n;i++)
printf("%d\t",*(a+i));
free(a);
getch();
}
OUTPUT
enter size of the an array: 5
allocated memory address are:
3744 3746 3748 3750 3752
enter an array elements:
3
4
3
2
1
array elements are
3 4 3 2 1
EXPERIMENT-14
STRUCTURES
A) Declare a structure time that has three fields hr, min, secs. Create two structure variables
start_time and end_time. Input there values from the user. Then if start_time is not equal to
end_time then display HELLO CREC on the screen.
PROGRAM:
#include<stdio.h>
void main()
{
Struct time
{
int hr,min,secs;
}start_time,end_time;
printf(“Enter the starting time :”);
scanf(“%d%d%d”,&start_time.hr, &start_time.min, &start_time.secs);
printf(“Enter the ending time :”);
scanf(“%d%d%d”,&end_time.hr, &end_time.min, &end_time.secs);
if(start_time!=end_time)
printf(“”HELLO CREC”);
else

Y.Saroja, Assistant Professor, CSE Department, CREC 52


printf(“EQUAL”);
}
OUTPUT
Enter the starting time:5 45 2
Enter the ending time:5 45 2
HELLO CREC
Enter the starting time:5 44 7
Enter the ending time:5 45 2
EQUAL

B) Write a C program to read student roll no, name and marks in six subjects for n number of
students and give class of each student by following the required conditions.
PROGRAM
#include <stdio.h>
struct student
{
char name[50];
int roll;
int marks[6];
} s[10];

int main()
{
int i,j,n,sum=0,avg;
clrscr();

printf("Enter information of students:\n");

// storing information
printf("enter no.of students");
scanf("%d",&n);
for(i=0; i<n; ++i)
{
s[i].roll = i+1;

printf("\nFor roll number%d,\n",s[i].roll);

printf("Enter name: ");


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

printf("Enter marks: ");


for(j=0;j<6;j++)
scanf("%d",&s[i].marks[j]);

Y.Saroja, Assistant Professor, CSE Department, CREC 53


printf("\n");
}

printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<n; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
for(j=0;j<6;j++)
sum=sum+s[i].marks[j];
avg=sum/6;
if(avg>=70)
printf("\nFirst class");
else
if(avg<=60 && avg>=50)
printf("\nSecond class");
else
printf("\nThird class");

printf("\n");
}
return 0;
}

C) Write a C program to demonstrate self referential structures.

PROGRAM
typedef struct NODE {
struct NODE *new;
int value;
}Node;

int main(void)
{
Node previous, current;

/* accessing members of 'previous' */


previous.new = &current;
previous.value=100;
/* previous.new is a 'pointer-to-struct NODE' */
current =NULL;
printf(“Enter the value:%d:”,&current.value);
printf(“ The current value:%d”, previous->value);

Y.Saroja, Assistant Professor, CSE Department, CREC 54


OUTPUT
Enter the value:20
The current value:20

EXPERIMENT-15
FILES

A) Write a program to create a file and write some text data on the file. Then display the
contents of the file and also print number of characters, number of words, and number
of lines in the file.
PROGRAM
#include <stdio.h>

int main()
{
FILE *fp;
char filename[100];
char ch;
int linecount, wordcount, charcount;

// Initialize counter variables


linecount = 0;
wordcount = 0;
charcount = 0;

// Prompt user to enter filename


printf("Enter a filename :");
gets(filename);

// Open file in read-only mode


fp = fopen(filename,"r");

// If file opened successfully, then write the string to file


if ( fp )
{
//Repeat until End Of File character is reached.
while ((ch=getc(fp)) != EOF) {
// Increment character count if NOT new line or space
if (ch != ' ' && ch != '\n') { ++charcount; }

// Increment word count if new line or space character


if (ch == ' ' || ch == '\n') { ++wordcount; }

// Increment line count if new line character


if (ch == '\n') { ++linecount; }

Y.Saroja, Assistant Professor, CSE Department, CREC 55


}

if (charcount > 0) {
++linecount;
++wordcount;
}
}
else
{
printf("Failed to open the file\n");
}

printf("Lines : %d \n", linecount);


printf("Words : %d \n", wordcount);
printf("Characters : %d \n", charcount);

getchar();
return(0);
}
a text file named wolf.txt with the following content:

Help Help,
Go away you naughty wolf.
With the above file as input, the program will be show the result as below

Enter a filename :wolf.txt


Lines : 2
Words : 7
Characters : 30

B) Write a C program to merge two files


Source Code
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2,*fp3;
char ch;
clrscr();
fp1=fopen("file1.txt","r");
if(fp1==NULL)
{
printf("Given file does n't exists");
exit(0);
}
fp3=fopen("file3.txt","w");
while(!feof(fp1))
{
ch=getc(fp1);

Y.Saroja, Assistant Professor, CSE Department, CREC 56


putc(ch,fp3);
}
fclose(fp1);
fp2=fopen("file2.txt","r");
if(fp2==NULL)
{
printf("Given file does n't exists");
exit(0);
}
while(!feof(fp2))
{
ch=getc(fp2);
putc(ch,fp3);
}
fcloseall();
}

OUTPUT
C:\TC3\BIN>type file1.txt
This is a demo file
My college name is PBRVITS, Kavali

C:\TC3\BIN>type file2.txt
My Name is Raghu Chandra
I am studying I btech in PBRVITS, Kavali
My branch is CSE

C:\TC3\BIN>type file3.txt
This is a demo file
My college name is PBRVITS, Kavali

My Name is Raghu Chandra


I am studying I btech in PBRVITS, Kavali
My branch is CSE

Y.Saroja, Assistant Professor, CSE Department, CREC 57


EXPERIMENT-16
FILES
A) Write a C program to create a text file and write data on it, then display every 5th
character in that file.

PROGRAM
#include<stdio.h>
void main()
{
char ch;
int loc;
FILE *fp;
fp=fopen(“text.txt”,”r”);
fseek(fp,4,0);
printf(“Location =%d”,ftell(fp));
ch=getc(fp);
printf(“ character is %c”,ch);
}

OUTPUT
text.txt
chadalawada

Location=5
Charater is a

B) Write a program to read student records into a file. Record consists of rollno, name and
marks of a student in six subjects and class. Class field is empty initially. Compute the
class of a student. The calculation of the class is as per CREC rules. Write the first class,
second class, third class and failed students lists separately to another file.

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char rollno[10];
char name[15];
int sub[6];
char grade[15];
}s[20];
void main()

Y.Saroja, Assistant Professor, CSE Department, CREC 58


{
int n,k,i,j,avg,d[20],f[20],se[20],t[20],fail[20],dc,fc,sc,tc,failc;
FILE *fp;
clrscr();
printf("\n Enter no. of students :");
scanf("%d",&n);
printf("\n Enter students details :");
for(i=1;i<=n;i++)
{
printf("\n Enter %d student details :",i);
flushall();
printf("\n Roll No :");
gets(s[i].rollno);
flushall();
printf("Name=");
gets(s[i].name);
printf("\n Enter six subject Marks:");
for(j=1;j<=6;j++)
scanf("%d",&s[i].sub[j]);
}
dc=fc=sc=tc=failc=0;
for(i=1;i<=n;i++)
{
if(s[i].sub[1]<40 || s[i].sub[2]<40 ||s[i].sub[3]<40
||s[i].sub[4]<40 ||s[i].sub[5]<40 ||s[i].sub[6]<40)
{
failc++;
fail[failc]=i;
strcpy(s[i].grade,"Fail");
}
else
{
avg=(s[i].sub[1]+s[i].sub[2]+s[i].sub[3]+s[i].sub[4]
+s[i].sub[5]+s[i].sub[6])/6;

if(avg>=70)
{
dc++;
d[dc]=i;
strcpy(s[i].grade,"Distinction");
}
else if(avg>=60)
{
fc++;
f[fc]=i;

Y.Saroja, Assistant Professor, CSE Department, CREC 59


strcpy(s[i].grade,"First Class");
}
else if(avg>=50)
{
sc++;
se[sc]=i;
strcpy(s[i].grade,"Second Class");
}
else
{
tc++;
t[tc]=i;
strcpy(s[i].grade,"Third Class");
}
}
}
fp=fopen("Results.txt","w");
fputs("Details of Distinction students \n",fp);
fputs("Roll No \t Name \t Grade \n",fp);
for(i=1;i<=dc;i++)
{
k=d[i];
fprintf(fp,"%10s%3s%4s\n",s[k].rollno,s[k].name,s[k].grade);
}
fputs("\nDetails of First class students \n",fp);
fputs("Roll No \t Name \t Grade \n\n",fp);
for(i=1;i<=fc;i++)
{
k=f[i];
fprintf(fp,"%12s%13s%13s\n",s[k].rollno,s[k].name,s[k].grade);
}
fputs("\nDetails of Second class students \n",fp);
fputs("Roll No \t Name \t Grade \n",fp);
for(i=1;i<=sc;i++)
{
k=se[i];
fprintf(fp,"%12s%13s%13s\n",s[k].rollno,s[k].name,s[k].grade);
}
fputs("\nDetails of Third class students \n",fp);
fputs("Roll No \t Name \t Grade \n",fp);
for(i=1;i<=tc;i++)
{
k=t[i];
fprintf(fp,"%12s%13s%13s\n",s[k].rollno,s[k].name,s[k].grade);
}

Y.Saroja, Assistant Professor, CSE Department, CREC 60


fputs("\nDetails of Failed students \n",fp);
fputs("Roll No \t Name \t Grade \n",fp);
for(i=1;i<=failc;i++)
{
k=fail[i];
fprintf(fp,"%12s%13s%13s\n",s[k].rollno,s[k].name,s[k].grade);
}

Input:-
Enter no. of students :5
Enter students details :
Enter 1 student details :
Roll No :108P1A0312
Name=Rakesh
Enter six subject Marks:65 84 73 81 96 77

Enter 2 student details :


Roll No :108P1A0318
Name=Bala Krishna
Enter six subject Marks:42 36 58 64 55 48

Enter 3 student details :


Roll No :108P1A0325
Name=Raghu
Enter six subject Marks:78 84 77 45 55 42

Enter 4 student details :


Roll No :108P1A0342
Name=Krishna
Enter six subject Marks:42 47 43 40 45 51

Enter 5 student details :


Roll No :108P1A0355
Name=Radhika
Enter six subject Marks:55 64 52 51 58 60

OUTPUT:-
C:\TC3\BIN>type results.txt
Details of Distinction students
Roll No Name Grade
108P1A0312 Rakesh Distinction

Details of First class students

Y.Saroja, Assistant Professor, CSE Department, CREC 61


Roll No Name Grade
108P1A0325 Raghu First Class

Details of Second class students


Roll No Name Grade
108P1A0355 Radhika Second Class

Details of Third class students


Roll No Name Grade
108P1A0342 Krishna Third Class

Details of Failed students


Roll No Name Grade
108P1A0318 Bala Krishna Fail

Y.Saroja, Assistant Professor, CSE Department, CREC 62

Das könnte Ihnen auch gefallen