All Over Final PC Lab Manual (EDITED)
All Over Final PC Lab Manual (EDITED)
List of Exercises
Ex: No:1. a
DATE: AREA OF A TRIANGLE
AIM:
To write a C program to find the area of a triangle.
FORMULA:
Area = (1/2) * base * height
ALGORITHM:
Step 1: start
Step 2: Declare the variable base, height,
area Step 3: Get the value of base, height
Step 4: Find area = (1/2) * base *
height Step 6: Print the value of
area
Step 7: Stop
FLOW CHART:
START
Read base,
height
PRINT area
STOP
SOURCE CODE:
#include
<stdio.h>
#include<conio.h
> void main() {
float base, height,
area; clrscr();
// Input the length of the base and height from the user
printf("Enter the length of the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle:
"); scanf("%f", &height);
// Calculate the area
area = 0.5 * base * height;
// Display the result
printf("The area of the triangle is: %f\n", area);
getch();
}
SCREEN SHOT:
RESULT:
Thus, the given program to calculate area of a triangle has been executed and verified successfully.
Ex. No:2. a READ A THREE-DIGIT NUMBER AND DISPLAY THE OUTPUT LIKE,
DATE: HUNDRED, TENS, UNITS FOR AN INPUT OF 172
AIM:
To write a C program to read a three-digit number and produce output like hundreds, tens, units.
ALGORITHM:
Step 1: Start
Step 2: Declare the variables n, r, a [5],
i=1 Step 3: Read the n value
Step 4: using while loop check
n>0 Step 5: Do a[i]=n%10,
n=n/10 Step 6: increment i value
Step 7: Repeat the step 4, 5 and 6 until the condition false
Step 8: Print the values in hundreds, tens and units with the Array’s index
specified. Step 9: Stop
FLOW CHART: START
Read n
while(n>0)
N
a[i]=n%10; n=n/10; i++;
STOP
SOURCE CODE :
#include<stdio.h>
#include<conio.h
> void main()
{
int
n,r,a[5],i=1;
clrscr();
printf("Enter any three digit number: ");
scanf("%d",&n);
while(n>0)
{
a[i]=n
%10;
n=n/10; i+
+;
}
printf("\n\n %d Hundreds \n %d Tens \n %d
Units",a[3],a[2],a[1]); getch();
}
SCREEN SHOT:
RESULT:
Thus, the given C program to read a three-digit number and produce output like hundreds, tens, units
has been executed and verified successfully.
AIM:
To write a C program to check whether a given character is vowel or not using Switch – Case statement.
ALGORITHM:
Step 1: Start
Step 2: Declare the character variable
ch Step 3: Read the character
Step 4: i) Using switch case check the character which is suitable for the given value
ii) if no cases matched, then print the default
statement. Step 5: Stop
FLOW CHART:
START
Read ch
Switch ch
case a: case A case e: case E case i : case I case o: case O case u: case U
PRINT vowel
STOP
SOURCE CODE:
#include<stdio.h>
#include<conio.h
> void main()
{
char
ch;
clrscr();
printf("enter any character: \n");
ch=getchar();
switch(ch)
{
case'a': case'A':
case'e': case'E':
case'i': case'I':
case'o': case'O':
case'u': case'U': printf("\n\
n It is a Vowel"); break;
default:
printf("\n\n It is not a Vowel");
}
getch();
}
SCREEN SHOT:
RESULT:
Thus, the given C program to check whether a given character is vowel or not using Switch –
Case statement has been executed and verified successfully.
Ex No:4. a
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
PRINT i*iT
STOP
SOURCE CODE :
#include
<stdio.h>
#include<conio.h
> void main()
{
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();
}
SCREEN SHOT:
RESULT:
Thus, the given C program to print the numbers from 1 to 10 along with their squareshas
been executed and verified successfully.
Ex.No:5. a
DATE: SUM OF ‘N’ NUMBERS USING FOR, DO-WHILE STATEMENTS
AIM:
To write a C program to find the sum of ‘n’ numbers using for, do – while statements.
ALGORITHM:
Step 1 : Start
Step 2 : Declare the variable n, count, sum=0,op
Step 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
ii) Display the sum value
Step 7 : In default, display the invalid option
message. Step 8 : Stop execution.
FLOW CHART :
START
Read n
Switch op
Case default
1 Case
2
Print invaid
Count=1
for(count=1;count<=
n;++count)
sum+=count
Print sum
while(co
sum+=count
; unt<=n)
++count
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);
case 2: break;
for(count=1;count<=n;++count)
{
sum+=count;
}
printf("\n\nSum = %d",sum);
break;
default:
printf("\n\n Invalid Option");
}getch();
}
SCREEN SHOT:
RESULT:
Thus, the program to find the sum of ‘n’ numbers using for, do – while statementshas been executed
and verified successfully.
Ex. No:6. a
DATE: FACTORIAL OF 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 : Declare 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)
Print fact
STOP
factorial(int n)
T F
for(i=1; i<=n; i++)
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;}
SCREEN SHOT:
RESULT:
Thus, the program to find the factorial of a given number using functions has been executed and
verified successfully.
EX.NO.7. a
DATE: CHECK WHETHER A GIVEN STRING IS PALINDROME OR NOT
Aim:
Write a C program to check whether a given string is palindrome or not?
Algorithm:
Step 1: Start
Step 2 : Read the input string from the user.
Step 3: Initialize two pointers, one at the beginning and one at the end of the
string. Step 4 : Repeat until the first pointer is less than the second pointer:
Step 5 : If the characters at both pointers are not equal, the string is not a
palindrome. Step 6 : Move the first pointer to the right.
Step 7: Move the second pointer to the left.
Step 8 : If the loop completes without finding unequal characters, the string is a
palindrome. Step 9 : Display the result.
Step 10 :End
Flowchart:
Source Code:
#include <stdio.h>
int main() {
int n, reversed = 0, remainder,
original; printf("Enter an integer: ");
scanf("%d", &n);
original = n;
// reversed integer is stored in reversed
variable while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 +
remainder; n /= 10;}
// palindrome if orignal and reversed are equal
if (original == reversed)
printf("%d is a palindrome.",
original); else
printf("%d is not a palindrome.",
original); return 0;}
SCREEN SHOT:
RESULT:
Thus, the program to check the palindrome of a given number has been executed and verified
successfully.
EX.NO:8. a
AIM:
To write a C program that checks whether a given value is a prime number or not.
ALGORITHM:
Step 1 :Start
Step 2 :Read the input value from the user.
Step 3 :If the input value is less than 2, it is not a prime number.
Step 4 :Initialize a variable is Prime to 1 (assuming the number is
prime). Step 5 :Iterate from 2 to the square root of the input value:
Step 6 :If the input value is divisible evenly by any number in this range, set is Prime to 0 and break the
loop.
Step 7 :If is Prime is still 1, the number is prime; otherwise, it is
not. Step 8 :Display the result.
Step 9 :End
FLOWCHART:
SOURCE CODE
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number
n:"); scanf("%d", &n);
//logic
for (i = 1; i <= n; i++)
{ if (n % i == 0) {
c++;
}
}
if (c == 2) {
printf("%d is a Prime number",n);
}
else {
printf("%d is not a Prime number",n);
}
return 0;
}
SCREEN SHOT:
Result:
Thus, the C program that checks whether a given value is a prime number or not has executed
and verified successfully.
Ex. No:9. a SWAP TWO NUMBERS USING CALL BY VALUE AND CALL BY
DATE: 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 function definition swap2
using temp variable.
FLOW CHART :
void swap1(int x, int y)
Declare t
Declare t
Return swap1
Return swap2
t=*x;*x=*y;*y=t;
START
PRINT a, b
values before swapping
CALL swap1
PRINT a, b
values
after swapping
CALL swap2
PRINT a, b
values
after swapping
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 swap2(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();
}
SCREEN SHOT:
RESULT:
Thus, the program to swap two numbers using call by value and call by reference has been
executed and verified successfully.
Ex. No:10. a
DATE: SMALLEST AND LARGEST ELEMENT IN AN ARRAY
AIM :
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 5 : Print the smallest and largest
element
Step 6 : 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
if(a[i]>larg
Y large=a[i];
N Y
if(a[i]<smal small=a[i]
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();
}
SCREEN SHOT:
RESULT:
Thus, the program to find the smallest and largest element in an array has been executed and
verified successfully.
Ex. No:11. a
DATE: MATRIX MULTIPLICATION 2*2
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
loop Step 5 : Get the values of the matrix b[i][j] using
for loop
Step 6 : Multiply using for loop and calculate the resultant
matrix Step 7 : Display the resultant matrix c[i][j] using for loop
Step 8: Stop
FLOW CHART :
START
Read n
Read a[i][j]
Read b[i][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();
}
SCREEN SHOT:
RESULT:
Thus, the program to perform matrix multiplication has been executed and verified successfully.
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);
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();
}
SCREEN SHOT:
RESULT:
Thus, the program to perform various string handling functions: strlen, strcpy, strcat, strcmp has
been executed and verified successfully.
Ex: No:13. a
REMOVE ALL CHARACTERS IN A STRING EXCEPT ALPHABETS
DATE:
AIM :
To write a C program to remove all characters in a string except alphabets.
ALGORITHM:
Step 1 : Start
Step 2 : Declare the variable line[30], i, j
Step 3 : Read the string
Step 4 :Using for loop check line[i]!='\0'
Step 5 : Using while loop Check line[i] contains alphabets
Step 6 : Using for loop check line[j]!='\0'
Step 7 : Assign line[j]=line[j+1] and print line.
Step 8 : Stop
FLOW CHART :
START
Read
strin
g
for(i=0; line[i]!Y='\0';++i) F
while(!((line[i]>='a'&&lin
e[i]<='z')||(line[i]>='A'&&l
ine[i]<='Z'||line[i]=='\0)))
N
Y
F
for(j=i;line[j]!='\0';++
j)
Y
line[j]=line[j+1];
line[j]='\0';
Print line
STOP
SOURCE CODE :
#include<stdio.h>
#include<conio.h
>
void main()
{
char
line[30]; int
i,j;
printf("Enter a string:
"); gets(line);
for(i=0; line[i]!='\0';++i)
{
while(!((line[i]>='a'&&line[i]<='z')||(line[i]>='A'&&line[i]<='Z'|| line[i]=='\0')))
{
for(j=i;line[j]!='\0';++j)
{
line[j]=line[j+1];
}
line[j]='\0';
}
}
printf("\nOutput String: ");
puts(line);
getch();
}
SCREEN SHOT:
RESULT:
Thus, the program to remove all characters in a string except alphabets has been executed and
verified successfully.
Ex: No:14. a
DATE: SUM OF AN 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 address of variable
ii) find sum = sum + *(p+i)
Step 5 : Print the sum value
Step 6 : Stop
FLOW CHART :
START
Read n
for(i=0;i<nY;i++) N
Get a[i]
F
for(i=0;i<n;i++)
Print *(p+i),
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();
}
SCREEN SHOT:
RESULT:
Thus, the program to find the sum of an integer array using pointers has been executed and
verified successfully.
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 value 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;
T
for(i=0;i<n;i++)
F
if(*p<*(p+i))
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();
}
SCREEN SHOT:
RESULT:
Thus, the program to find the Maximum element in an integer array using
pointers has been executed and verified successfully.
Ex: No:16. a
CREATE EMPLOYEE DETAILS USING STRUCTURES
DATE:
AIM :
To write a C program to create employee details using Structures.
ALGORITHM:
Step 1 : Start
Step 2 : Declare empno, empname[10], salary in structure name employee
Step 3 : Declare n, i, dummy variable
Step 4 : Get the n value
Step 5 : Using for loop get empno, empname, salary and store it an
array Step 6 : Using for loop print the empno, empname, salary
Step 7 :Stop
FLOW CHART :
START
Read n
for(i=1;i<=n;i++) F
T
Get empno,empname,salary
s[i].salary=dummy
for(i=1;i<=n;i++)
STOP
SOURCE CODE :
#include<stdio.h>
#include<conio.h
> void main()
{
struct employee
{
int empno;
char
empname[100];
float salary;
}s[100];
int n,i;
float dummy;
SCREEN SHOT:
RESULT:
Thus, the program to create student details using structures has been executed and verified
successfully.
Ex: No:17. a:
DATE: DISPLAY THE CONTENTS OF THE FILE ON THE MONITOR SCREEN
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==NUL Print
Read characters
while((ch=getch
ar())!=EOF)
Print ch in
fclose(fp)
fp=fopen("textfile.txt","r")
while((ch=ge
tc(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();
}
SCREEN SHOT:
RESULT:
Thus, the program to display the contents of the file on the monitor screen has been executed and
verified successfully.
AIM :
To write a C program to Create a File by getting the input from the keyboard and retrieve 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 itemnumberitemname Price Quantity from the keyboard using for
loop Step 5 : Write itemnumberitemname Price Quantity to the file
Step 6 : Close the file
Step 7 : Open the file itemdet.dat in reading mode
Step 8 : Print itemnumberitemname Price Quantity netprice from the file
Step 9 : Close the file
Step 10 : Stop
FLOW CHART :
START
fp=fopen("itemdet.dat","w")
for(i=1;i<=2;i++)
T
T
Write no, item, price, qty to the file
fclose(fp)
fp=fopen("itemdet.dat","r")
for(i=1;i<=2;i++)
Print
no,item,price,
F
T
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 itemnumberitemname 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("ItemnumberItemname 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();
}
SCREEN SHOT:
RESULT:
Thus, the program to Create a File by getting the input from the keyboard and retrieve the contents of
the file using file operation commands has been executed successfully.
AIM:
The aim is to write a C program that creates two files with a set of values and then merges the contents of
these two files into a single file.
ALGORITHM:
Step 1 : Start
Step 2 : Create and open the first file for writing (file1.txt).
Step 3 : Write a set of values to the first file.
Step 4 : Close the first file.
Step 5 : Create and open the second file for writing (file2.txt).
Step 6 : Write another set of values to the second file.
Step 7: Close the second file.
Step 8 : Open the first file for reading.
Step 9 : Open the second file for reading.
Step 10 : Create and open a third file for writing (merged_file.txt).
Step 11 : Read contents from the first file and write to the third file.
Step 12 : Read contents from the second file and append to the third
file. Step 13 : Close all three files.
Step 14 : End
FLOWCHART:
Source code :
#include <stdio.h>
int main() {
FILE *file1, *file2,
*mergedFile; char ch;
// Create and write to file1.txt
file1 = fopen("file1.txt", "w");
fprintf(file1, "File1 Content\n");
fclose(file1);
// Create and write to file2.txt
file2 = fopen("file2.txt", "w");
fprintf(file2, "File2 Content\n");
fclose(file2);
// Open file1.txt for reading
file1 = fopen("file1.txt", "r");
// Open file2.txt for reading
file2 = fopen("file2.txt", "r");
// Create merged_file.txt for writing
mergedFile = fopen("merged_file.txt",
"w");
// Read from file1.txt and write to
merged_file.txt while ((ch = fgetc(file1)) !=
EOF) {
fputc(ch, mergedFile);
}
// Read from file2.txt and append to
merged_file.txt while ((ch = fgetc(file2)) != EOF)
{
fputc(ch, mergedFile);
}
// Close all files
fclose(file1);
fclose(file2);
fclose(mergedFile)
return 0;
}
SCREEN SHOT:
Result:
Thus, the program to a create two files with a set of values and then merges the contents of these two
files into a single file.
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
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);
}
SCREEN SHOT:
RESULT:
Thus, the given program to pass the parameter using command line arguments has been executed
successfully.
#include<stdio.h>
#include<conio.h>
#include<process.h>
main(int argc, char
*argv[])
{
FILE *fp;
if(argc<2
)
{
printf("insufficient");
exit(1);
}
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("not
found"); exit(1);
}
unlink (argv[1]);
printf("deleted")
; return 0;
}