24CSE401-FOCC LAB - Manual
24CSE401-FOCC LAB - Manual
1
I/O STATEMENTS AND EXPRESSIONS
AIM:
ALGORITHM:
1
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
1
OUTPUT:
RESULT:
Thus the C program to perform input and output operations using built-in printf()
and scanf() function has been done and verified successfully.
2
Ex. No. 2
CONDITIONAL BRANCHING STATEMENTS
AIM:
ALGORITHM:
STEP 4: Check the input value whether it is a positive number or negative number.
STEP 5: If the number is less than ZERO, then print the result as “NEGATIVE”.
Otherwise display the result as “POSITIVE”.
3
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“*********************\n”);
printf(“POSITIVE OR NEGATIVE\n”);
printf(“**********************\n”);
printf("Enter the number\n");
scanf("%d",&n);
if(n>0)
printf("The given number %d is positive\n",n);
else if(n<0)
printf("The given number %d is negative",n);
else
printf("The given number %d is neither positive nor negative",n);
getch();
}
4
OUTPUT:
***********************
POSITIVE OR NEGATIVE
***********************
RESULT:
Thus the C program to check for positive and negative numbers using decision
making construct has been
5
Ex. No. 3
MENU DRIVEN CALCULATOR USING
SWITCH CASE STATEMENT
AIM:
ALGORITHM:
STEP 3: Get two inputs from the user using scanf() function and store them in “a” and “b”
respectively.
STEP 4: Get the user option and based on the user options, perform the corresponding
arithmetic operations on the user data.
STEP 5: Store the result in a variable called “c” and display the value of “c” using printf().
6
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int ch,a,b,c;
clrscr();
printf("**********************\n");
printf("Menu driven program\n");
printf("**********************\n");
printf("Menu\n");
printf(" 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\n 5) Square\n");
printf(“Enter your choice:”);
scanf("%d",& ch);
switch(ch)
{
case 1:
printf("Enter two numbers to be added:");
scanf("%d%d",&a,&b);
c=a+b;
printf("\nThe sum of two numbers %d and %d is:%d",a,b,c);
break;
case 2:
printf("Enter two numbers to be subtracted:");
scanf("%d%d",&a,&b);
c=a-b;
printf("\nThe difference of two numbers %d and %d is:%d",a,b,c);
break;
7
case 3:
printf("Enter two numbers to be multiplied:");
scanf("%d%d",&a,&b);
c=a*b;
printf("\nThe product of two numbers %d and %d is:%d",a,b,c);
break;
case 4:
printf("Enter two numbers to be divided:");
scanf("%d%d",&a,&b);
c=a/b;
printf("\nThe division of two numbers %d and %d is:%d",a,b,c);
break;
case 5:
printf("Enter a number to be Square:");
scanf("%d",&a);
c=a*a;
printf("\nThe square of a number %d is:%d",a,c); break;
default:
printf("Invalid option");
}
getch();
}
8
OUTPUT:
*******************
Menu driven program
*******************
Menu
RESULT:
Thus the menu-drive arithmetic calculator has been implemented in C and verified
successfully.
9
Ex. No. 4 FIND WHETHER GIVEN NUMBER IS AN ARMSTRONG
NUMBER OR NOT
AIM:
ALGORITHM:
10
PROGRAM:
#include <stdio.h>
#include <math.h>
int main()
{
int n1, onum, r, result = 0, n = 0;
printf("\n\n Check whether an n digits number is armstrong or not :\n");
printf("-----------------------------------------------------------\n");
printf(" Input an integer : ");
scanf("%d", &n1);
onum = n1;
while (onum != 0)
{
onum /= 10;
++n;
}
onum = n1;
while (onum != 0)
{
r = onum % 10;
result += pow(r, n);
onum /= 10;
}
if(result == n1)
printf(" %d is an Armstrong number.\n\n", n1);
else
printf(" %d is not an Armstrong number.\n\n", n1);
return 0;
}
11
OUTPUT:
RESULT:
Thus the program to check for an Armstrong number has been written in C and the
output was verified successfully.
12
Ex. No. 5A TO PRINT MINIMUM AND MAXIMUM ELEMENTS IN THE 1-D
ARRAY
AIM:
To write a C program to print minimum and maximum elements in the 1-D array.
ALGORITHM:
STEP 2: Declare necessary variables and get the size of the array from the user.
STEP 3: Read the entered array size and store that value into the variable n.
STEP 4: Initialize min, max values with the 1st element of the array.
STEP 5: Compare min, max values with a[i]. If min value is greater than a[i] then
initialise min=a[i] and if max value is less than a[i] then initialize max=a[i].
STEP 7: Print the minimum of the array and maximum of the array values.
13
PROGRAM:
#include <stdio.h>
int main()
{
int a[1000],i,n,min,max;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("Minimum of array is : %d",min);
printf("\nMaximum of array is : %d",max);
return 0;
}
14
OUTPUT:
76
85
55
Minimum of array is : 3
Maximum of array is : 85
RESULT:
Thus the program to print the minimum and maximum element in the 1-D array
has been written in C and the output was verified successfully.
15
Ex. No. 5B
TO PERFORM MATRIX ADDITION AND MATRIX SUBTRACTION
AIM:
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare all required variables and array to store values of matrix.
STEP 3: Get an input from the user and store it in arrays.
STEP 4: Using the user input, perform the addition and subtraction operation using for loop
STEP 5: Print the result of addition and subtraction in matrix format.
STEP 6: Stop the Program
16
PROGRAM:
#include<stdio.h>
int main()
{
int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the %d elements of the first matrix \n\n", m*n);
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
scanf("%d", &first[c][d]);
}
}
printf("\nEnter the %d elements of the second matrix \n\n", m*n);
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
scanf("%d", &second[c][d]);
}
}
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", first[c][d]);
}
printf("\n");
}
17
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", second[c][d]);
}
printf("\n");
}
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
}
}
printf("\n\nThe sum of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];
printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
18
printf("%d\t", diff[c][d]);
}
printf("\n");
}
return 0;
}
19
OUTPUT:
Enter the number of rows and columns of the first matrix
2
2
RESULT:
Thus the program to print the addition and subtraction of matrix has been written in
C and the output was verified successfully.
20
Ex. No. 6 TO READ AND DISPLAY A VALUE USING
GETCHAR(), PUTCHAR(), GETS() AND PUTS()
AIM:
To write a C Program to read and display a value using getchar(), putchar(), gets() and
puts().
ALGORITHM:
21
PROGRAM:
#include<string.h>
#include<stdio.h>
int main()
{
char s[1000],c1,c2,ch;
int i=0;
printf("Enter the string : ");
gets(s);
puts("Enter a character replace : ");
c1=getchar();
printf("\nEnter character to replace with %c : ",c1);
c2=getchar();
printf("\n Before replace:%s",s);
for(i=0;s[i];i++)
{
if(s[i]==c1)
{
s[i]=c2;
}
}
printf("\nAfter replace : %s",s);
while((ch=getchar())!='\n')
{
s[i]=ch;
i++;
}
printf("\nOriginal String : %s\n",s);
printf("\nReverse String : ");
printf("%s",strrev(s));
return 0;
}
22
OUTPUT:
RESULT:
Thus the program to read and display a value using getchar(), putchar(), gets() and
puts() has been written in C and the output was verified successfully.
23
Ex. No. 7 TO FIND PRODUCT OF TWO NUMBERS USING FUNCTIONS
WITH ARGUMENTS, WITH RETURN TYPE
AIM:
To write a C Program to find product of two numbers using functions with arguments,
with return type.
ALGORITHM:
24
PROGRAM:
#include<stdio.h>
int mul(int,int);
int main()
int a,b,pro;
scanf("%d%d",&a,&b);
pro=mul(a,b);
int p;
p=x*y;
return(p);
25
OUTPUT:
RESULT:
Thus the program to find product of two numbers using functions with arguments, with
return type has been written in C and the output was verified successfully.
26
Ex. No. 8
TO CREATE STRUCTURE FOR AN ACCOUNT HOLDER IN A BANK
AIM
STEP 2: Include necessary header files and declare all required variables.
STEP 3: Display the list of options to the user as a menu and get the user input.
STEP 4: Based the user input, validate the values given by the user and do the calculation.
STEP 5: Display the status and result of the requested operation. STEP
6: Repeat the steps from 3 to 5 until user exits from the program. STEP
27
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MINBAL 500
struct Bank_Account
{
char no[10]; char
name[20]; char
balance[15];
};struct Bank_Account acc;
void main()
{
long int pos1,pos2,pos;FILE *fp;
char *ano,*amt;char
choice;
int type,flag=0;float bal;
do
{
clrscr(); fflush(stdin);
printf("1. Add a New Account Holder\n");
printf("2. Display\n");
printf("3. Deposit or Withdraw\n");
printf("4. Number of Account Holder Whose Balance is lessthan the Minimum
Balance\n");
printf("5. Stop\n");
printf("Enter your choice : ");
choice=getchar()
switch(choice)
{
case 1 :
fflush(stdin);
fp=fopen("acc.dat","a");
28
printf("\nEnter the Account Number : ");
gets(acc.no);
printf("\nEnter the Account Holder Name : ");
gets(acc.name);
printf("\nEnter the Initial Amount to deposit : ");
gets(acc.balance);
fseek(fp,0,2);
fwrite(&acc,sizeof(acc),1,fp);
fclose(fp);
break;
case 2 :
fp=fopen("acc.dat","r");
if(fp==NULL)
printf("\nFile is Empty");
else
{
printf("\nA/c Number\tA/c Holder NameBalance\n");
while(fread(&acc,sizeof(acc),1,fp)==1)
printf("%-10s\t\t% 20s\t%s\n",acc.no,acc.name,acc.balance);
fclose(fp);
}
break;case
3:
fflush(stdin);
flag=0;
fp=fopen("acc.dat","r+");
printf("\nEnter the Account Number :");
gets(ano);
for(pos1=ftell(fp);
fread(&acc,sizeof(acc),1,fp)==1;pos1=ftel l(fp))
{
if(strcmp(acc.no,ano)==0)
{
printf("\nEnter the Type 1 for deposit & 2 forwithdraw : ");
scanf("%d",&type);
29
printf("\nYour Current Balance is: %s",acc.balance);
printf("\nEnter the Amount to transact : ");fflush(stdin);
gets(amt);
if(type==1)
bal = atof(acc.balance) + atof(amt);
else
{
bal = atof(acc.balance) - atof(amt);if(bal<0)
printf("\nRs.%s Not available in your A/c\n",amt);
flag=2;
break;
}
}
flag++;
break;
}
}
if(flag==1)
{
pos2=ftell(fp); pos = pos2-
pos1;
fseek(fp,-pos,1);
sprintf(amt,"%.2f",bal);
strcpy(acc.balance,amt);
fwrite(&acc,sizeof(acc),1,fp);
}
else if(flag==0)
printf("\nA/c Number Not exits... Check it again");fclose(fp);
break;
case 4 :
fp=fopen("acc.dat","r");flag=0;
while(fread(&acc,sizeof(acc),1,fp)==1)
{
bal = atof(acc.balance);
if(bal<MINBAL)
30
flag++;
}
printf("\nThe Number of Account Holder whose Balance less than the
Minimum Balance :
%d",flag);
fclose(fp);
break;
case 5 :
fclose(fp);
exit(0);
}
printf("\nPress any key to continue....");
getch();
} while (choice!='5');
}
31
OUTPUT:
RESULT:
Thus the implementation of structure for Banking has been implemented and
verified successfully.
32
Ex. No. 9
TO CREATE UNION PROGRAM
AIM
STEP 2: Include necessary header files and declare all required variables.
STEP 4: Based the user input, validate the values given by the user and do the calculation.
STEP 5: Call the union with object then print the member vales.
33
PROGRAM:
#include<stdio.h>
union Job
float salary;
int workerno;
}j;
void main()
j.workerno=100;
printf(“Salary=%1f”,j.salary);
printf(“Workerno=%d”,j.workerno);
34
OUTPUT:
Salary=0.0
Workerno=100
RESULT:
35
Ex. No. 10
TO PRINT THE ADDRESS OF VARIABLE USING POINTER
AIM:
To write a C Program to print the address of variable using pointer
ALGORITHM:
36
PROGRAM:
#include<stdio.h>
void main()
int myage=37;
int *p=&myage;
printf(“\n Age=%d”,myage);
printf(“\n Address=%p”,p);
37
OUTPUT:
Age=37
Address=0x7ffe536e044
RESULT:
38
Ex. No.11 TO SWAP TWO NUMBERS USING A) CALL BY
VALUE B) CALL BY REFERENCE
AIM:
To write a C Program to swap two numbers using a) call by value b) call by reference
ALGORITHM:
39
(a) PROGRAM (CALL BY VALUE)
#include <stdio.h>
void swap(int, int);
int main()
{
int x, y;
printf("\n\nEnter the value of x and y : ");
scanf("%d%d",&x,&y);
printf("\n\nBefore Swapping : \nx = %d\ny = %d\n", x, y);
swap(x, y);
printf("\n\nAfter Swapping : \nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf("Values of a and b is %d %d\n",a,b);
40
(b) PROGRAM (CALL BY REFERENCE)
#include <stdio.h>
void swap(int*, int*);
int main()
{
int x, y;
printf("\n\nEnter the value of x and y : ");
scanf("%d%d",&x,&y);
printf("\n\nBefore Swapping : \nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("\n\nAfter Swapping : \nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
41
OUTPUT:
Enter the value of x and y : 99 45
Before Swapping :
x = 99
y = 45
After Swapping :
x = 45
y = 99
RESULT:
Thus the program to swap two numbers using a) call by value b) call by reference has
been written in C and the output was verified successfully.
42
Ex. No. 12 COPIES THE CONTENTS OF ONE FILE TO ANOTHER FILE
AIM:
ALGORITHM:
STEP 1: Start the program
STEP 2: Open source file in read mode
STEP 3: If NULL pointer, then print source file can not be open
STEP 4: Open destination file in write mode
STEP 5: If NULL pointer, then print destination file can not be open
STEP 6 : Read a character from source file and write to destination file until EOF
STEP 7: Close source file and destination file
STEP 8: Stop the program
43
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *f1, *f2;
char c;
clrscr();
printf(“Enter the data to the file1.txt file \n”);
f1 = fopen(“file1.txt”, “w”);
while((c = getchar()) != EOF)
putc(c, f1);
fclose(f1);
f2 = fopen(“file2.txt”, “w”);
f1 = fopen(“file1.txt”, “r”);
while((c = getc(f1)) != EOF)
putc(c,f2);
fclose(f1);
fclose(f2);
printf(“After copying the data in file2.txt file is \n”);
f2 = fopen(“file2.txt”, “r”);
while((c = getc(f2)) != EOF)
printf(“%c”, c);
fclose(f2);
getch();
}
44
OUTPUT:
RESULT:
Thus the program which copies the contents of one file to another file using
command line arguments has been implemented and verified successfully.
45
Ex. No. 13 PROGRAM USING COMMAND LINE ARGUMENT
AIM:
ALGORITHM:
STEP 4: Run the program in command prompt with program name and message
46
PROGRAM
#include <stdio.h>
if(argc < 2)
else
47
OUTPUT:
C:\TC\BIN\>program13.exe Hello
RESULT:
Thus the program using command line arguments has been implemented and verified
successfully.
48