[go: up one dir, main page]

0% found this document useful (0 votes)
23 views57 pages

C Lab Manual - CS3271

Uploaded by

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

C Lab Manual - CS3271

Uploaded by

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

EX 1

PROGRAM USING I/O STATEMENTS AND EXPRESSIONS


DATE:

AIM:

i] Program to display your personal details

ALGORITHM:

Step 1: Start
Step 2: Declare and initialize the variables for name, address, date of birth, mobile
number and age
Step 3: Display name, address, date of birth, mobile number and age
Step 4: End

PROGRAM:

#include<stdio.h>
void main()
{
char name[20]= "SAI RAM";
char address[80]= "west tambharam,chennai";
int date=20;
int month=10;
int year=1990;
int mobile=987456321;
int age=25;
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\n ADDRESS:%s", address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n AGE:%d", age);
printf("\n=====================");
}
OUTPUT:

NAME: SAI RAM


ADDRESS:west tambaram,chennai
DOB:20:10:1990
MOBILE NUMBER:987456321
AGE:25

RESULT

Thus the C Program to display the personal details has been executed and the output was
verified.
AIM:

ii] Program to get the user details and display it.

ALGORITHM:

Step 1: Start
Step 2: Declare the variables for name, address, date, month, year, mobile number,
age.
Step 3: Read values of name, address , date, month, year, mobile number, age
from the user.
Step 4: Display name, address, date, month, year, mobile number, age.
Step 5: End

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char name[20];
char address[80];
int date;
int month;
int year;
long int mobile;
char gender[20];
int age;
printf("\n ENTER YOUR NAME:=");
gets(name);
printf("\nENTER YOUR ADDRESS=");
gets(address);
printf("\nENTER YOUR date/month/year=");
scanf("%d/%d/%d",&date,&month,&year);
printf("\n ENTER YOUR AGE=");
scanf("%d",&age);
printf("\n ENTER YOUR GENDER(MALE/FEMALE)=");
scanf("%s",gender);
printf("\nENTER YOUR MOBILE NUMBER=");
scanf("%ld" ,&mobile);
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\n ADDRESS:%s", address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n AGE:%d", age);
printf("\n GENDER:%s", gender);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n=====================");
return 0;
}

OUTPUT:

ENTER YOUR NAME:=karthikeyan

ENTER YOUR ADDRESS=west tambharam,chennai.

ENTER YOUR date/month/year=03/12/1992

ENTER YOUR AGE=28

ENTER YOUR GENDER(MALE/FEMALE)=MALE

ENTER YOUR MOBILE NUMBER=987654321

=====================
NAME: karthikeyan
ADDRESS:west tambharam,chennai.
DOB:3:12:1992
AGE:28
GENDER:MALE
MOBILE NUMBER:987654321
========================

RESULT:

Thus the C Program to read and display the user details has been executed and the
output was verified.
EX 2A
PROGRAM TO DISPLAY BIGGEST OF TWO NUMBERS

DATE:

AIM:

ii] Program to check biggest of three numbers

ALGORITHM:

Step 1:Start

Step 2:Read three numbers A,B & C

Step 3:If A>B,then go to step 6

Step 4:If B>C,then print B & go to step 8

Step 5:print C is greatest & go to step 8

Step 6:If A>C,then print A is greatest & go to step 8

Step 7:Print C is greatest

Step 8:end

PROGRAM:

#include <stdio.h>
void main()
{
int A,B,C;
printf("Enter 3 integer number \n");
scanf("%d",&A);
scanf("%d",&B);
scanf("%d",&C);
if(A>B){
if(A>C){
printf(" %d is the Greatest Number \n",A);
}
else{
printf("%d is the greatest Number \n",C);
}
}
else{
if(B>C){
printf("%d is the greatest Number \n",B );
}
else{
printf("%d is the greatest Number \n", C);
}
}
}

OUTPUT:

Enter three numbers: -4.5


