0% found this document useful (0 votes)
48 views49 pages

24CSE401-FOCC LAB - Manual

The document contains a series of C programming exercises that demonstrate various programming concepts such as input/output operations, conditional branching, menu-driven calculators, and matrix operations. Each exercise includes an aim, algorithm, program code, output, and result, confirming successful execution. The exercises cover topics like Armstrong numbers, array manipulation, and function usage.

Uploaded by

B Anandakumar
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)
48 views49 pages

24CSE401-FOCC LAB - Manual

The document contains a series of C programming exercises that demonstrate various programming concepts such as input/output operations, conditional branching, menu-driven calculators, and matrix operations. Each exercise includes an aim, algorithm, program code, output, and result, confirming successful execution. The exercises cover topics like Armstrong numbers, array manipulation, and function usage.

Uploaded by

B Anandakumar
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/ 49

Ex. No.

1
I/O STATEMENTS AND EXPRESSIONS

AIM:

To write C programs to demonstrate the uses of various formatted and unformatted


input and output functions, expressions and functional blocks.

ALGORITHM:

STEP 1: Start the program.


STEP 2: Declare all required variables and initialize them.
STEP 3: Get input values from the user using input function scanf()
STEP 4: Use output function printf( ) to display the output after the calculation.
STEP 5: Stop the program.

1
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{

int age,bir_year,cur_year; clrscr();


printf("Enter the year of birth:");
scanf("%d",&bir_year);
printf("\nEnter the current year:");
scanf("%d",&cur_year);
age=cur_year-bir_year;
printf("\nThe age is:%d years",age);
getch();
}

1
OUTPUT:

Enter the year of birth: 2006


Enter the current year: 2024
The age is: 18 years

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:

To write a C program to check whether the given number is positive number or


negative number

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare all required variables.

STEP 3: Get an input from the user and store it in a variable.

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”.

STEP 6: Stop the program.

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
***********************

Enter the number : 12


The given number 12 is positive

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:

To write a C program to design a menu-based calculator to perform various basic


arithmetic operations like addition, subtraction, multiplication, division and modulo.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare all required variables

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().

STEP 6: Stop the program.

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

Enter the choice:


1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Square

Enter a number to be squared: 4

The square of a number 4 is: 16

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:

To write a C program to check whether the given number is an Armstrong number


or Not.

ALGORITHM:

STEP 1: Start the program.


STEP 2: Declare and intialize the required variables.
STEP 3: Repeat the loop till find the number of digits in a given input by separating number
thru’ modulo division.
STEP 4: Repeat the loop till separating last digit of the given number and do adding nth power
of individual digits.
STEP 5: if input and sum of nth power of individual digits of input is equal then it is an
Amstrong number.
STEP 6: Otherwise it is not an Amstrong number.
STEP 7: Stop.

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:

Check whether an n digits number is armstrong or not :


-----------------------------------------------------------
Input an integer : 1634
1634 is an Armstrong number.

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 1: Start the program.

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 6: Repeat the step 5 till i>=n.

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:

Enter size of the array : 5

Enter elements in array :

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:

To write a C program to perform matrix addition and matrix subtraction.

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

Enter the 4 elements of the first matrix


23
43
54
87

Enter the 4 elements of the second matrix


54
32
98
76

The first matrix is:


23 43
54 87

The second matrix is:


54 32
98 76

The sum of the two entered matrices is:


77 75
152 163

The difference(subtraction) of the two entered matrices is:


-31 11
-44 11

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:

STEP 1: Start the Program


STEP 2: Declare all the required variables
STEP 3: Read the text using gets() and getchar().
STEP 4: Replace the word in the text by user specific word if match.
STEP 5: Reverse the string using while loop
STEP 6: Display the output using puts() and putchar() .
STEP 7: Stop the program

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:

Enter the string : hello


Enter a character replace : h
Enter character to replace with h : H
Before replace : hello
After replace : Hello
Original String : Hello
Reverse String : olleH

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:

STEP 1: Start the Program


STEP 2: Declare all the required variables and a function mul().
STEP 3: Pass the arguments to the function mul() in main.
STEP 4: Find out the product and store the result in p.
STEP 5: Return the output value p to the main function.
STEP 6: Display the output in main.
STEP 7: Stop the program

24
PROGRAM:

#include<stdio.h>

int mul(int,int);

int main()

int a,b,pro;

printf("\nInput two integer numbers : ");

scanf("%d%d",&a,&b);

pro=mul(a,b);

printf("\n\nMultiplication of two integer = %d",pro);

int mul(int x, int y)

int p;

p=x*y;

return(p);

25
OUTPUT:

Input two integer numbers:21 21

Multiplication of two integer=441

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

To create a structure for an account holder in a bank


ALGORITHM:

STEP 1: Start the program.

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

7: Stop the program.

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:

1. Add a New Account Holder


2. Display
3. Deposit or Withdraw
4. Number of Account Holder Whose Balance is less than the Minimum Balance
5. Stop
Enter your choice : 1
Enter the Account Number : 11111
Enter the Account Holder Name : kkkk
Enter the Initial Amount to deposit : 25555
Enter your choice : 2
A/c Number : 11111
A/c Holder Name Balance: kkkk
25555

RESULT:

Thus the implementation of structure for Banking has been implemented and
verified successfully.

32
Ex. No. 9
TO CREATE UNION PROGRAM

AIM

To create a union program


ALGORITHM:

STEP 1: Start the program.

STEP 2: Include necessary header files and declare all required variables.

STEP 3: Create the union with member name.

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.

STEP 6: Stop the program.

33
PROGRAM:

#include<stdio.h>

union Job

float salary;

int workerno;

}j;

void main()

j.salary=12.3;//when j.workerno is assigned a value

//j.salary will no longer hold 12.3

j.workerno=100;

printf(“Salary=%1f”,j.salary);

printf(“Workerno=%d”,j.workerno);

34
OUTPUT:

Salary=0.0
Workerno=100

RESULT:

Thus the implementation of union program is created and executed successfully.

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:

STEP 1: Start the program


STEP 2: Declare the variable
STEP 3: Assign the variable address to pointer variables.
STEP 4: print the address of the variable using pointer variable
STEP 5: Stop the program

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:

Thus the implementation of pointer is executed successfully.

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:

STEP 1: Start the program


STEP 2: Declare the variables
STEP 3: Declare the function for swapping called swap()
STEP 4: Call the function swap() in main and do the following
temp = b;
b = a;
a = temp;
STEP 5: Display the output of swapped variables

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:

To copy the contents of one file to another file.

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:

Enter the data to the file1.txt file

STUDENT BOX OFFICE.IN

After copying the data in file2.txt file is

STUDENT BOX OFFICE.IN

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:

To write a program using command line argument .

ALGORITHM:

STEP 1: Start the program

STEP 2: Use arguments in main()

STEP 3: Pass two arguments in main()

STEP 4: Run the program in command prompt with program name and message

STEP 5: Stop the program

46
PROGRAM

#include <stdio.h>

void main(int argc, char *argv[] )

printf("Program name is: %s\n", argv[0]);

if(argc < 2)

printf("No argument passed through command line.\n");

else

printf("First argument is: %s\n", argv[1]);

47
OUTPUT:

C:\TC\BIN\>program13.exe Hello

Program Name is:C:TC\BIN\PROGRAM13.EXE

First argument is:Hello

RESULT:

Thus the program using command line arguments has been implemented and verified

successfully.

48

You might also like