I/O STATEMENT, OPERATOR AND EXPRESSION
EX.NO:1
DATE:
AIM;
To write a c program for I/O statement using operators and expression.
ALGORITHM:
STEP 1: start the program.
STEP 2: declare all required variables and initialize them.
STEP 3: get input values from the user for arithmetic operator.
STEP 4: use input function scanf() and output function printf() to display the output after the
calculation.
STEP 5: end the program.
PROGRAM:
#include <stdio.h>
int main()
{
int num1, num2, add, sub, mul;
float div;
printf("Enter two numbers: \n");
scanf("%d %d", &num1, &num2);
add = num1 + num2;
sub = num1 - num2;
mul = num1 * num2;
div = (float)num1 / num2;
printf("addition : %d\n", add);
printf("subtraction : %d\n", sub);
printf("multiplication : %d\n", mul);
printf("division : %.2f\n", div);
return 0;
}
OUTPUT:
Enter two numbers:
40
20
Addition:60
Subtraction:20
Multiplication:800
Division:2.000000
RESULT:
Thus the c program for I/O statement using operators and expression was executed.
FIND GIVEN NUMBER ODD OR EVEN
EX.NO:2A
DATE:
AIM:
To write a c program to check whether the given number is odd or even.
ALGORITHM:
STEP 1: start the program.
STEP 2: declare all required variables.
STEP 3: get an input from the users and store it in a variables.
STEP 4: give the appropriate if condition and required statement.
STEP 5: if the if condition(num%2 ==0) fails then execute else statement.
STEP 6: if the number end the program.
PROGRAM;
#include <stdio.h>
int main()
{
intnum;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
{
printf("%d is even.\n", num);
}
else
{
printf("%d is odd.\n", num);
}
return 0;
}
OUTPUT1;
Enter a number:59
59 is odd
OUTPUT2:
Enter a number:24
24 is even
RESULT:
Thus the c program for checking whether the given number is odd or even was executed.
PRINT NUMBER USING LOOP
EX.NO:2B
DATE:
AIM:
To write a program for using loop to print numbers.
ALGORITHM:
STEP 1: start the program.
STEP 2: declare the variables and initialize it.
STEP 3: print the statement and use increment.
STEP 4: give the if condition as(i<=10).
STEP 5: if the condition is true then goto loop.
STEP 6: if the condition fails end the program.
PROGRAM;
#include <stdio.h>
int main()
{
inti = 1;
loop:
printf("Value of i: %d\n", i);
i++;
if (i<= 10)
{
goto loop;
}
return 0;
}
OUTPUT:
Value of i:1
Value of i:2
Value of i:3
Value of i:4
Value of i:5
Value of i:6
Value of i:7
Value of i:8
Value of i:9
Value of i:10
RESULT:
Thus the c program to print numbers using loop was executed.
PRINT WORD USING SWITCH CASE
EX.NO:2C
DATE:
AIM:
To print word using switch case in c program.
ALGORITHM:
STEP 1: start the program.
STEP 2: declare the variables and get a value from user.
STEP 3: enter three choices.
STEP 4: give an default statement.
STEP 5: end.
PROGRAM:
#include <stdio.h>
int main()
{
int choice;
printf("Select an option:\n");
printf("1. Print Hello\n");
printf("2. Print World\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Hello\n");
break;
case 2:
printf("World\n");
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
return 0;
}
OUTPUT:
Select an option:
1. print Hello
2. print World
3. Exit
Enter your choice:2
World
RESULT:
Thus the c program for printing word by using switch case was executed.
TRIANGLE PATTERN USING FOR LOOP
EX.NO:3A
DATE:
AIM:
Write a program to display the following pattern
ALGORITHM:
STEP 1: Start
STEP 2: Declare two variables
STEP 3: Give the for loop condition and use the increment program
STEP 4: Print “*”
STEP 5: End the program
PROGRAM:
#include <stdio.h>
int main()
{
inti,j;
for (i = 1; i<= 5; i++)
{
for (j=1;j<=i; j++)
printf("*");
}
printf("\n");
}
}
OUTPUT:
*
**
***
****
*****
RESULT:
Thus the c program to print triangle pattern using for loop was executed.
PRINT EVEN NUMBERS USING WHILE LOOP
EX.NO:3B
DATE:
AIM:
To write c program to print even numbers by using while loop
ALGORITHM:
STEP 1: Start
STEP 2: Declare a variable and initialize it
STEP 3: Give while condition as (i<=0) and if condition as (i%2==0)
STEP 4: Printi and use increment program
STEP 5: End
PROGRAM:
#include <stdio.h>
int main()
{
inti = 1;
while (i<= 10)
{
if(i%2==0)
{
printf("%d\n", i);
}
i++ ;
}
return 0;
}
OUTPUT
2
4
6
8
10
RESULT:
Thus the c program to print even numbers by using while loop is executed.
PRINT ODD NUMBERS USING DO WHILE LOOP
EX.NO:3C
DATE:
AIM:
To write a c program to print odd numbers using do while loop
ALGORITHM:
STEP 1: Start
STEP 2: Declare a variable and initialize it
STEP 3: Give the if condition as (i%2!=0)
STEP 4: Print “I”
STEP 5: Use increment operator
STEP 6: End
PROGRAM:
#include <stdio.h>
int main()
{
inti = 1;
do
{
if(i%2!=0)
{
printf("%d\n", i);
}
i++ ;
}while (i<= 10);
return 0;
}
OUTPUT:
1
3
5
7
9
RESULT:
Thus the c program to print odd numbers using do while loop is executed.
ARRAYS 1D AND 2D MULTI DIMENSIONAL ARRAYS AND TRAVERSAL
EX.NO:4
DATE:
AIM:
To write a c program of 1D and 2D multi dimensional arrays and traversal
ALGORITHM;
STEP 1: Start the program
STEP 2: Use the ID array-user defined function.
STEP 3: Using the four loop and printing the ar[i].
STEP 4: Use the 2D array. [intarr[Rows ] [cols])
STEP 5: Use for loop print arr[i][j];
STEP 6: Call the main function.
STEP 7: Traverse the 1D & 2D arrays
STEP 8: End of the program.
PROGRAM:
#include <stdio.h>
#define ROWS 3
#define COLS 4
//1D array - user defined function
void print1DArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
//2D array - user defined function
void print2DArray(int arr[ROWS][COLS])
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main()
{
// 1D array traversal
int arr1D[] = {1, 2, 3, 4, 5};
int size1D = sizeof(arr1D) / sizeof(arr1D[0]);
printf("1D Array:\n");
print1DArray(arr1D, size1D);
// 2D array traversal
int arr2D[ROWS][COLS] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
printf("\n2D Array:\n");
print2DArray(arr2D);
return 0;
}
OUTPUT:
1D Array:
12345
2D Array:
1234
5678
9 10 11 12
RESULT:
Thus the c program of 1D and 2D multi dimensional arrays and transversal is executed.
OPERATION OF STRING
EX.NO:5
DATE:
AIM:
To write a c program for operation of string
ALGORITHM:
STEP 1: Start the program
STEP 2: Declare the header File & call the main function.
STEP 3: Initialize the string character.
STEP 4: Use various string function and its syntax
STEP 5: End the program
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "programming in c";
char str2[20] = "language";
char str3[40];
printf("Length of str1: %ld \n", strlen(str1));
strcpy(str3, str1);
printf("str3 after copying str1: %s\n", str3);
strcat(str1, " ");
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
int result = strcmp(str1, str2);
if (result == 0)
{
printf("str1 and str2 are equal\n");
}
else if (result < 0)
{
printf("str1 is less than str2\n");
}
else
{
printf("str1 is greater than str2\n");
}
char *ptr = strstr(str1, "language");
if (ptr != NULL)
{
printf("Substring 'language' found at index: %ld\n", ptr - str1);
}
else
{
printf("Substring not found\n");
}
return 0;
}
OUTPUT:
Length of str1: 16
str3 after copying str1: programming in c
Concatenated string: programming in c language
str1 is greater than str2
Substring 'language' found at index: 17
RESULT:
Thus the c program for operation of string is executed
FUNCTION
EXP.NO: 6
DATE :
AIM:
To write a c program to perform various function
ALGORITHM:
STEP 1: Start the program.
STEP 2: Use the function which takes two integrals parameters and return their sum.
STEP 3: Use the swapping function using pass by reference.
STEP 4: Use function that print the elements of an integer array.
STEP 5: Call the function and return a value.
STEP 6: Passing parameters by reference and swapping values.
STEP 7: Passing an array to a function.
STEP 8: Modifying the array inside the function.
STEP 9: Stop the program.
PROGRAM:
#include <stdio.h>
// Function that takes two integers as parameters and returns their sum
int add(int a, int b)
{
return a + b;
}
// Function that swaps the values of two integers using pass-by-reference
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Function that prints the elements of an integer array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int num1 = 5, num2 = 10;
// Function call and returning a value
int sum = add(num1, num2);
printf("Num1 : %d\n",num1);
printf("Num2 : %d\n",num2);
printf("Sum: %d\n", sum);
// Passing parameters by reference and swapping values
printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swap: num1 = %d, num2 = %d\n", num1, num2);
// Passing an array to a function
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Array before modification: ");
printArray(arr, size);
// Modifying the array inside the function
for (int i = 0; i < size; i++)
{
arr[i] *= 15;
}
printf("Array after modification: ");
printArray(arr, size);
return 0;
}
OUTPUT:
Num1 : 5
Num2 : 10
Sum: 15
Before swap: num1 = 5, num2 = 10
After swap: num1 = 10, num2 = 5
Array before modification: 1 2 3 4 5
Array after modification: 15 30 45 60
RESULT:
Thus the c program for functions is executed
RECURSION
EXP.NO: 7
DATE :
AIM:
To write a c program to find the factorial of a number using recursion
ALGORITHM:
STEP 1: Start the program.
STEP 2: declare a variable globally.
STEP 3: use if or else conditions.
STEP 4: call the main function.
STEP 5: print a variable and get it from user.
STEP 6: give if condition as 9 (num<0) and give appropriate print f statement.
STEP 7: end the program.
PROGRAM:
#include <stdio.h>
int factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0)
{
printf("Error: Factorial is undefined for negative numbers.\n");
return 1;
}
int result = factorial(num);
printf("The factorial of %d is %d.\n", num, result);
return 0;
}
OUTPUT:
Enter a positive integer: 5
The factorial of 5 is 120.
RESULT:
Thus the c program for Recursion is executed
POINTER TO FUNCTIONS & ARRAY
EXP.NO: 8A
DATE :
AIM:
To write a c program using pointer to functions and array.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare variables (with arguments)
STEP 3: Using function add to numbers.
STEP 4: Using function subtract two numbers the variables.
STEP 5: Return the variables.
STEP 6: Call the main function.
STEP 7: Print the result of addition & subtraction variable to Arrays.
STEP 9: Print the variables to the array.
STEP 10: Use the pointer syntax and address the variable.
STEP 11: Use the for loop for pointer to Arrays.
STEP 12: Print the array.
STEP 13: Print the result.
STEP 14: End the program.
PROGRAM:
#include <stdio.h>
// Function to add two numbers
int add(int a, int b)
{
return a + b;
}
// Function to subtract two numbers
int subtract(int a, int b)
{
return a - b;
}
int main()
{
// Pointers to functions
int (*operation)(int, int);
operation = add;
int result = operation(4, 2);
printf("Result of addition: %d\n", result);
operation = subtract;
result = operation(4, 2);
printf("Result of subtraction: %d\n", result);
// pointer to Arrays
int numbers[] = {1, 2, 3, 4, 5};
int length = sizeof(numbers) / sizeof(numbers[0]);
printf("Array elements: ");
int (*ptr)[];
ptr = &numbers;
for (int i = 0; i < length; i++)
{
printf("%d ", (*ptr)[i]);
}
printf("\n");
return 0;
}
OUTPUT:
Result of addition: 6
Result of subtraction: 2
Array elements: 1 2 3 4 5
RESULT:
Thus the c program for Pointer to Functions and arrays is executed.
POINTERS TO STRING AND POINTERS AND ARRAYS OF POINTERS
EXP.NO: 8B
DATE :
AIM:
To write a c program of pointers to string and points and array of pointers.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Call the main function.
STEP 3: Print a string and use pointer.
STEP 4: Print the string characters using pointer.
STEP 5: Use while loop and increment the pointer values.
STEP 6: Declare variable and give the values.
STEP 7: Print**ptr toptr=&ptr3;
STEP 8: Print value.
STEP 9: For array of pointers declare and initialize the variable.
STEP 10: Print the array elements.
STEP 11: Use for loop to print*ptr_array[i];
STEP 12: End the program.
PROGRAM:
#include <stdio.h>
int main()
{
// Pointer to a string
char *message = "Hello, World!";
char *ptr2 = message;
printf("String characters using pointer: ");
while (*ptr2 != '\0')
{
printf("%c", *ptr2);
ptr2++;
}
printf("\n");
// Pointers to Pointers
int value = 42;
int *ptr3 = &value;
int **ptr_to_ptr = &ptr3;
printf("Value: %d\n", **ptr_to_ptr);
// Array of Pointers
int x = 1, y = 2, z = 3;
int *ptr_array[] = {&x, &y, &z};
printf("Array elements: ");
for (int i = 0; i < 3; i++)
{
printf("%d ", *ptr_array[i]);
}
printf("\n");
return 0;
}
OUTPUT:
String characters using pointer: Hello, World!
Value: 42
Array elements: 1 2 3
RESULT:
Thus the c program for Pointer to Strings and arrays of pointers is executed.
NESTED STRUCTURES AND POINTERS TO STRUCTURES
EXP.NO: 9A
DATE :
AIM:
To write nested structure collecting employees & program to and pointers to structures for
perform details.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Consider two structures, Employee and date & it has the data members like name, salary
employee ID.
STEP 3: Use union function to get id, price and name.
STEP 4: Call the main function.
STEP 5: Using struct employee, emp1 print the day, month, year, ID.
STEP 6: Print the Employee a details.
STEP 7: Using pointer to structure function print employee 2 details.
STEP 8: Print employee's ID, name, day, month, year.
STEP 9: Print the employee 2 details.
STEP 10: Print return 0.
STEP 11: End the program.
PROGRAM:
#include <stdio.h>
#include <string.h>
// Structure definitions
struct Date
{
int day;
int month;
int year;
};
struct Employee
{
int empId;
char name[50];
struct Date dob;
};
union Item
{
int id;
float price;
char name[20];
};
int main()
{
// Nested structures
struct Employee emp1;
emp1.empId = 1001;
strcpy(emp1.name," Kishore");
emp1.dob.day = 23;
emp1.dob.month = 10;
emp1.dob.year = 1994;
printf("Employee Details:\n");
printf("ID: %d\n", emp1.empId);
printf("Name: %s\n", emp1.name);
printf("Date of Birth: %d-%d-%d\n", emp1.dob.day, emp1.dob.month, emp1.dob.year);
printf("\n");
// Pointers to structures
struct Employee emp2;
struct Employee *ptr = &emp2;
ptr->empId = 1002;
strcpy(ptr->name, "Kasi Perumal");
ptr->dob.day = 20;
ptr->dob.month = 09;
ptr->dob.year = 1995;
printf("Employee Details (using pointer):\n");
printf("ID: %d\n", ptr->empId);
printf("Name: %s\n", ptr->name);
printf("Date of Birth: %d-%d-%d\n", ptr->dob.day, ptr->dob.month, ptr->dob.year);
printf("\n");
return 0;
}
OUTPUT:
Employee Details:
ID: 1001
Name: Kishore
Date of Birth: 23-10-1994
Employee Details (using pointer):
ID: 1002
Name: Kasi Perumal
Date of Birth: 20-09-1995
RESULT:
Thus the c program for Nested structures and pointers to structures is executed.
ARRAYS OF STRUCTURES AND UNIONS
EXP.NO: 9B
DATE:
AIM:
Write a c program to perform various of Array of Structures collecting employee's details and
union for functions
ALGORITHM:
STEP 1: Start the program.
STEP 2: Consider two structures as Employee and date & it has the data members like ID, Name
day, month, year.
STEP 3: Use union function to get ID, price and name.
STEP 4: Call the main functions.
STEPs 5: Using struct employee, print employee's day, month, ID,year and give the appropriate
array size.
STEP 6: Print the employee details using array of structures structures.
STEP 7: Using union function, print item id, item name and item. price.
STEP 8: Print return 0.
STEP 9: End the program.
PROGRAM:
#include <stdio.h>
#include <string.h>
// Structure definitions
struct Date
{
int day;
int month;
int year;
};
struct Employee
{
int empId;
char name[50];
struct Date dob;
};
union Item
{
int id;
float price;
char name[20];
};
int main()
{
// Arrays of structures
struct Employee employees[3];
employees[0].empId = 1001;
strcpy(employees[0].name, "Madhan Raj");
employees[0].dob.day = 27;
employees[0].dob.month = 10;
employees[0].dob.year = 1995;
employees[1].empId = 1002;
strcpy(employees[1].name, "Mathesh");
employees[1].dob.day = 05;
employees[1].dob.month = 10;
employees[1].dob.year = 1995;
employees[2].empId = 1003;
strcpy(employees[2].name, "Aravind Kumar");
employees[2].dob.day = 06;
employees[2].dob.month = 06;
employees[2].dob.year = 1996;
printf("Employee Details (array of structures):\n");
for (int i = 0; i < 3; i++)
{
printf("Employee %d:\n", i + 1);
printf("ID: %d\n", employees[i].empId);
printf("Name: %s\n", employees[i].name);
printf("Date of Birth: %d-%d-%d\n", employees[i].dob.day, employees[i].dob.month,
employees[i].dob.year);
printf("\n");
}
// Unions
union Item item;
item.id = 1001;
printf("Item ID: %d\n", item.id);
item.price = 99.99;
printf("Item Price: %.2f\n", item.price);
strcpy(item.name, "Shoes");
printf("Item Name: %s\n", item.name);
return 0;
}
OUTPUT:
Employee Details (array of structures):
Employee 1:
ID: 1001
Name: Madhan Raj
Date of Birth: 27-10-1995
Employee 2:
ID: 1002
Name: Mathesh
Date of Birth: 05-10-1995
Employee 3:
ID: 1003
Name: Aravind Kumar
Date of Birth: 06-06-1996
Item ID: 1001
Item Price: 99.99
Item Name: Shoes
RESULT:
Thus the c program for Arrays of structures and unions is executed.
READING AND WRITING, FILE POINTERS, RANDOM ACCESS, PROCESSOR
DIRECTIVES
EXP.NO: 10
DATE:
AIM:
To write a c program of files: reading and writing, file pointers, random access, processor
directives.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Call the main function.
STEP 3: Use the pointer to write a file.
STEP 4: Use the if condition as (file==NULL) to print “error in opening the file” and return 1.
STEP 5: Print Hello world! and close the file .
STEP 6: Read the file and use if condition to print “error in opening the file” and return 1 .
STEP 7: Use chat button [100] to print the content of file.
STEP 8: Close the file .
STEP 9: Use the random access function .
STEP 10: Using fseek function, move the file pointer .
STEP 11: Move the file pointer to the beginning of file access process and close the file.
STEP 12: Then print the content of file after random access process and close the file .
STEP 13: Use the preprocessor directives like #if def win32, #elif Linux, #elif APPLE and #end if.
STEP 14: Print return 0.
STEP 15: Stop the program.
PROGRAM:
#include <stdio.h>
int main()
{
// Writing to a file
FILE *file = fopen("example.txt", "w");
if (file == NULL)
{
printf("Error opening the file.\n");
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
// Reading from a file
file = fopen("example.txt", "r");
if (file == NULL)
{
printf("Error opening the file.\n");
return 1;
}
char buffer[100];
printf("Contents of the file:\n");
while (fgets(buffer, sizeof(buffer), file))
{
printf("%s", buffer);
}
fclose(file);
// Random access
file = fopen("example.txt", "r+");
if (file == NULL)
{
printf("Error opening the file.\n");
return 1;
}
fseek(file, 7, SEEK_SET); // Move the file pointer to the 7th character
fprintf(file, "Universe!\n"); // Overwrite the text from the 7th character onwards
rewind(file); // Move the file pointer to the beginning of the file
printf("\nContents of the file after random access:\n");
while (fgets(buffer, sizeof(buffer), file))
{
printf("%s", buffer);
}
fclose(file);
// Processor directives
#ifdef WIN32
printf("\nThis program Running on Windows.\n");
#elif linux
printf("\nThis program Running on Linux.\n");
#elif APPLE
printf("\nThis program Running on macOS.\n");
#endif
return 0;
}
OUTPUT:
Contents of the file:
Hello, World!
Contents of the file after random access:
Hello, Universe!
This program Running on Linux.
RESULT:
Thus the c program for Files: Reading and Writing ,File pointer, Random Access, Processor
Directives is executed.