3.9
5.6
5.60 is the largest number.

RESULT:

Thus the C Program to display the personal details has been executed and the output was
verified.
EX 2B PROGRAM TO CHECK WHETHER THE ENTERED
CHARACTER IS VOWEL OR NOT(USE SWITCH CASE)
DATE:

AIM:

ii] Program to check whether the entered character is vowel or not(Use switch case)

ALGORITHM:

Step 1: Start
Step 2: Declare and initialize the variables
Step 3: Get the input from the user and compare with each cases
Step 4: if match found, print vowel otherwise print consonant
Step 5: End

PROGRAM:

#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
//condition to check character is alphabet or not
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
{

switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a VOWEL.\n",ch);
break;
default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}

return 0;
}

OUTPUT:

Enter a character

E is a vowel

Enter a character

X is a consonant

Enter a character

+ is not an alphabet

RESULT:

Thus the C Program check whether the entered character is vowel or not (Use switch
case) has been executed and the output was verified.
Ex:No:3(a) Write a simple C program to print the numbers from 1 to 10 along with their
Date: squares.

AIM :
To write a C program to print the numbers from 1 to 10 along with their squares.

ALGORITHM:

Step 1 : Start
Step 2 : Declare the variable i
Step 3 : Using for loop calculate and print the squares
Step 4 : Stop

FLOW CHART :

START

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

T
PRINT i*i

STOP
SOURCE CODE :
#include<stdio.h>
#include<conio.h>
Voidmain
{
int i;
clrscr();
printf(“\n The numbers with their squares is: \n”);
for(i = 1; i <= 10; i ++)
printf("%d = %d \n", i, i * i);
getch();
}
Output:

The numbers with their squares is:

1 =1
2 =4
3 =9
4 =16
5 =25
6 =36
7 =49
8 =64
9 =81
10 =100

RESULT:

Thus the given C program to print the numbers from 1 to 10 along with theirsquares has
been executed and verified successfully.
Ex:No:3(b) Write a simple C program to find the sum of ‘n’ numbers

Date: using for, do –while statements.

AIM :

To write a C program to find the sum of „n‟ numbers using for, do – whilestatements.

ALGORITHM:

Step 1 : Start
Step 2 : Declare the variable n, count, sum=0,opStep 3 :
get the value of n and op
Step 4 : Using switch case select do while loop (or) for loop
Step 5 : Within for loop, calculate sum+=count,until count<n
i) Display the sum value
Step 6 : Within do while loop, calculate sum value until count<n
i) Display the sum value
Step 7 : In default, display the invalid option message.

Step 8 : Stop execution.


FLOW CHART :

START

Read n

Switch op

Case1 default
Case2

Print invaid
Count=1
for(count=1;cou
nt<=n;++count)
do
sum+=count
sum+=count;
++count

while(coun
Print sum
t<=n)

Print sum

STOP
SOURCE CODE :

