Q1. Write a program to delete a specified line from a text file.
rnAnswer:
rninclude <stdio.h>
rnint main()
rn{
rnFILE *fileptr1, *fileptr2;
rnchar filename[40];
rnchar ch;
rnint delete_line, temp = 1;
rnprintf(“Enter file name: “);
rnscanf(“%s”, filename);
rn//open file in read mode
rnfileptr1 = fopen(filename, “r”);
rnch = getc(fileptr1);
rn` while (ch != EOF)
rn{
rnprintf(“%c”, ch);
rnch = getc(fileptr1);
rn}
rn//rewind
rnrewind(fileptr1);
rnprintf(” n Enter line number of the line to be deleted:”);
rnscanf(“%d”, &delete_line);
rn//open new file in write mode
rnfileptr2 = fopen(“replica.c”, “w”);
rnch = getc(fileptr1);
rnwhile (ch != EOF)
rn{
rnch = getc(fileptr1);
rnif (ch == ‘n’)
rntemp++;
rn//except the line to be deleted
rnif (temp != delete_line)
rn{
rn//copy all lines in file replica.c
rnputc(ch, fileptr2);
rn}
rn}
rnfclose(fileptr1);
rnfclose(fileptr2);
rnremove(filename);
rn//rename the file replica.c to original name
rnrename(“replica.c”, filename);
rnprintf(“n The contents of file after being modified are as follows:n”);
rnfileptr1 = fopen(filename, “r”);
rnch = getc(fileptr1);
rnwhile (ch != EOF)
rn{
rnprintf(“%c”, ch);
rnch = getc(fileptr1);
rn}
rnfclose(fileptr1);
rnreturn 0;
rn}
rnQ2. Write a program to find the greatest among ten numbers.
rnAnswer:
rn#include <stdio.h>
rnint main() {
rnint a[10];
rnint i;
rnint greatest;
rnprintf(“Enter ten values:”);
rn//Store 10 numbers in an array
rnfor (i = 0; i < 10; i++) {
rnscanf(“%d”, &a[i]);
rn}
rn//Assume that a[0] is greatest
rngreatest = a[0];
rnfor (i = 0; i < 10; i++) {
rnif (a[i] > greatest) {
rngreatest = a[i];
rn}
rn}
rnprintf(“
rnGreatest of ten numbers is %d”, greatest);
rnreturn 0;
rn}
rnQ3. Write a program to check whether the given number is a prime.
rnAnswer:
rn#include <stdio.h>
rn#include <stdlib.h>
rnvoid main()
rn{
rnint num, j, flag;
rnprintf(“Enter a number n”);
rnscanf(“%d”, &num);
rnif (num <= 1)
rn{
rnprintf(“%d is not a prime numbers n”, num);
rnexit(1);
rn}
rnflag = 0;
rnfor (j = 2; j <= num / 2; j++)
rn{
rnif ((num % j) == 0)
rn{
rnflag = 1;
rnbreak;
rn}
rn}
rnif (flag == 0)
rnprintf(“%d is a prime number n”, num);
rnelse
rnprintf(“%d is not a prime number n”, num);
rn}
rnQ4. Write a program to check whether the given number is a palindrome C number.
rnAnswer:
rn#include <stdio.h>
rnvoid main()
rn{
rnint num, temp, remainder, reverse = 0;
rnprintf(“Enter an integer n”);
rnscanf(“%d”, &num);
rn/* original number is stored at temp */
rntemp = num;
rnwhile (num > 0)
rn{
rnremainder = num % 10;
rnreverse = reverse * 10 + remainder;
rnnum /= 10;
rn}
rnprintf(“Given number is = %dn”, temp);
rnprintf(“Its reverse is = %dn”, reverse);
rnif (temp == reverse)
rnprintf(“Number is a palindrome n”);
rnelse
rnprintf(“Number is not a palindrome n”);
rn}
rnQ5. Write a program to check whether the given string is a palindrome.
rnAnswer:
rn#include <stdio.h>
rn#include <string.h>
rnvoid main()
rn{
rnchar string[25], reverse_string[25] = {‘