0% found this document useful (0 votes)
6 views

C Programming Loop

The document contains multiple C programming code snippets that perform various tasks such as reading and writing files, managing employee records, handling library information, manipulating strings, and performing mathematical operations. Each code snippet demonstrates different programming concepts, including file handling, data structures, recursion, and array manipulation. The snippets are designed for educational purposes, showcasing practical applications of C programming.

Uploaded by

aryankhanx595
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)
6 views

C Programming Loop

The document contains multiple C programming code snippets that perform various tasks such as reading and writing files, managing employee records, handling library information, manipulating strings, and performing mathematical operations. Each code snippet demonstrates different programming concepts, including file handling, data structures, recursion, and array manipulation. The snippets are designed for educational purposes, showcasing practical applications of C programming.

Uploaded by

aryankhanx595
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/ 14

1.

#include <stdio.h>
#include <string.h>
int main()
{
FILE *file;
FILE *trialFile;
char name[50];
char line[81];
int i, totalWords, length, fourLetterWords, lineCount;
i = totalWords = length = fourLetterWords = lineCount = 0;
trialFile = fopen("TRIAL.TXT", "r");

if (trialFile == NULL)
{
printf("Could not open TRIAL.TXT for reading.\n");
}
else
{
printf("Reading from TRIAL.TXT:\n\n");

while (fgets(line, sizeof(line), trialFile) != NULL && lineCount < 50)


{
printf("%s", line);
lineCount++;
}

fclose(trialFile);
printf("\nTotal lines read from TRIAL.TXT: %d\n\n", lineCount);
}
file = fopen("TRIAL.TXT", "w");

if (file == NULL)
{
printf("File Doesn't Exist\n");
}
else
{
printf("Enter a Name: ");
gets(name);
fputs(name, file);
i = 0;
while (name[i] != '\0')
{
if (name[i] == ' ')
totalWords++;
i++;
}
totalWords++; // for the last word
fprintf(file, "\nThe number of words: %d", totalWords);
i = 0;
length = 0;

while (name[i] != '\0')


{
if (name[i] != ' ' && name[i] != '\n')
{
length++;
}
else
{
if (length == 4)
fourLetterWords++;
length = 0;
}
i++;
}
if (length == 4)
fourLetterWords++;

fprintf(file, "\nThe number of four letter words: %d", fourLetterWords);


printf("\nFile created successfully\n");
fclose(file);
}
return 0;
}
3.
#include <stdio.h>
struct employee
{
int code;
char name[50];
int doj_day, doj_month, doj_year;
};
int main()
{
int num, i;
int curr_day, curr_month, curr_year;
printf("How many employees information: ");
scanf("%d", &num);
struct employee employee[num];
for(i = 0; i < num; i++)
{
printf("\nEnter details of employee %d\n", i+1);
printf("Code: ");
scanf("%d", &employee[i].code);

printf("Name: ");
fflush(stdin);
gets(employee[i].name); // safer than gets()

printf("Date of Joining (dd mm yyyy): ");


scanf("%d %d %d", &employee[i].doj_day, &employee[i].doj_month,
&employee[i].doj_year);
}
printf("\nEnter current date (dd mm yyyy): ");
scanf("%d %d %d", &curr_day, &curr_month, &curr_year);
printf("\nEmployees with tenure >= 3 years:\n");
for(i = 0; i < num; i++)
{
int years = curr_year - employee[i].doj_year;
if (curr_month < employee[i].doj_month || (curr_month ==
employee[i].doj_month && curr_day < employee[i].doj_day))
{
years--;
}
if (years >= 3)
{
printf("%s\n", employee[i].name);
}
}
return 0;
}
4.
#include <stdio.h>
#include <string.h>
#define MAX 100