#include <stdio.h>
#include <conio.h>
void main()
{
int n, count, sum=0,op;
clrscr();
printf("Enter an integer: ");
scanf("%d",&n);
printf("\n Press 1-do while loop, 2-for loop: ");
scanf("%d",&op);
switch(op)
{
case 1:
count=1;
do
{
sum+=count;
++count;
}
while(count<=n); printf("\n\nSum =
%d",sum); break;
case 2:
for(count=1;count<=n;++count)
{
sum+=count;
}
printf("\n\nSum = %d",sum);
break;
default:
printf("\n\n Invalid Option");
}
getch();
}

OUTPUT:

Enter an integer:
Press 1-do while loop,2-for loop:2
Sum=21

RESULT:

Thus the program to find the sum of „n‟ numbers using for, do – while statements hasbeen executed
and verified successfully.
Ex:No:4(a) Write a simple C program to perform matri x multiplication.
Date:

AIM :

To write a C program to perform matrix multiplication.

ALGORITHM:

Step 1 : Start
Step 2 : Declare the variables i, j, m, n, k, a[5][5], b[5][5], c[5][5]Step 3 :
Get the order of matrix
Step 4 : Get the values of the matrix a[i][j] using for loopStep 5 :
Get the values of the matrix b[i][j] using for loop
Step 6 : Multiply using for loop and calculate the resultant matrixStep 7 :
Display the resultant matrix c[i][j] using for loop
Step 8: Stop
FLOW CHART :

START

Read n

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

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

Read a[i][j]

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

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

Read b[i][j]

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

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

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

c[i][j] += a[i][k] *b[k][j];

Print c[i][j]

STOP
SOURCE CODE :
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, m, n, k, a[5][5], b[5][5], c[5][5] = {0};
clrscr();
printf("\t\t\t **** MATRIX MULTIPLICATION **** \n");
printf("\nEnter order of matrix (m*n): ");
scanf("%d%d",&m,&n);
printf("\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("\nEnter elements of Matrix A [%d][%d]: ",i+1,j+1);
scanf("\n\t%d", &a[i][j]);
}
}
printf("\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("\nEnter elements of Matrix B [%d][%d]: ",i+1,j+1);
scanf("\t%d", &b[i][j]);
}
}
printf("\n The resultant Matrix :\n ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
for (k = 0; k < n; k++)
c[i][j] += a[i][k] *b[k][j];
printf("\t%d", c[i][j]);
}
printf("\n");
}
getch();
}
Output:
****MATRIX MULTIPLICATION****

Enter order of matrix<m*n>:2*2

Enter elements of matrix A[1][1]:2


Enter elements of matrix A[1][2]:2
Enter elements of matrix A[2][1]:2
Enter elements of matrix A[2][2]:2

Enter elements of matrix B[1][1]:3


Enter elements of matrix B[1][2]:3
Enter elements of matrix B[2][1]:3
Enter elements of matrix B[2][2]:3

The resultant Matrix:


12 12
12 12

RESULT:

Thus the program to perform matrix multiplication has been executed and verified
successfully.
Ex:No:4(b) Write a simple C program to find the
Date: smallest and largest element in an array.

AIM :

To write a C program to find the smallest and largest element in an array.

ALGORITHM:

Step 1 : Start
Step 2 : Declare the variable a[50],i,n,large,small

Step 3 : Get the n values and store it in array


Step 4 : Using if check a[i]>large then assign large=a[i]
Step 5 : Using if check a[i]<small then assign small=a[i]
Step 6 : Print the smallest and largest element
Step 7 : Stop
FLOW CHART :
START

Read n

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

Get a[i]

large=small=a[0];

N
for(i=1;i<n;i++)
Y
Y
if(a[i]>large) large=a[i];
N
Y
if(a[i]<small) small=a[i]
N

Print large, small

STOP
SOURCE CODE :

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,large,small;
clrscr();
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;i++)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);

getch();
}
OUTPUT:

How many elements:6


Enter the array:78
3412
65
90
81
The largest element is 90
The smallest element is 12

RESULT:

Thus the program to find the smallest and largest element in an array has been executedand verified
successfully.
Ex:No:5 Write a simple C program to perform various string handling
Date: functions: strlen, strcpy, strcat, strcmp.

AIM :

To write a C program to perform various string handling functions: strlen, strcpy,strcat, strcmp.

ALGORITHM:

Step 1 : Start
Step 2 : Declare the variable dest[15], string2[15], len,cmp

Step 3 : Calculate the length of string using strlen


Step 4 : Copy the string using strcpy
Step 5 : Combine two string using strcat
Step 6 : Compare two string using strcmp and return singed integer values
Step 7 : Stop
FLOW CHART :
START

len=strlen(string)

Print len

strcpy(dest,string)

Print dest

strcat(string,string2);

Print string

strcat(string,string2);

cmp=strcmp(string,string2);

