C File
C File
C File
Output:-
Practical 02.
Write a program to find the greatest number among 3 numbers given by the
user.
Code:
#include <stdio.h>
#include<conio.h>
int main() {
int a,b,c;
printf("Enter three numbers\n");
scanf("%d %d %d",&a, &b, &c);
if(a>b && a>c){
printf("a is the greatest number");
}
else if (b>a && b>c){
printf("b is the greatest number");
}
else{
printf("c is the greatest number");
}
getch();
return 0;
}
Output:-
Practical 03.
Write a program to check if a given number is a prime number or not.
Code:
#include <stdio.h>
#include<conio.h>
int main() {
float n,i,result;
printf("Enter a number\n");
scanf("%f",&n);
result=n/i;
for(i=2;i<=n/2;i++){
if(result!=0){
printf("It is a prime number");
}
else{
printf("It is NOT a Prime number");
}
}
getch();
return 0;
}
Output:-
Practical 04.
Write a program to display the following pattern upto N rows, taking the
value of N from the user.
1
23
456
7 8 9 10
Code:
#include <stdio.h>
#include<conio.h>
int main() {
int n, i, j, num = 1;
printf("Enter the number of lines: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
getch();
return 0;
}
Output:-
Practical 05.
Write a program to input marks for 50 students using an array and display
the average marks of the class.
Code:
#include <stdio.h>
#include<conio.h>
int main() {
int marks[50];
int i;
float sum = 0.0, average;
for (i = 0; i < 50; i++) {
printf("Enter marks for student %d: ", i + 1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum / 50.0;
printf("The average marks of the class is: %.2f\n", average);
getch();
return 0;
}
Output:-
Practical 06.
Write a program to search for a number entered by the user in a given
array and display the array in ascending order.
Code:
#include <stdio.h>
#include<conio.h>
void sortArray(int arr[], int size) {
Output:-
Practical 09.
Code:
#include <stdio.h>
#include<conio.h>
#include <string.h>
#define NUM_EMPLOYEES 10
struct Employee {
int id;
char name[50];
int age;
char address[100];
char department[30];
float salary;
};
void inputEmployeeData(struct Employee employees[], int count) {
for (int i = 0; i < count; i++) {
printf("Enter details for Employee %d:\n", i + 1);
printf("Employee ID: ");
scanf("%d", &employees[i].id);
printf("Name: ");
scanf(" %[^\n]", employees[i].name);
printf("Age: ");
scanf("%d", &employees[i].age);
printf("Address: ");
scanf(" %[^\n]", employees[i].address);
printf("Department: ");
scanf(" %[^\n]", employees[i].department);
printf("Salary: ");
scanf("%f", &employees[i].salary);
printf("\n");
}
}
void displayEmployeeByID(struct Employee employees[], int count, int id) {
for (int i = 0; i < count; i++) {
if (employees[i].id == id) {
printf("\nEmployee Details:\n");
printf("ID: %d\n", employees[i].id);
printf("Name: %s\n", employees[i].name);
printf("Age: %d\n", employees[i].age);
printf("Address: %s\n", employees[i].address);
printf("Department: %s\n", employees[i].department);
printf("Salary: %.2f\n", employees[i].salary);
return;
}
}
printf("Employee with ID %d not found.\n", id);
}
int main() {
struct Employee employees[NUM_EMPLOYEES];
int id;
inputEmployeeData(employees, NUM_EMPLOYEES);
printf("Enter Employee ID to display details: ");
scanf("%d", &id);
displayEmployeeByID(employees, NUM_EMPLOYEES, id);
getch();
return 0;
}
Output:-
Practical 10.
Write a program to create two files with names EvenFile and OddFile. Input
20 numbers from the user and save even numbers in EvenFile and odd
numbers in OddFile.
Code:
#include <stdio.h>
#include<conio.h>
int main() {
int numbers[20];
FILE *evenFile, *oddFile;
evenFile = fopen("EvenFile.txt", "w");
if (evenFile == NULL) {
printf("Error opening EvenFile.txt\n");
return 1;
}
oddFile = fopen("OddFile.txt", "w");
if (oddFile == NULL) {
printf("Error opening OddFile.txt\n");
return 1;
}
printf("Enter 20 numbers:\n");
for (int i = 0; i < 20; i++) {
printf("Number %d: ", i + 1);
scanf("%d", &numbers[i]);
if (numbers[i] % 2 == 0) {
fprintf(evenFile, "%d\n", numbers[i]);
} else {
fprintf(oddFile, "%d\n", numbers[i]);
}
}
fclose(evenFile);
fclose(oddFile);
printf("Even numbers have been written to EvenFile.txt\n");
printf("Odd numbers have been written to OddFile.txt\n");
getch();
return 0;
}
Output:
Practical 11.
Write a menu driven program to construct a calculator for following
arithmetic operations: addition, subtraction, multiplication, division, average,
and percentage.
Code.
#include <stdio.h>
#include<conio.h>
int main() {
char op;
double a,b;
printf("Enter an operator(+,-,*,/,a,p):");
scanf("%c",&op);
printf("Enter 2 operands:");
scanf("%lf %lf",&a,&b);
switch(op){
case '+':
printf("%.1lf+%.1lf=%.1lf",a,b,a+b);
break;
case '-':
printf("%.1lf-%.1lf=%.1lf",a,b,a-b);
break;
case'*':
printf("%.1lf*%.1lf=%.1lf",a,b,a*b);
break;
case'/':
printf("%.1lf/%.1lf=%.1lf",a,b,a/b);
break;
case'a':
printf("%.1lfa%.1lf=%.1lf",a,b,(a+b)/2);
break;
case'p':
printf("%.1lfp%.1lf=%.1lf",a,b,(a+b)/100);
break;
default:
printf("Entered Operator is NOT correct");
}
getch();
return 0;
}
Output:-
Practical 12.
Write a menu driven program to perform the following operations:
1. Print armstrong numbers upto N
2. Display prime numbers between 1 to N
3. Reverse of an integer
Code:
#include<stdio.h>
#include<conio.h>
int main(){
int choice, n;
printf("\nMenu:\n");
printf("1. Print Armstrong numbers up to N\n");
printf("2. Display Prime numbers between 1 to N\n");
printf("3. Reverse of an integer\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a number: ");
scanf("%d", &n);
printf("Armstrong numbers up to %d: ", n);
for (int i = 1; i <= n; i++) {
if (armstrong(i)) printf("%d ", i);
}
printf("\n");
break;
case 2:
printf("Enter a number: ");
scanf("%d", &n);
printf("Prime numbers between 1 to %d: ", n);
for (int i = 1; i <= n; i++) {
if (prime(i)) printf("%d ", i);
}
printf("\n");
break;
case 3:
printf("Enter a number: ");
scanf("%d", &n);
printf("Reverse of %d: %d\n", n, reverse(n));
break;
default:
printf("Invalid choice. Please choose again.\n");
}
getch();
return 0;
}
Output:
Practical 13.
Write a program to calculate factorial of a number and display Fibonacci
series upto N terms using recursive functions.
Code:
#include <stdio.h>
#include <conio.h>
int fibonacci (int n){
if (n==0)
return 0;
else if (n==1)
return 1;
else
return fibonacci (n-1)+fibonacci(n-2);
}
int main(){
int n,i;
clrscr();
printf(“Enter the number of terms:”);
scanf(“%d”,&n);
printf(“Fibonacci series:”);
for(i=0; i<n;i++){
printf (“%d”,fibonacci(i));
}
getch();
return 0;
}
Output:
Practical 14.
WAP to perform I/O and make use of file positioning functions on Binary
files. (using fseek, tell, rewind functions)
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
int numbers[] = {10, 20, 30, 40, 50};
int num, position;
file = fopen("data.bin", "wb");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fwrite(numbers, sizeof(int), 5, file);
fclose(file);
file = fopen("data.bin", "rb");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fseek(file, 2 * sizeof(int), SEEK_SET);
fread(&num, sizeof(int), 1, file);
printf("Third number in file (using fseek): %d\n", num);
position = ftell(file);
printf("Current file position (after reading third number): %d\n", position);
rewind(file);
printf("File position after rewind: %d\n", ftell(file));
fread(&num, sizeof(int), 1, file);
printf("First number in file (after rewind): %d\n", num);
fclose(file);
getch();
return 0;
}
Output:
Practical 15.
Write a menu driven program to implement the following string
operations:
(i) Calculate length of a string
(ii) Concatenate at the end of a given
(iii) Copy one string to another
(iv) Compare contents of two strings
(v) Copy nth character string to another
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void calculate_length(char str[]){
printf("length of string: %lu\n", strlen(str));
}
void concatenate_strings(char str1[], char str2[]){
strcat(str1,str2);
printf("concatenated strings: %s\n", str1);
}
void copy_string(char source[], char destination[]) {
strcpy(destination, source);
printf("Copied string: %s\n", destination);
}
void compare_strings(char str1[], char str2[]){
int result= strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else {
printf("Strings are not equal\n");
}
}
void copy_nth_character(char str[], int n) {
if (n > 0 && n <= strlen(str)) {
printf("Nth character: %c\n", str[n-1]);
} else {
printf("Index out of range\n");
}
}
int main() {
char str1[100], str2[100], destination[100];
int choice, n;
while(1){
printf("Menu:\n");
printf("1. Calculate length of a string\n");
printf("2. Concatenate at the end of a given string\n");
printf("3. Copy one string to another\n");
printf("4. Compare contents of two strings\n");
printf("5. Copy nth character string\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a string: ");
scanf("%s", str1);
calculate_length(str1);
break;
case 2:
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
concatenate_strings(str1, str2);
break;
case 3:
printf("Enter a string to copy: ");
scanf("%s", str1);
copy_string(str1, destination);
break;
case 4:
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
compare_strings(str1, str2);
break;
case 5:
printf("Enter a string: ");
scanf("%s", str1);
printf("Enter the position: ");
scanf("%d", &n);
copy_nth_character(str1, n);
break;
case 6:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
getch();
return 0;
}
Output:-