C Programming Loop
C Programming Loop
#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");
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;
printf("Name: ");
fflush(stdin);
gets(employee[i].name); // safer than gets()
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);
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>
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);
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;
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];
return 0;
}
2.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LEN 10