Y Print
if(cmp>0
string>string2
N
Y Print
if(cmp<0
string<string2
N
Print
string=string2

STOP
SOURCE CODE :

#include <stdio.h>
#include <string.h>
void main()
{
char string[]="computer", dest[15], string2[15];
int len,cmp;
clrscr();
len=strlen(string);
printf("\n Length of %s is %d\t",string,len);

strcpy(dest,string);
printf("\n\n%s is copied to dest string",dest);

printf("\n\n Enter any string: ");


gets(string2);

strcat(string,string2);
printf("\nconcatenated string is %s\n\n",string);

cmp=strcmp(string,string2);
if(cmp>0)
printf("%s > %s",string,string2);
else
{
if(cmp<0)
printf("%s < %s",string,string2);
else
printf("%s = %s",string,string2);
}
getch();
}
Output:
Length of computer is 8
Computer is copied to dest string
Enter any string: information
Concatenated string is computer information
Computer information < information

RESULT:

Thus the program to perform various string handling functions: strlen, strcpy, strcat,strcmp has
been executed and verified successfully.
Ex No:6(a) A simple C program to find the factorial of a
Date: given number using functions

AIM :

To write a C program to find the factorial of a given number using Functions.

ALGORITHM:

Step 1 : Start
Step 2 : Declere the function
Step 3 : Declare the variable i, fact, num
Step 4 : Get the value of num
Step 5 : Call the function factorial(num)
Step 6 : In the called function declare the variable i, f=1
Step 7 : Using for loop find the factorial and return to main function
Step 8 : Stop

FLOW CHART :

START

Read n

CALL FUNCTION
factorial(num)

factorial(int n)

F
for(i=1; i<=n; i++)
T
f=f*i;

Return f
SOURCE CODE :

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

int factorial(int);
void main()
{
int i,fact,num;
clrscr();
printf("Enter a number: ");
scanf("%d",&num);
fact = factorial(num);
printf("\n\nFactorial of %d is: %d",num,fact);
getch();
}

int factorial(int n)
{
int i,f=1;
for(i=1; i<=n; i++)
f=f*i;
return f;
}

OUTPUT:
Enter a number:6
Factorial of 6 is:720

RESULT:

Thus the program to find the factorial of a given number using functions has been executed and
verified successfully.
Ex:No:6(b) Write a simple C program to swap two numbers using call by value and
DATE: call by reference.

AIM :

To write a C program to swap two numbers using call by value and call by reference.

ALGORITHM:

Step 1 : Start
Step 2 : Declare the variable a=40, b=70
Step 3 : Declare the function swap1 and swap2

Step 4 : Print the values of a and b before swapping

Step 5 : Call swap1 and swap2


Step 6 : Change the values of a and b in function definition swap1 using
temp variable
Step 7: Change the values of a and b with implementing pointer concept in functiondefinition swap2
using temp variable
Step 8: Display the value of a and b after swapping.
Step 9: Stop execution
FLOW CHART :

START void swap1(int x, int y)

Declare a=40, b=70


Declare t

Declare function swap1 and swap2 t=x; x=y; y=t;

PRINT a, b values
before swapping Return swap1

CALL swap1

PRINT a, b values void swap2(int *x, int *y)


after swapping

Declare t
CALL swap2
t=*x; *x=*y; *y=t;
PRINT a, b values
after swapping
Return swap2

STOP
SOURCE CODE :

#include<stdio.h>
#include<conio.h>
void main()
{
int a=40;
int b=70;
void swap1(int, int);// fn declaration for call by value void s
wap2(int *x,int *y);// fn declaration for call by reference
clrscr();
printf("\n\t\t Before swapping result is: \n a=%d \t %d\n\n",a,b);
swap1(a,b);
printf("\n\n In call by value, After swapping result is:\n\n a=%d \t b=%d \n",a,b);
swap2(&a,&b);
printf("\n In call by reference, After swapping result is:\n\n a=%d \t b=%d ",a,b);
getch();
}

void swap1(int x, int y)// call by value


{
int t;
t=x;
x=y;
y=t;
}

void swap2(int *x,int *y)// call by reference


{
int t;
t=*x;
*x=*y;
*y=t;
}
Output:

Before swapping result is:


a=40 b=70

In call value, After swapping result is:


a=70 b=40
In call by reference, After swapping result is:
a=70 b=40

RESULT:

Thus the program to swap two numbers using call by value and call by reference hasbeen executed
and verified successfully.
Ex No:7 Factorial of a given number by using Recursive function
Date:

Aim:
To write a C program to find a factorial of a given number by recursive function.

ALGORITHM:

step 1. Start
step 2. Read the number n
step 3. [Initialize]
i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
Flowchart:
Source code:

#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,fact=1;
printf("Enter any number : ");
scanf("%d", &n);
for(i=1; i<=n; i++)
fact = fact * i;
printf("Factorial value of %d = %d",n,fact);
return 0;
}

OUTPUT:
Enter any number :5
Factorial value of 5 =120

Result :
Thus the program has executed and verified successfully.
EX NO:8(a) Write a simple C program to find the sum of an
DATE: integer array using pointers.

AIM :
To write a C program to find the sum of an integer array using pointers.

ALGORITHM:

Step 1 : Start
Step 2 : Declare the variable a[10],i,n,sum=0, *p

Step 3 : Get the n value n store it an array


Step 4 : Within for loop

i) display content and adress of variable


