C Lab Record 2023 NEW
C Lab Record 2023 NEW
2022-2023
C Program Using Simple Statement and Expressions
Aim:
To write a C program to calculate Area and Circumference of Circle.
Algorithm:
Step 1: Start
Step 2: Declare variables radius, area, circum in float type
Step 3: Read radius of circle as radius
Step 4: Calculate area using the expression: area = 3.14 * radius * radius
Step 5: Calculate circumference using the expression: circum=2*3.14*radius
Step 6: Print area and circum
Step 7: Stop
Program:
#include<stdio.h>
int main()
{
float radius, area,circum;
printf("\nEnter the radius of Circle : ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
circum=2*3.14*radius;
printf("\nArea of Circle : %.2f", area);
printf("\nCircumference of Circle : %.2f", circum);
return (0);
}
Output:
Enter the radius of Circle : 5
Area of Circle : 78.50
Circumference of Circle : 31.40
Result:
Thus, the C program to calculate Area and Circumference of Circle was written
executed and the output was verified successfully.
Output:
Enter Principle amount, Rate of interest & time:
20000
6
2
Simple interest = 2400
Result:
Thus, the C program to calculate Simple Interest was written executed and the
output was verified successfully.
Aim:
To write a C program to find the largest of three numbers using if...else if.
Algorithm:
Step 1. Read the values of x, y and z.
Step 2. If x is greater than y and x is greater than z then print x is greatest, otherwise
go to step 3.
Step 3. If y is greater than z then print y is greatest, otherwise go to step 4.
Step 4. display z is greatest.
Program:
#include<stdio.h>
int main()
{
int x,y,z;
printf("Enter the values for x,y and z \n");
scanf("%d%d%d",&x,&y,&z);
if((x>y)&& (x>z))
printf(" %d is greatest",x);
else if (y>z)
printf ("%d is greatest",y);
else
printf("%d is greatest",z);
return 0;
}
Output:
Run1:
Enter the values for x, y and z
25
46
22
46 is greatest
Run2:
Enter the values for x, y and z
75
46
22
75 is greatest
Aim:
To write a C program to check whether the given year is leap year or not.
Algorithm:
Step 1. Get the input year from the user to check for leap year.
Step 2. If the year is evenly divisible by 4, go to step 3. Otherwise, go to step 6.
Step 3. If the year is evenly divisible by 100, go to step 4. Otherwise, go to step 5.
Step 4. If the year is evenly divisible by 400, go to step 5. Otherwise, go to step 6.
Step 5. The year is a leap year (it has 366 days).
Step 6. The year is not a leap year (it has 365 days).
Program:
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}
Output:
Run1:
Enter a year: 1900
1900 is not a leap year.
Run2:
Enter a year: 2004
2004 is a leap year.
Aim:
To write a C program to check whether a given number is Armstrong number or
not.
Algorithm:
Step 1: Initialize the value of res to 0.
Step 2: Read the three digit number in num variable to check for Armstrong number.
Step 3: Assign originalNum to the variable num.
Step 4: Extract the digits from the num.
Step 5: Find the cube of each digit in num and add them and store it in variable res.
Step 6: Repeat the step 5 untill the num is not equal to zero..
Step 7: Compare the res and originalNum, if it is equal display the number is an
Armstrong number, otherwise display the number is not an Armstrong number.
Program:
#include <stdio.h>
int main()
{
int num, originalNum, rem, res = 0;
printf("Enter an integer: ");
scanf("%d", &originalNum);
num = originalNum;
while (num != 0)
{
rem = num%10;
res+= rem*rem*rem;
num /= 10;
}
if(res == originalNum)
printf("%d is an Armstrong number.",originalNum);
else
printf("%d is not an Armstrong number.",originalNum);
return 0;
}
Output:
Run1:
Enter an integer:
153
153 is an Armstrong number.
Run2:
Enter an integer:
103
103 is not an Armstrong number.
Result:
Thus, the C program to scientific Problem-Solving using Decision Making and Looping
was written executed and the output was verified successfully.
Aim:
To write a C program to generate following pattern:
1
123
12345
1234567
123456789
1234567
12345
123
1
Algorithm:
Step 1: Start
Step 2: Read Number of Rows to print
Step 3: Use outer loop for maintaining Number of rows
Step 4: Use Inner Loop appropriately for required Coolum output
Step 5: Print required numbers
Step 6: Stop
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int n=5, x, y, k;
for(x = 1; x <= n; x++)
{
for(y = x; y <n; y++)
{
printf(" ");
}
for(k = 1; k < (x*2); k++)
{
printf("%d",k);
}
printf("\n");
}
for(x = 4; x >= 1; x--)
{
for(y = n; y > x; y--)
Output:
1
123
12345
1234567
123456789
1234567
12345
123
1
Output:
Run 1:
Enter the number of columns7
*******
******
*****
****
***
**
*
**
***
St.Joseph’s Institute of Technology Page 8 of 39
CS4208-Programming in C Laboratory 2023-2024
2022-2023
****
*****
******
*******
Run 2:
Enter the number of columns5
*****
****
***
**
*
**
***
****
*****
Aim:
To write a C program to generate following pattern:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
Algorithm:
Step 1: Start
Step 2: Read Number of lines to print
Step 3: Use outer loop for maintaining Number of rows
Step 4: Use Inner Loop appropriately for required Coolum output
Step 5: Print required letter
Step 6: Stop
Program:
#include<stdio.h>
int main()
{
int i,n,j;
printf("Enter the no of lines\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
Output:
Run 1:
Enter the no of lines
5
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
Run 2:
Enter the no of lines
8
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
Result: Thus, the C program to generating Different Patterns using Multiple Control
Statements was written executed and the output was verified successfully.
Aim:
To write a C program to find the sum of array elements.
Algorithm:
Step 1: Start
Step 2:Read Number of elements in the array as n
Step 3: Initialize a variable s=0 for summation purpose
Step 4: Read n elements and store it in a array called a, using for loop
Step 5: Add elements with s while reading.
Step 6: Print value of s
Step 7: Stop
Program:
#include<stdio.h>
int main()
{
int i,n, a[10],s;
printf("Enter the number of element:\n");
scanf("%d",&n);
s=0;
printf("Enter element:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
s=s+a[i];
}
printf("Sum of array element:%d",s);
return 0;
}
Output:
Enter the number of element:
5
Enter element:
a[0]=10
a[1]=20
a[2]=30
a[3]=4
a[4]=7
Sum of array element:71
Aim:
To write a C program to insert an element in an Array.
Algorithm:
Step 1: Start
Step 2:Read Number of elements in the array as n
Step 3: Read n elements and store it in a array called a, using for loop
Step 4:Read element to be insert and position in num and pos
Step 5: From the end of the array, Using for loop start moving the elements one index
ahead till the required index reach.
Step 6: increase number of elements n by one
Step 7: Insert the element in the required index.
Step 8: Print all the elements.
Step 9: Stop
Program:
#include<stdio.h>
int main()
{
int i,n,pos,num, a[10];
printf("Enter the number of element:\n");
scanf("%d",&n);
printf("Enter element:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
printf("\nEnter the pos where the no. is to be inserted:");
scanf("%d",&pos);
printf("\nEnter the the no. is to be inserted:");
scanf("%d",&num);
for(i=n-1;i>=pos;i--)
a[i+1]=a[i];
n=n+1;
a[pos]=num;
printf("\n Display array after insertion:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
return 0;
}
Result:
Thus, the C program to Mathematical Problem Solving using one Dimensional Array
was written executed and the output was verified successfully.
Aim:
To write a C program to add two Matrices.
Algorithm:
Step 1: Start
Step 2: Declare variables m,n,p,q for storing number of rows and columns of two
matrices.
Step 3: Read Number of rows and columns of first matrix as m,n
Step 4: Read Number of rows and columns of second matrix as p,q
Step 5: Check whether m==p and n==q if so go to step 6 else print matrix addition not
possible and stop the program.
Step 6: Using two nested for loop read elements of first matrix
Step 7: Using two nested for loop read elements of second matrix
Step 8: Using two nested for loop add first matrix element and second matrix element
and store it in third matrix.
Step 9: Using two nested for loop print third matrix.
Step 10: Stop.
Program:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int i,j,m,n,p,q;
int a[10][10], b[10][10], c[10][10];
printf("\nEnter no of rows and column of matrix A:");
scanf("%d%d",&m,&n);
printf("\nEnter no of rows and column of matrix B:");
scanf("%d%d",&p,&q);
if(m!=p && n!=q)
{
printf("\n Matrix cannot be added.");
exit(0);
}
printf("\n Matrix can be added");
printf("\n Enter elements of matrix A:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter elements of matrix B:");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
Output:
Run1:
Enter no of rows and column of matrix A:2
2
Enter no of rows and column of matrix B:2
2
Matrix can be added
Enter elements of matrix A:1
2
3
4
Enter elements of matrix B:5
6
7
8
Display matrix A:
1 2
3 4
St.Joseph’s Institute of Technology Page 15 of 39
CS4208-Programming in C Laboratory 2023-2024
2022-2023
Display matrix B:
5 6
7 8
Added matrix:
6 8
10 12
Run2:
Enter no of rows and column of matrix A:2
3
Enter no of rows and column of matrix B:3
2
Matrix cannot be added.
Aim:
To write a C program to multiply two Matrices.
Algorithm:
Step 1: Start
Step 2: Declare variables m,n,p,q for storing number of rows and columns of two
matrix.
Step 3: Read Number of rows and columns of first matrix as m,n
Step 4: Read Number of rows and columns of second matrix as p,q
Step 5: Check whether n==p if so go to step 6 else print matrix multiplication not
possible and stop the program.
Step 6: Using two nested for loop read elements of first matrix
Step 7: Using two nested for loop read elements of second matrix
Step 8: Using three nested for loop multiply first matrix element and second matrix
element and store it in third matrix.
Step 9: Using two nested for loop print third matrix.
Step 10: Stop.
Program:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int i,j,m,n,p,q,k;
int a[10][10], b[10][10], c[10][10];
printf("\nEnter no of rows and column of matrix A:");
scanf("%d%d",&m,&n);
printf("\nEnter no of rows and column of matrix B:");
scanf("%d%d",&p,&q);
if(n!=p)
Display matrix B:
7 8
9 10
11 12
Display Product:
58 64
139 154
Run2:
Enter no of rows and column of matrix A:2
3
Enter no of rows and column of matrix B:2
3
Matrix cannot be multiplied
Result:
Thus, the C program to Mathematical Problem Solving using two Dimensional Array
was written executed and the output was verified successfully.
Aim:
To write a C program to find the length of a string without string function.
Algorithm:
Step 1: Start
Step 2: Read a String and store it in s
Step 3: Initialize i=0 in for loop
Step 4: Use a for loop to iterate over all character of the string and check for ‘\0’
character, also increment i value
Step 5: When ‘\0’ found stop for loop and print i value as length of string
Step 6: Stop
Program:
#include <stdio.h>
int main()
{
char s[100];
int i;
printf("Enter a string: ");
scanf("%[^\n]", s);
for(i = 0; s[i] != '\0'; ++i);
printf("Length of string: %d", i);
return 0;
}
Output:
Enter a string: C Programming
Length of string: 13
Aim:
Aim:
To write a C program to copy a string without string function.
Algorithm:
Step 1: Start
Step 2: Declare two character array named str, copystr
Step 3: Read a String and store it in str
Step 4: Using a for loop copy all the characters of str to copystr
Step 5: Add ‘\0’ at the end of Copystr
Step 6: Print copystr
Step 7: Stop
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char Str[100], CopyStr[100];
int i;
printf("\n Please Enter any String : ");
gets(Str);
for (i = 0; Str[i]!='\0'; i++)
{
CopyStr[i] = Str[i];
}
CopyStr[i] = '\0';
printf("\n String that we coped into CopyStr = %s", CopyStr);
return 0;
}
Output:
Please Enter any String: Strings in C
String that we coped into CopyStr = Strings in C
Aim:
To write a C program to copy a string with string function.
Algorithm:
Step 1: Start
Step 2: Declare two character array named str, copystr
Step 3: Read a String and store it in str
Step 4: Using strcpy() function copy content of str to copystr
Step 5: Print copystr
Step 6: Stop.
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char Str[100], CopyStr[100];
int i;
printf("\n Please Enter any String : ");
gets(Str);
strcpy(CopyStr,Str);
printf("\n String that we coped into CopyStr = %s", CopyStr);
return 0;
}
Output:
Please Enter any String : Strings in C
String that we coped into CopyStr = Strings in C
Aim:
To write a C program to compare two strings without string function.
Algorithm:
Step 1: Start
Step 2: Define a user defined function called compare it will take two input strings and
return 0 if both strings are same, return 1 if both strings are different.
Step 3: Inside compare function using a while loop check both the strings index by
index, if all index elements are same then return 0 or else return 1 Step 4: In Main
function Declare two character array named str1, str2
Step 5: Read two Strings and store it in str1, str2
Step 6: Call user defined compare function by passing str1,str2 as input
Program:
#include <stdio.h>
int compare(char[],char[]);
int main()
{
char str1[20];
char str2[20];
printf("Enter the first string : ");
gets(str1);
printf("Enter the second string : ");
gets(str2);
int c= compare(str1,str2);
if(c==0)
printf("strings are same");
else
printf("strings are not same");
return 0;
}
Aim:
To write a C program to compare the two strings with string function.
Algorithm:
Step 1: Start
Step 2: Declare two character array named str1, str2
Step 3: Read two Strings and store it in str1, str2
Step 4: Call strcmp() function by passing str1,str2 as input
Step 5: If the function return 0 then print both strings are same else print both strings
are different.
Step 6: Stop.
Program:
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20];
char str2[20];
int value;
printf("Enter the first string : ");
gets(str1);
printf("Enter the second string : ");
gets(str2);
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("strings are not same");
return 0;
}
Output:
Run 1:
Enter the first string : C Programming
Enter the second string : C Language
strings are not same
Run2:
Enter the first string : C Programming
Enter the second string : C Programming
strings are same
Result:
Thus, the C program to compare two strings with string function was written executed
and the output was verified successfully.
Aim:
To write a C program to swap two numbers using call by Value Method.
Algorithm:
Step 1: Start
Step 2: Define a swap function such that it will accept two integer values Step 3: Inside
swap function interchange the values of both integers
Step 4: In main function Read two integers
Step 5: Call swap function using the integers read from user
Step 6: Print the value of both integers
Step 7: Stop
Program:
#include <stdio.h>
void swap(int , int);
int main()
{
int a,b;
printf("Enter Two Values:\n");
scanf("%d%d",&a,&b);
printf("Before swapping the values in main: a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values in main: a = %d, b = %d\n",a,b);
return 0;
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function: a = %d, b = %d\n",a,b);
}
Output:
Enter Two Values:
10
20
Before swapping the values in main: a = 10, b = 20
After swapping values in function: a = 20, b = 10
After swapping values in main: a = 10, b = 20
Result:
Thus, the C program to Swap Two Numbers using call by Value Method was written
executed and the output was verified successfully.
Aim:
To write a C program to Swap Two Numbers using call by Reference Method.
Algorithm:
Step 1: Start
Step 2: Define a swap function such that it will accept two integer pointers
Step 3: Inside swap function interchange the values of both integers using pointer
Step 4: In main function Read two integers
Step 5: Call swap function using the address of integers read from user
Step 6: Print the value of both integers
Step 7: Stop
Program:
#include <stdio.h>
void swap(int *, int *);
int main()
{
int a,b;
printf("Enter Two Values:\n");
scanf("%d%d",&a,&b);
printf("Before swapping the values in main: a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main: a = %d, b = %d\n",a,b);
return 0;
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function: a = %d, b = %d\n",*a,*b);
}
Output:
Enter Two Values:
10
20
Before swapping the values in main: a = 10, b = 20
After swapping values in function: a = 20, b = 10
After swapping values in main: a = 20, b = 10
Result:
Thus, the C program to Swap Two Numbers using call by Reference Method was
written executed and the output was verified successfully.
Aim:
To write a C program to sort an array of elements using functions.
Algorithm:
Step 1: Start
Step 2: Define a function named mySort(). It will take an integer array and number of
values in a array as input.
Step 3: Inside mySort() function Using two nested for loops check elements and swap
elements to sort array.
Step 4: Print the sorted array
Step 5: In main function declare an array
Step 6: Read total number of elements in the array as n
Step 7: Using for loop read n elements one by one
Step 8: call mySort() function to sort array
Step 9: Stop
Program:
#include<stdio.h>
void mySort(int[],int);
int main ()
{
int arr[10],n,i;
printf("Enter Number of elements in the array\n");
scanf("%d",&n);
printf("Enter Elements one by one\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
mySort(arr,n);
return 0;
}
void mySort(int a[],int n)
{
int i, j,temp;
for(i = 0; i<n; i++)
{
for(j = i+1; j<n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
Output:
Enter Number of elements in the array
5
Enter Elements one by one
6
2
8
3
1
Printing Sorted Element List :
1
2
3
6
8
Result:
Thus, the C program to Sort an Array of Elements using Functions was written
executed and the output was verified successfully.
Aim:
To write a C program to find the factorial of a number using recursive functions.
Algorithm:
Step 1: Start
Step 2: Read N value from user
Step 3: Call recursive function factorial(), by passing N and store the return value to fact
Step 4: In factorial() function, if N==0 then return 1 else return N*factorial(N-1)
Step 5: Print fact value
Step 6: Stop
Program:
#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
int main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}
Output:
Enter a number: 6
Factorial of 6 is 720
Result:
Thus, the C program to find Factorial of a Number using Recursive Functions was
written executed and the output was verified successfully.
Aim:
To write a C program to generate Fibonacci series using recursive functions.
Algorithm:
Step 1: Start
Step 2: Read Number of terms from user as n
Step 3: Using a for loop call recursive function fibonacci(), n times and each time print
the return value
Step 4: In fibonacci() function if n==0 or n==1 then return n else return fibonacci(n-1) +
fibonacci(n-2)
Step 5: Stop
Program:
#include<stdio.h>
int fibonacci(int);
int main()
{
int n, m= 0, i;
printf("Enter Total terms:\n");
scanf("%d", &n);
printf("Fibonacci series terms are:\n");
for(i = 1; i <= n; i++)
{
printf("%d\n", fibonacci(m));
m++;
}
return 0;
}
int fibonacci(int n)
{
if(n == 0 || n == 1)
return n;
else
return(fibonacci(n-1) + fibonacci(n-2));
}
Output:
Enter Total terms:
10
Fibonacci series terms are:
0
1
1
2
Result:
Thus, the C program to generate Fibonacci Series using Recursive Functions was
written executed and the output was verified successfully
Aim:
To write a C program to solve Tower of Hanoi using Recursive Functions.
Algorithm:
Step 1: Start
Step 2: Read Number of disks from user as num
Step 3: Call recursive function towers(),by passing number of disks num and name of
the source, destination, auxiliary towers
Step 4: In towers() function if n==1 then print move disk 1 from source to destination
and go to step 8 else go to step 5
Step 5: Call recursive function towers() with arguments num-1, source, auxiliary,
destination
Step 6: Print Move num from source to destination
Step 7: Call recursive function towers() with arguments num-1, auxiliary, destination,
source
Step 8: Stop
Program:
#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
Output:
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi :
Result:
Thus, the C program to solve Tower of Hanoi using Recursive Functions was written
executed and the output was verified successfully.
Aim:
To write a C program to print a text using Dynamic Memory.
Algorithm:
Step 1: Start
Step 2: Declare a character pointer named text
Step 3: Read number of character in the string from user as n
Step 4: Allocate n bytes in memory using malloc() function and store the starting
address in the pointer variable text
Step 5: Read and store the string in text
Step 6: print entered string and length of the string
Step 7: Delete the allocated memory using free() function
Step 8: Stop
Program:
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
int main()
{
int n;
char *text;
printf("Enter limit of the text: ");
scanf("%d",&n);
text=(char*)malloc(n*sizeof(char));
printf("Enter text: ");
scanf(" ");
gets(text);
printf("Inputted text is: %s\n",text);
printf("\n%ld",strlen(text));
free(text);
return 0;
}
Output:
Enter limit of the text: 25
Enter text: This is C Programming Lab
Inputted text is: This is C Programming Lab
25
Result:
Thus, the C program to Print Text using Dynamic Memory was written executed and
the output was verified successfully.
Aim:
To write a C program to process One Dimensional array using Dynamic Memory
method.
Algorithm:
Step 1: Start
Step 2: Declare a integer pointer named arr
Step 3: Read total number of elements in the array from user as limit
Step 4: Allocate n*size(int) bytes in memory using malloc() function and store the
starting address in the pointer variable arr
Step 5: Check whether the memory is allocated or not, if not allocated print error
message and stop, if allocated then proceed to next step
Step 6: Using a for loop read elements and store in allocated memory using pointer
notation, also calculate sum of the elements
Step 7: Using for loop and pointer notation print the elements
Step 8: Print sum
Step 9: Stop
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr;
int limit,i;
int sum=0;
printf("Enter total number of elements: ");
scanf("%d",&limit);
arr=(int*)malloc(limit*sizeof(int));
printf("Enter %d elements:\n",limit);
Result:
Thus, the C program to process One Dimensional array using Dynamic Memory
method was written executed and the output was verified successfully.
Aim:
To write a C program to create employee pay slip using Structure.
Algorithm:
Step 1: Start
Step 2: Define a Employee structure with required fields using struct keyword Step 3:
Declare a employee structure pointer
Step 4: Read total number of employees as n
Step 5: Allocate memory of n*sizeof(emplyeestructure) using malloc() function Step 6: If
memory not allocated stop else proceed to next step
Step 7: Using a for loop read n number of employees details and store them in
respective structure variable using -> operator
Step 8: Calculate net pay of each employee by adding basic pay and allowance and
reducing deduction
Step 9: Using a ‘for’ loop iterate each employee structure and print all data present in it.
Step 10: Stop
Program:
#include <stdio.h>
#include <stdlib.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
};
int main()
{
struct emp *pemp;
int n,i;
printf("Enter total number of Employees: ");
scanf("%d",&n);
pemp=(struct emp*)malloc(n*sizeof(struct emp));
if(pemp==NULL)
{
printf("Insufficient Memory, Exiting... \n");
return 0;
}
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
Output:
Enter total number of Employees: 3
Enter the employee number : 101
Enter the name : Raja
Result:
Thus, the C program to Create Employee Pay slip using Structure was written executed
and the output was verified successfully.
Aim:
To write a C program to create student details using Structure and Union.
Algorithm:
Step 1: Start
Step 2: Define a structure student with a nested union
Step 3: Ask whether need to read name or roll number
Step 4: Based on that either read name or roll number and store it in nested union
Step 5: Read mark and store it in structure variable
Step 6: Print the details
Stop 7: Stop
Program:
#include<stdio.h>
struct student
{
union
{
char name[10];
int roll;
};
int mark;
};
int main()
{
struct student stud;
char choice;
int n;
st: printf("\n You can enter your name or roll number ");
printf("\n Do you want to enter the name (y or n): ");
scanf("%c",&choice);
if(choice=='y'||choice=='Y')
{
printf("\n Enter name: ");
scanf("%s",stud.name);
printf("\n Display Name:%s",stud.name);
}
else
{
printf("\n Enter roll number :");
scanf("%d",&stud.roll);
printf("\n Display Roll number:%d",stud.roll);
}
printf("\n Enter marks:");
scanf("%d",&stud.mark);
Output:
Display Name:deby
Enter marks:89
Display Marks:89
Do you want to continue (press 1): 1
Display Marks:69
Do you want to continue (press 1): 2
Result:
Thus, the C program to Create Student Details using Structure and Union was written
executed and the output was verified successfully.