Assignment on:
C Programming Language
Course Title: Programming Language and Application l Sessional.
Course Code: CSEC 122
Submitted To:
MOHAMMAD ALAUDDIN
Lecturer,
Department of CSE
Uttara University.
Submitted By:
Shamima Khanum
ID: 2202081010
Department of CSE
Uttara University.
1. Program to find the sum of two float numbers using function
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
clrscr();
printf(“Enter first number:”);
scanf(“%f”,&a);
printf(“Enter second number:”);
scanf(“%f”,&b);
c=a+b;
printf(“Sum: %.2f”,c);
getch();
Output:
Enter first number: 2.5
Enter second number: 5.8
Sum of two float numbers= 8.30
2. Programs that find whether a number is even or odd using function
#include<stdio.h>
int main()
{
int a;
printf("Enter any number\n");
scanf("%d",&a);
if((a%2)==0)
printf("Entered number is even");
else
printf("Entered number is odd");
return 0;
}
3. Programs that find the larger and smaller of given three numbers using function
#include <stdio.h>
int main()
{
float num1,num2,num3; printf("Enter tree
numbers\n"); scanf("%f %f
%f",&num1,&num2,&num3); if(num1<num2 &&
num1<num3) {
printf("\n%.2f is smallest",num1);
}
else if(num2<num3)
{
printf("\n%.2f is smallest",num2);
}
else
{
printf("\n%.2f is smallest",num3);
}
if(num1>num2 && num1>num3)
{
printf("\n%.2f is largest",num1);
}
else if(num2>num3)
{
printf("\n%.2f is largest",num2);
}
else{ printf("\n%.2f is largest",num3); }
return 0;
}
4. Program to find out the factorial of number using function
#include<stdio.h>
void factorial(int);
int main()
{
int num;
printf("Enter a positive number to find
Factorial\n"); scanf("%d", &num);
factorial(num);
return 0;
}
void factorial(int num)
{
int count, fact = 1;
if(num == 0)
{
printf("Factorial of 0 is 1 (!0 = 1)\n");
}
else
{
for(count = 1; count <= num; count++)
{
fact = fact * count;
}
printf("\nFactorial of %d is %d (!%d = %d)\n", num, fact, num, fact);
}
}
Output:
Enter a positive number to find Factorial
5
Factorial of 5 is 120 (!5 = 120)
5. Program that return the sum of squares of all odd numbers from 1 to 100.
#include<stdio.h>
int main()
{
int NUM,i,j,SUM=0;
printf("\nENTER INTERGER NUMBER : ");
scanf("%d",&NUM);
for(i=1;i<NUM+1;i++)
{
if(i%2!=0)
{
SUM=SUM+(i*i);
}
}
printf("\nTHE SUM OF SQUARE OF ODD NOS. TILL %d NO. IS :
%d",NUM,SUM);
return 0;
}
6. Program to find the sum of digits of any number
using function #include <stdio.h>
int main()
{
int Number, Reminder, Sum=0;
printf("\n Please Enter any number\n");
scanf("%d", &Number);
while(Number > 0)
{
Reminder = Number % 10;
Sum = Sum+ Reminder;
Number = Number / 10;
}
printf("\n Sum of the digits of Given Number = %d", Sum);
return 0;
}
7. Program to print all prime numbers less than 100 using function
#include<stdio.h>
#include<math.h>
int isprime (int n);
main()
{
int i;
for(i=1;i<=100;i++)
if(isprime (i))
printf("%d",i);
}
int isprime (int n)
{
8. Programs that find whether a number is prime or not
using function #include <stdio.h>
int main()
{
int a,b;
printf("Enter a number");
scanf("%d",&a);
for(b=2;b<=a-1;b++)
if(a%b==0)
break;
if(b==a)
printf("%d is a prime number",a);
else
printf("%d is not a prime number",a);
return 0;
}
9. Program to convert a decimal number to binary number
#include <stdio.h>
#include <math.h>
long decimalToBinary(int decimalnum)
{
long binarynum = 0;
int rem, temp = 1;
while (decimalnum!=0)
{
rem = decimalnum%2;
decimalnum = decimalnum / 2;
binarynum = binarynum + rem*temp;
temp = temp * 10;
}
return binarynum;
}
int main()
{
int decimalnum;
printf("Enter a Decimal Number: ");
scanf("%d", &decimalnum);
printf("Equivalent Binary Number is: %ld",
decimalToBinary(decimalnum)); return 0;
Output:
Enter a Decimal Number: 234
Equivalent Binary Number is: 11101010
10. Program to find out the factorial of number numbers using recursive function
#include<stdio.h>
int fact(int);
int main()
{
int num;
printf("Enter a positive number to find its
Factorial\n"); scanf("%d", &num);
printf("\nFactorial of %d is %d.\n", num, fact(num));
return 0;
}
int fact(int num)
{
if(num)
return(num * fact(num - 1));
else
return 1;
}
Output: Enter a positive number to find its
Factorial 7 Factorial of 7 is 5040.
11. Program to generate Fibonacci series using recursive method
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Input : 5
Output:
Fibonacci series
0
1
1
2
3
12. Program to convert a binary or octal number to a decimal number using function
#include <stdio.h>
#include <math.h>
int binaryToDecimal(long binarynum)
{
int decimalnum = 0, temp = 0, remainder;
while (binarynum!=0)
{
remainder = binarynum % 10;
binarynum = binarynum / 10;
decimalnum = decimalnum + remainder*pow(2,temp);
temp++;
}
return decimalnum;
}
int main()
{
long binarynum;
printf("Enter a binary number: ");
scanf("%ld", &binarynum);
printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum));
return 0;
}
Output:
Enter a Binary Number: 11101010
Equivalent Decimal Number is: 234
13. Program that tests whether a number is divisible by 11 or not using function
#include <stdio.h>
int main()
{
int a;
printf("Enter a number: ");
scanf("%d", &a);
if((a % 11 == 0))
{
printf("%d is divisible by 11",a);
}
else
{
printf("%d is not divisible by 11",a);
}
return 0;
}
14. Program to convert a decimal number to binary, octal or decimal number using function
#include <stdio.h>
#include <conio.h>
long decimalToOctal(long n);
int main() {
long decimal;
printf("Enter a decimal number\n");
scanf("%ld", &decimal);
printf("Octal number of %ld is %ld", decimal, decimalToOctal(decimal));
getch();
return 0;
}
/* Function to convert a decinal number to octal
number */ long decimalToOctal(long n) { int
remainder;
long octal = 0, i = 1;
while(n != 0) {
remainder = n%8;
n = n/8;
octal = octal + (remainder*i);
i = i*10;
}
return octal;
}
Output:
Enter a decimal number 1234
Octal number of 1234 is 2322
15. Program to find out the LCM and HCF of two numbers
recursively #include <stdio.h>
long gcd(long, long);
int main() {
long x, y, hcf, lcm;
printf("Enter two integers\n");
scanf("%ld%ld", &x, &y);
hcf = gcd(x, y);
lcm = (x*y)/hcf;
printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);
return 0;
}
long gcd(long x, long y) {
if (x == 0) {
return y;
}
while (y != 0) {
if (x > y)
x = x - y;
else
y = y - x;
}
return x;
}
16. Program to add the elements of an array
#include<stdio.h>
main()
{
int arr[5],i,sum=0;
for(i=0;i<5;i++)
{
printf("Enter the value for arr[%d] : ",i);
scanf("%d",&arr[i]);
sum+=arr[i];
}
printf("sum = %d\n",sum);
17. Program to count the even and odd numbers in an array
#include<stdio.h>
int main()
{
int Size, i, a[10];
int Even_Count = 0, Odd_Count = 0;
printf("\n Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements\n");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < Size; i ++)
{
if(a[i] % 2 == 0)
{
Even_Count++;
}
else
{
Odd_Count++;
}
}
printf("\n Total Number of Even Numbers in this Array = %d ", Even_Count);
printf("\n Total Number of Odd Numbers in this Array = %d ", Odd_Count);
return 0;
}
18. Program to find the maximum and minimum number
in an array #include <stdio.h>
int main()
{
int a[100],size, c, l, L;
printf("Enter the number of elements in
array\n"); scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &a[c]);
l = L = 0;
for (c = 1; c < size; c++) {
if (a[c] > a[L])
L = c;
else if (a[c] < a[l])
l = c;
}
printf("Minimum element is present at location %d and its value is %d.\n", l + 1, a[l]);
printf("Maximum element is present at location %d and its value is %d.\n", L + 1, a[L]);
return 0;
}
19. Program to reverse the elements of an array
#include<stdio.h>
int main()
{
int a[100],reverse[100],i,n;
printf("\nEnter no of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
reverse[i]=a[n-i-1];
}
printf("\nOn reversing the array elements we get\n");
for(i=0;i<n;i++)
{
printf("%d ",reverse[i]);
}
}
20. Program to convert a decimal number to binary number of an array
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10],n,i;
system("cls");
printf("Enter the number to convert: ");
scanf("%d",&n);
for(i=0;n>0;i++)
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given number is= ");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
21. Program to search for an item in the array
#include<stdio.h>
int main(){
int array1[30];
int i, j, n1, n2;
printf("\nEnter no of elements to array :");
scanf("%d", &n1);
printf("\n\n Enter the Array elements one by one \n");
for (i = 1; i<=n1; i++) {
scanf("%d", &array1[i]);
}
printf("\nEnter the element to be searched :");
scanf("%d", &n2);
for (i = 1; i<=n1; i++)
if(array1[i]==n2){
j=i;
}
printf("\n The Array elements are : \n");
for (i = 1; i<=n1; i++)
printf("\n %d", array1[i]);
printf("\n\n The Location of the searched element %d in the Array is %d", n2,
j);
return(0);
}
22. Program to pass array elements to a function
#include<stdio.h>
float findAverage(int marks[]);
int main()
{
float avg;
int marks[] = {99, 90, 96, 93, 95};
avg = findAverage(marks); // name of the array is passed as argument.
printf("Average marks = %.1f", avg);
return 0;
}
float findAverage(int marks[])
{
int i, sum = 0;
float avg;
for (i = 0; i <= 4; i++) {
sum += marks[i];
}
avg = (sum / 5);
return avg;
}
23. Program to input and display a
matrix #include <stdio.h>
int main()
{
int i, j, m, n;
int matrix[10][20];
printf("Enter number of rows : ");
scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
/* Input data in matrix */
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("Enter data in [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
/* Display the matrix */
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
24. Program for addition of two matrices
#include <stdio.h>
int main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,n;
printf("\n\nAddition of two Matrices :\n");
printf("------------------------------\n");
printf("Input the size of the square matrix (less
than 5): "); scanf("%d", &n);
printf("Input elements in the first matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Input elements in the second
matrix :\n"); for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",arr1[i][j]);
}
printf("\nThe Second matrix is :\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",brr1[i][j]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
crr1[i][j]=arr1[i][j]+brr1[i][j];
printf("\nThe Addition of two matrix is : \n");
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",crr1[i][j]);
}
printf("\n\n");
}
Output:
Addition of two Matrices :
------------------------------
Input the size of the square matrix (less than 5): 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
The First matrix is :
1 2
34
The Second matrix is :
5 6
78
The Addition of two
matrix is : 6 8
10 12
25. Program for multiplication of two
matrices #include <stdio.h>
int main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;
printf("\n\nMultiplication of two Matrices :\n");
printf("----------------------------------\n");
printf("\nInput the rows and columns of first
matrix : "); scanf("%d %d",&r1,&c1);
printf("\nInput the rows and columns of second matrix : ");
scanf("%d %d",&r2,&c2);
if(c1!=r2){
printf("Mutiplication of Matrix is not possible.");
printf("\nColumn of first matrix and row of second matrix must be same.");
}
else
{
printf("Input elements in the first matrix :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Input elements in the second
matrix :\n"); for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",arr1[i][j]);
}
printf("\nThe Second matrix is :\n");
for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
printf("%d\t",brr1[i][j]);
}
//multiplication of matrix
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
crr1[i][j]=0;
for(i=0;i<r1;i++) //row of first matrix
{
for(j=0;j<c2;j++) //column of second matrix
{
sum=0;
for(k=0;k<c1;k++)
sum=sum+arr1[i][k]*brr1[k][j];
crr1[i][j]=sum;
}
}
printf("\nThe multiplication of two matrices is : \n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d\t",crr1[i][j]);
}
}
}
printf("\n\n");
}
Output:
Multiplication of two Matrices :
----------------------------------
Input the rows and columns of first matrix : 2
2
Input the rows and columns of second matrix : 2
2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
The First matrix is :
1 2
34
The Second matrix is :
5 6
78
The multiplication of two matrices is :
19 22
43 50
26. Program to find the transpose of a matrix
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
// Assigning elements to the
matrix printf("\nEnter matrix
elements:\n"); for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1,
j + 1); scanf("%d", &a[i][j]);
}
// Displaying the matrix a[][]
printf("\nEntered matrix: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
// Finding the transpose of
matrix a for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
// Displaying the transpose of
matrix a printf("\nTranspose of the
matrix:\n"); for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j) {
printf("%d ",
transpose[i][j]); if (j == r - 1)
printf("\n");
}
return 0;
}
27. Program to convert a decimal number to binary, octal or decimal
number using switch case and array
#include<stdio.h> // include stdio.h library
void convert_to_x_base(int, int);
int main(void)
{
int num, choice, base;
while(1)
{
printf("Select conversion: \n\n");
printf("1. Decimal to binary. \n");
printf("2. Decimal to octal. \n");
printf("3. Exit. \n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
base = 2;
break;
case 2:
base = 8;
break;
case 3:
printf("Exiting ...");
exit(1);
default:
printf("Invalid choice.\n\n");
continue;
}
printf("Enter a number: ");
scanf("%d", &num);
printf("Result = ");
convert_to_x_base(num, base);
printf("\n\n");
}
return 0; // return 0 to operating system
}
void convert_to_x_base(int num, int base)
{
int rem;
// base condition
if (num == 0)
{
return;
}
else
{
rem = num % base; // get the rightmost digit
convert_to_x_base(num/base, base); //
recursive call if(base == 16 && rem >= 10) {
printf("%c", rem+55);
}
else
{
printf("%d", rem);
}
}
28. Program to dereference pointer variable
#include<stdio.h>
int main()
{
int a=90;
int *ptr1,*ptr2;
ptr1=&a;
ptr2=&a;
*ptr1=7;
*ptr2=6;
printf("The value of a is: %d",a);
return 0;
}
29. Program to understand dynamic memory allocation using malloc () function
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
int *element;
printf("Enter total number of elements: ");
scanf("%d", &n);
/*
returns a void pointer(which is type-casted to int*)
pointing to the first block of the allocated space
*/
element = (int*) calloc(n,sizeof(int));
/*
If it fails to allocate enough space as specified,
it returns a NULL pointer.
*/
if(element == NULL)
{
printf("Error.Not enough space available");
exit(0);
}
for(i = 0; i < n; i++)
{
/*
storing elements from the user
in the allocated space
*/
scanf("%d", element+i);
}
for(i = 1; i < n; i++)
{
if(*element > *(element+i))
{
*element = *(element+i);
}
}
printf("Smallest element is %d", *element);
return 0;
}
30. Program to understand the use of realloc () function
#include<stdio.h>
#include<stdlib.h>
int main( )
{
int i, *ptr;
ptr = (int *)malloc(5 *sizeof(int));
if(ptr == NULL)
{
printf("Memory not available\n");
exit(1);
}
printf("Enter 5 integers : ");
for(i=0; i<5; i++)
scanf("%d", ptr+i);
/*Allocate memory for 4 more integers*/
ptr = (int *)realloc(ptr, 9*sizeof(int));
if(ptr == NULL)
{
printf("Memory not available\n");
exit(1);
}
printf("Enter 4 more integers : ");
for(i=5; i<9; i++)
scanf("%d", ptr+i);
for(i=0; i<9; i++)
printf("%d ", *(ptr+i));
}
31. Program to understand the use of gets ()
function #include <stdio.h>
int main()
{
char name[50];
printf("\n Please Enter your Full Name: \n");
gets(name);
printf("=============\n");
printf("%s", name);
return 0;
}
32. Program to understand the use of strlen () function
#include<stdio.h>
#include <string.h>
int main()
{
char ch[]={'g', 'e', 'e', 'k', 's', '\0'};
printf("Length of string is: %d", strlen(ch));
return 0;
}
33. Program to understand the use of strcmp () function
#include <stdio.h>
#include <string.h>
int main () {
char str1[20];
char str2[20];
int result;
//Assigning the value to the string str1
strcpy(str1, "hello");
//Assigning the value to the string str2
strcpy(str2, "hEllo");
result = strcmp(str1, str2);
if(result > 0) {
printf("ASCII value of first unmatched character of str1 is greater than str2");
} else if(result < 0) {
printf("ASCII value of first unmatched character of str1 is less
than str2"); } else {
printf("Both the strings str1 and str2 are equal");
}
return 0;
}
34. Program to understand the use of strcpy () function
#include <stdio.h>
#include <string.h>
int main () {
char str1[20];
char str2[20];
//copying the string "Apple" to the str1
strcpy(str1, "A1");
printf("String str1: %s\n", str1);
//copying the string "Banana" to the str2
strcpy(str2, "A2");
printf("String str2: %s\n", str2);
//copying the value of str2 to the string str1
strcpy(str1, str2);
printf("String str1: %s\n", str1);
return 0;
}
35. Program to understand the use of strcat () function
#include <stdio.h>
#include <string.h>
int main () {
char str1[50], str2[50];
//destination string
strcpy(str1, "This is my initial string");
//source string
strcpy(str2, ", add this");
//concatenating the string str2 to the string str1
strcat(str1, str2);
//displaying destination string
printf("String after concatenation: %s", str1);
return(0);
}
36. Program to display the values of structure numbers
#include <stdio.h>
/* Created a structure here. The name of the structure is
* StudentData.
*/struct StudentData{
char *stu_name;
int stu_id;
int stu_age;
};
int main()
/* student is the variable of structure
StudentData*/ struct StudentData student;
/*Assigning the values of each struct member here*/
student.stu_name = "S";
student.stu_id = 1134;
student.stu_age = 22;
/* Displaying the values of struct
members */ printf("Student Name is: %s",
student.stu_name); printf("\nStudent Id
is: %d", student.stu_id); printf("\nStudent
Age is: %d", student.stu_age); return 0;
}
Output:
Student Name is: S
Student Id is: 1134
Student Age is: 22
37. Program to understand array of structure
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record[2];
// 1st student's record
record[0].id=1;
strcpy(record[0].name, "Raju");
record[0].percentage = 86.5;
// 2nd student's record
record[1].id=2;
strcpy(record[1].name, "Surendren");
record[1].percentage = 90.5;
// 3rd student's record
record[2].id=3;
strcpy(record[2].name, "Thiyagu");
record[2].percentage = 81.5;
for(i=0; i<3; i++)
{
printf(" Records of STUDENT : %d \n", i+1);
printf(" Id is: %d \n", record[i].id);
printf(" Name is: %s \n", record[i].name);
printf(" Percentage is: %f\n\n",record[i].percentage);
}
return 0;
}
38. Program for accessing union numbers
#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;
int main() {
j.salary = 12.3;
j.workerNo = 100;
printf("Salary = %.1f\n", j.salary);
printf("Number of workers = %d", j.workerNo);
return 0;
}
39. Program to compare the memory allocated for a union and structure
#include <stdio.h>
#include <string.h>
struct struct_example
{
int integer;
float decimal;
char name[20];
};
union union_example
{
int integer;
float decimal;
char name[20];
};
void main()
{
struct struct_example s={18,38,"geeksforgeeks"};
union union_example u={18,38,"geeksforgeeks"};
printf("structure data:\n integer: %d\n"
"decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
printf("\nunion data:\n integeer: %d\n"
"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
printf("\nsizeof structure : %d\n", sizeof(s));
printf("sizeof union : %d\n", sizeof(u));
printf("\n Accessing all members at a time:");
s.integer = 183;
s.decimal = 90;
strcpy(s.name, "geeksforgeeks");
printf("structure data:\n integer: %d\n "
"decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
u.integer = 183;
u.decimal = 90;
strcpy(u.name, "geeksforgeeks");
printf("\nunion data:\n integeer: %d\n "
"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
printf("\n Accessing one member at time:");
printf("\nstructure data:");
s.integer = 240;
printf("\ninteger: %d", s.integer);
s.decimal = 120;
printf("\ndecimal: %f", s.decimal);
strcpy(s.name, "C programming");
printf("\nname: %s\n", s.name);
printf("\n union data:");
u.integer = 240;
printf("\ninteger: %d", u.integer);
u.decimal = 120;
printf("\ndecimal: %f", u.decimal);
strcpy(u.name, "C programming");
printf("\nname: %s\n", u.name);
printf("\nAltering a member value:\n");
s.integer = 1218;
printf("structure data:\n integer: %d\n "
" decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
u.integer = 1218;
printf("union data:\n integer: %d\n"
" decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
}
Output:
structure data:
integer: 18
decimal: 38.00
name: geeksforgeeks
union data:
integeer: 18
decimal: 0.00
name:
sizeof structure : 28
sizeof union : 20
Accessing all members at a time:structure data:
integer: 183
decimal: 90.00
name: geeksforgeeks
union data:
integeer: 1801807207
decimal: 277322871721159510000000000.00
name: geeksforgeeks
Accessing one member at time:
structure data:
integer: 240
decimal: 120.000000
name: C programming
union data:
integer: 240
decimal: 120.000000
name: C programming
Altering a member value:
structure data:
integer: 1218
decimal: 120.00
name: C programming
union data:
integer: 1218
decimal: 0.00
name: -