ii) find sum = sum + *(p+i)

Step 5 : Print the sum value


Step 6 : Stop
FLOW CHART :

START

Read n

N
for(i=0;i<n;i++)
Y
Get a[i]

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

Print *(p+i), p+i

sum = sum + *(p+i);

Print sum

STOP
SOURCE CODE :

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,sum=0, *p;
clrscr();
printf("Enter the number of elements: \n");
scanf("%d", &n);
printf("Enter Elements of First List\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
p=a;
for(i=0;i<n;i++)
{
printf(" a[%d] %d %u \n", i, *(p+i), p+i);sum = sum
+ *(p+i);
}
printf("Sum of all elements in array using pointer= %d\n", sum);getch();
}
OUTPUT:
Enter the number of elements:
4
Enter Elements of First List
34
567
12
8
a[0] 34 65502
a[1] 567 65504
a[2] 12 65506
a[3] 8 65508
sum of all elements in array = 621

RESULT:

Thus the program to find the sum of an integer array using pointers has been executedand verified
successfully.
Ex No:8(b) Write a simple C program to find the Maximum element in an integer
Date: array using pointers.

AIM :

To write a C program to find the Maximum element in an integer array using pointers.

ALGORITHM:

Step 1 : Start
Step 2 : Declare the variable i,n,arr[20],*p

Step 3 : Get the n val ue and store it an array