struct library {
int acc_no;
char title[100];
char author[100];
float price;
int issued;
};
int main() {
struct library books[MAX];
int count = 0;
int choice;
while(1) {
printf("\n----- Library Menu -----\n");
printf("1. Add book information\n");
printf("2. Display book information\n");
printf("3. List all books of given author\n");
printf("4. List the title of specified book\n");
printf("5. List the count of books in the library\n");
printf("6. List the books in the order of accession number\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if(choice == 1) {
if (count >= MAX) {
printf("Library is full!\n");
continue;
}
printf("Enter accession number: ");
scanf("%d", &books[count].acc_no);
printf("Enter title: ");
scanf(" %[^\n]", books[count].title);
printf("Enter author: ");
scanf(" %[^\n]", books[count].author);
printf("Enter price: ");
scanf("%f", &books[count].price);
printf("Is the book issued? (1 for Yes, 0 for No): ");
scanf("%d", &books[count].issued);

count++;
}
else if(choice == 2) {
for(int i = 0; i < count; i++) {
printf("\nBook %d:\n", i + 1);
printf("Accession No: %d\n", books[i].acc_no);
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Price: %.2f\n", books[i].price);
printf("Issued: %s\n", books[i].issued ? "Yes" : "No");
}
}
else if(choice == 3) {
char search_author[100];
int found = 0;
printf("Enter author name: ");
scanf(" %[^\n]", search_author);

printf("Books by %s:\n", search_author);


for(int i = 0; i < count; i++) {
if(strcmp(books[i].author, search_author) == 0) {
printf("%s\n", books[i].title);
found = 1;
}
}
if (!found) {
printf("No books found by this author.\n");
}
}
else if(choice == 4) {
char search_title[100];
int found = 0;
printf("Enter title: ");
scanf(" %[^\n]", search_title);
for(int i = 0; i < count; i++) {
if(strcmp(books[i].title, search_title) == 0) {
printf("Book found:\n");
printf("Accession No: %d\n", books[i].acc_no);
printf("Author: %s\n", books[i].author);
printf("Price: %.2f\n", books[i].price);
printf("Issued: %s\n", books[i].issued ? "Yes" : "No");
found = 1;
break;
}
}
if (!found) {
printf("Book not found.\n");
}
}
else if(choice == 5) {
printf("Total number of books in the library: %d\n", count);
}
else if(choice == 6) {
printf("Books in the order of accession number:\n");
for(int i = 0; i < count; i++) {
printf("%d - %s\n", books[i].acc_no, books[i].title);
}
}
else if(choice == 7) {
printf("Exiting the program. Thank you!\n");
break;
}
else {
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
5.
#include<stdio.h>
int main()
{
int i=0,j=0;
char str[80],ne[80];
printf("Enter a String:");
gets(str);

while(str[i]!='\0')
{
char ch=str[i];
if(!(ch == 'a'|| ch== 'e'|| ch=='i'|| ch=='o'|| ch=='u'||ch == 'A'|| ch== 'E'|| ch=='I'||
ch=='O'|| ch=='U'))
{
ne[j]=ch;
j++;
}
i++;
}
ne[j]='\0';
printf("Sentence without vowels:%s",ne);
return 0;
}
6.
#include <stdio.h>
#include <string.h>

void extractSubstring(char str[], int pos, int len)


{
int i;
int str_len = strlen(str);
if (pos < 0 || pos >= str_len)
{
printf("Invalid starting position.\n");
return;
}
if (len == 0)
{
len = str_len - pos;
}
char result[100];
for (i = 0; i < len && str[pos + i] != '\0'; i++)
{
result[i] = str[pos + i];
}
result[i] = '\0';

printf("Extracted string: %s\n", result);


}
int main()
{
char input[100];
int pos, len;

printf("Enter the string: ");


fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0';
printf("Enter starting position (0-based): ");
scanf("%d", &pos);
printf("Enter number of characters to extract (0 for all): ");
scanf("%d", &len);

extractSubstring(input, pos, len);


return 0;
}
8.
#include <stdio.h>
int sumOfDigits(int x)
{
if (x == 0)
return 0;
else
return (x % 10) + sumOfDigits(x / 10);
}
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);

int result = sumOfDigits(num);


printf("Sum of digits: %d\n", result);

return 0;
}
9.
#include <stdio.h>
int power(int a, int b)
{
if (b == 0)
return 1;
else
return a * power(a, b - 1);
}

int main()
{
int base, exponent;
printf("Enter base (a): ");
scanf("%d", &base);
printf("Enter exponent (b): ");
scanf("%d", &exponent);

int result = power(base, exponent);


printf("%d^%d = %d\n", base, exponent, result);

return 0;
}
10.
#include <stdio.h>
void reverseArray(int *arr, int n) {
int *start = arr;
int *end = arr + n - 1;
while (start < end) {
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main()
{
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
reverseArray(arr, n);
printf("Reversed array:\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
7.
#include <stdio.h>
#include <math.h>
#define SIZE 6
void getCofactor(int mat[SIZE][SIZE], int temp[SIZE][SIZE], int p, int q, int n)
{
int i = 0, j = 0;

for (int row = 0; row < n; row++) {


for (int col = 0; col < n; col++) {
if (row != p && col != q) {
temp[i][j++] = mat[row][col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
}
}
int determinant(int mat[SIZE][SIZE], int n) {
int det = 0;
if (n == 1)
return mat[0][0];

int temp[SIZE][SIZE];
int sign = 1;
for (int f = 0; f < n; f++) {
getCofactor(mat, temp, 0, f, n);
det += sign * mat[0][f] * determinant(temp, n - 1);
sign = -sign;
}

return det;
}
int main()
{
int mat[SIZE][SIZE];

printf("Enter the elements of 6x6 matrix:\n");


for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
scanf("%d", &mat[i][j]);

int det = determinant(mat, SIZE);


printf("Determinant of the matrix = %d\n", det);

return 0;
}
2.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LEN 10

void reverseWord(char *word)


{
int len = strlen(word);
for (int i = len - 1; i >= 0; i--)
{
putchar(word[i]);
}
}
int main()
{
FILE *fp;
char ch;
char word[MAX_WORD_LEN + 1];
int index = 0;
fp = fopen("INPUT.TXT", "r");
if (fp == NULL)
{
printf("Error: Cannot open file.\n");
return 1;
}
printf("Output: ");
while ((ch = fgetc(fp)) != EOF) {
if (isspace(ch))
{
if (index > 0) {
word[index] = '\0';
reverseWord(word);
printf(" ");
index = 0;
}
} else {
if (index < MAX_WORD_LEN) {
word[index++] = ch;
}
}
}
if (index > 0) {
word[index] = '\0';
reverseWord(word);
}
fclose(fp);
return 0;
}

You might also like