Step 4 : Within in for loop check (*p<*(p+i)

Step 5 : Assign arr[0]= *(p+i)


Step 6 : Print the value arr[0]
Step 7 : Stop
FLOW CHART :
START

Read n

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

Get arr[i]

P=arr;

F
for(i=0;i<n;i++)
T
F
if(*p<*(p+i))

T
arr[0]= *(p+i);

Print arr[0]

STOP
SOURCE CODE :

#include <stdio.h>
#include<conio.h>
void main()
{
int i,n;
float arr[20],*p;
clrscr();
printf("Enter total number of elements(1 to 100): ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;++i)
{
printf("Enter Number %d: ",i+1);
scanf("%f",&arr[i]);
}
p=arr;
for(i=0;i<n;++i) /* Loop to store largest number to arr[0] */
{
if(*p<*(p+i)) /* Change < to > if you want to find smallest element*/arr[0]=
*(p+i);
}
printf("\n\nLargest element = %.2f",arr[0]);
getch();
}
OUTPUT:
Enter total number of elements<1 to 100>:5

Enter number 1:34


Enter number 2:78
Enter number 3:123
Enter number 4: 6
Enter number 5: 789

Largest element = 789.00

RESULT:

Thus the program to find the Maximum element in an integer array using pointershas been
executed and verified successfully.
Ex No:9 Write a simple C program to create student details using
Date: Structures.

AIM :

To write a C program to create student details using Structures.

ALGORITHM:

Step 1 : Start
Step 2 : Declare rno, name[10], cgpa in structure name student

Step 3 : Declare n, i, dummy variable


Step 4 : Get the n value
Step 5 : Using for loop get roll no, name, cgpa and store it an array

Step 6 : Using for loop print the roll no, name, cgpa
Step 7 : Stop
FLOW CHART :

START

Read n

F
for(i=1;i<=n;i++)
T
Get roll no,name,cpa

s[i].CGPA=dummy

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

Print roll no,


name,cgpa

STOP
SOURCE CODE :
#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int rno;
char name[10];
float CGPA;
}s[10];
int n,i;
float dummy;
clrscr();
printf("\n\n\t\t**********STUDENT NAME LIST*********\n\n");
printf("\n Enter the Number of students:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the roll number of student %d : ",i);
scanf("%d",&s[i].rno);
printf("\n Enter the name of student %d : ",i);
scanf("%s",s[i].name);
printf("\n Enter the CGPA of student %d : ",i);
scanf("%f",&dummy);
s[i].CGPA=dummy;
}
printf("\n\n The Student details are:\n");
printf("------------------------------------------ \n\n");
printf("ROLLNO \t\t Student Name \t\t CGPA\n");
printf("*********\t\t*************\t\t**********\n\n");
for(i=1;i<=n;i++)
{
printf("\n %d \t %s \t %.2f",s[i].rno,s[i].name,s[i].CGPA);
}
getch();
}
Output:
**********STUDENT NAME LIST**********
Enter the number of students: 2
Enter the roll number of student 1: 12756
Enter the name of student 1: abhi
Enter the SGPA of student 1: 9.5
Enter the roll number of student 2:12345
Enter the name of student 2: umesh
Enter the SGPA of student 2: 8.9
The student details are:
------------------------------------
ROLLNO Student Name SGPA
*********** ****************** ************

12756 abhi 9.50

12345 umesh 8.90

RESULT:

Thus the program to create student details using structures has been executed and
verified successfully
Ex No:10(a) Write a simple C program to display the contents of the file on the monitor
screen
Date:

AIM :
To write a C program to display the contents of the file on the monitor screen.

ALGORITHM:

Step 1 : Start
Step 2 : Open the file textfile .txt in writing mode
Step 3 : Check the condition if fp is null then print error

Step 4 : Read the character


Step 5 : Using while read the character up to EOF and print in fp
Step 6 : Close the file
Step 7 : Open the file textfile .txt in read mode
Step 8 : Using while read the character up to EOF and print the character

Step 9 : Close the file


Step 10 : Stop
FLOW CHART :

START

fp=fopen("textfile.txt","w")

if(fp==NULL) Print Error

Read characters

while((ch=getchar
())!=EOF)

Print ch in fp

fclose(fp)

fp=fopen("textfile.txt","r")

while((ch=getc
(fp))!=EOF)

Print ch

fclose(fp)

STOP
SOURCE CODE :

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
printf("Open file for input\n");
fp=fopen("textfile.txt","w");
if(fp==NULL)
printf("\nError in file opening\n");else
{
printf("Enter characters [press ctrl+z to exit]: \n");
while((ch=getchar())!=EOF)
putc(ch,fp);
}
fclose(fp); fp=fopen("textfile.txt","r");
printf("\n The content read from file and display on the screen:\n\n");
while((ch=getc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
getch();
}
OUTPUT:
Open file for input
Enter characters[press ctrl+z to exit]:
Welcome to c
One of the efficient concept files
^Z

The content read from filr and display on the screen:


Welcome to c
One of the efficient concept files

RESULT:

Thus the program to display the contents of the file on the monitor screen has beenexecuted and
verified successfully.
Ex No:10(b) Create a File by getting the input from the keyboard and retrieve the
Date: contents of the file using file operation commands.

AIM :
To write a C program to Create a File by getting the input from the keyboard andretrieve the
contents of the file using file operation commands.

ALGORITHM:

Step 1 : Start
Step 2 : Open the file itemdet.dat in writing mode
Step 3 : Using if condition check (fp==NULL) then error
Step 4 : Read itemnumber itemname Price Quantity from the keyboard using for loop

Step 5 : Write itemnumber itemname Price Quantity to the file


Step 6 : Close the file
Step 7 : Open the file itemdet.dat in reading mode
Step 8 : Print itemnumber itemname Price Quantity netprice from the file

Step 9 : Close the file


Step 10 : Stop
FLOW CHART :

START

fp=fopen("itemdet.dat","w")

F
if(fp==NULL) Print error

T
for(i=1;i<=2;i++) T

Write no, item, price,


qty to the file

fclose(fp)

fp=fopen("itemdet.dat","r")
F
for(i=1;i<=2;i++) T

Print no, item,


price, qty, net

fclose(fp)

STOP
SOURCE CODE :
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp; char
item[30];int
i,no;
float price,qty,net;
clrscr();
fp=fopen("itemdet.dat","w");if(fp==NULL)
{
printf("Cannot open file.\n");
exit(1);
}
printf("Enter itemnumber itemname Price Quantity: \n\n");for(i=1;i<=2;i++)
{
fscanf(stdin,"%d%s%f%f",&no,item,&price,&qty); /* read from keyboard */
fprintf(fp,"%6d %6s %8.2f %8.2f",no,item,price,qty); /* write to file */
}
fclose(fp);

fp=fopen("itemdet.dat","r");
printf("Itemnumber Itemname Price Quantity netprice: \n\n");

for(i=1;i<=2;i++)
{
fscanf(fp,"%d%s%f%f",&no,item,&price,&qty);
net=price*qty;
fprintf(stdout,"%6d %6s %8.2f %8.2f %8.2f\n\n",no,item,price,qty,net);
}

fclose(fp);
getch();
Output:
Enter itemnumber itemname price quantity:

101
Pen
40
6
102
Jam
36
5
Itemnumber itemname price quantity netprice:
101 pen 40.00 6.00 240.00

102 jam 36.00 5.00 180.00

RESULT:
Thus the program to Create a File by getting the input from the keyboard and retrievethe contents of
the file using file operation commands has been executed successfully.
Ex No:10(c) Write a simple C program to pass the parameter using command line
Date: arguments.

AIM :
To write a C program to pass the parameter using command line arguments.

ALGORITHM:

Step 1 : Start
Step 2 : Declare argument count and argument vector

Step 3 : Declare i,sum=0


Step 4 : Using if condition check (argc<3) then type numbers
Step 5 : Using for loop sum the argument vector values
Step 6 : Print the sum value
Step 7 : Stop

FLOW CHART :
START

Declare i,sum=0

if(argc<3) Read num

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

sum = sum + atoi(argv[i])

Print sum

STOP
SOURCE CODE :

#include<stdio.h>
#include<stdlib.h>
void main(int argc , char *argv[])
{
int i,sum=0;
if(argc<3)
{
printf("you have forgot to type numbers.");
exit(1);
}
printf("The sum is : ");
for(i=1;i<argc;i++)
sum = sum + atoi(argv[i]);
printf("%d",sum);
}
Output:
C:\TC\BIN>cmdadd.exe 56 23 94 47
The sum is: 220

C:\TC\BIN>

RESULT:

Thus the given program to pass the parameter using command line arguments has been
executed successfully.

You might also like