C Programs for you – Set 2

C Programs for you – Set 2

Q1. Write a program to delete a specified line from a text file.

rn

Answer:

rn

include <stdio.h>

rn

 int main()

rn

{

rn

    FILE *fileptr1, *fileptr2;

rn

    char filename[40];

rn

    char ch;

rn

    int delete_line, temp = 1;

rn

     printf(“Enter file name: “);

rn

    scanf(“%s”, filename);

rn

    //open file in read mode

rn

    fileptr1 = fopen(filename, “r”);

rn

    ch = getc(fileptr1);

rn

 `  while (ch != EOF)

rn

    {

rn

        printf(“%c”, ch);

rn

        ch = getc(fileptr1);

rn

    }

rn

    //rewind

rn

    rewind(fileptr1);

rn

    printf(” n Enter line number of the line to be deleted:”);

rn

    scanf(“%d”, &delete_line);

rn

    //open new file in write mode

rn

    fileptr2 = fopen(“replica.c”, “w”);

rn

    ch = getc(fileptr1);

rn

    while (ch != EOF)

rn

    {

rn

        ch = getc(fileptr1);

rn

        if (ch == ‘n’)

rn

            temp++;

rn

            //except the line to be deleted

rn

            if (temp != delete_line)

rn

            {

rn

                //copy all lines in file replica.c

rn

                putc(ch, fileptr2);

rn

            }

rn

    }

rn

    fclose(fileptr1);

rn

    fclose(fileptr2);

rn

    remove(filename);

rn

    //rename the file replica.c to original name

rn

    rename(“replica.c”, filename);

rn

    printf(“n The contents of file after being modified are as follows:n”);

rn

    fileptr1 = fopen(filename, “r”);

rn

    ch = getc(fileptr1);

rn

    while (ch != EOF)

rn

    {

rn

        printf(“%c”, ch);

rn

        ch = getc(fileptr1);

rn

    }

rn

    fclose(fileptr1);

rn

    return 0;

rn

}

rn

Q2. Write a program to find the greatest among ten numbers.

rn

Answer:

rn

#include <stdio.h>

rn

  int main() {

rn

    int a[10];

rn

    int i;

rn

    int greatest;

rn

    printf(“Enter ten values:”);

rn

    //Store 10 numbers in an array

rn

    for (i = 0; i < 10; i++) {

rn

            scanf(“%d”, &a[i]);

rn

    }

rn

    //Assume that a[0] is greatest

rn

    greatest = a[0];

rn

    for (i = 0; i < 10; i++) {

rn

            if (a[i] > greatest) {

rn

            greatest = a[i];

rn

    }

rn

    }

rn

    printf(“

rn

    Greatest of ten numbers is %d”, greatest);

rn

    return 0;

rn

  }

rn

Q3. Write a program to check whether the given number is a prime.

rn

Answer:

rn

#include <stdio.h>

rn

#include <stdlib.h>

rn

 void main()

rn

{

rn

    int num, j, flag;

rn

     printf(“Enter a number n”);

rn

    scanf(“%d”, &num);

rn

     if (num <= 1)

rn

    {

rn

        printf(“%d is not a prime numbers n”, num);

rn

        exit(1);

rn

    }

rn

    flag = 0;

rn

    for (j = 2; j <= num / 2; j++)

rn

    {

rn

        if ((num % j) == 0)

rn

        {

rn

            flag = 1;

rn

            break;

rn

        }

rn

    }

rn

    if (flag == 0)

rn

        printf(“%d is a prime number n”, num);

rn

     else

rn

        printf(“%d is not a prime number n”, num);

rn

}

rn

Q4. Write a program to check whether the given number is a palindrome C number.

rn

Answer:

rn

#include <stdio.h>

rn

 void main()

rn

{

rn

    int num, temp, remainder, reverse = 0;

rn

     printf(“Enter an integer n”);

rn

    scanf(“%d”, &num);

rn

    /*  original number is stored at temp */

rn

    temp = num;

rn

    while (num > 0)

rn

    {

rn

        remainder = num % 10;

rn

        reverse = reverse * 10 + remainder;

rn

        num /= 10;

rn

    }

rn

    printf(“Given number is = %dn”, temp);

rn

    printf(“Its reverse is  = %dn”, reverse);

rn

    if (temp == reverse)

rn

        printf(“Number is a palindrome n”);

rn

    else

rn

        printf(“Number is not a palindrome n”);

rn

}

rn

Q5. Write a program to check whether the given string is a palindrome.

rn

Answer:

rn

#include <stdio.h>

rn

#include <string.h>

rn

 void main()

rn

{

rn

    char string[25], reverse_string[25] = {‘’};

rn

    int i, length = 0, flag = 0;

rn

     printf(“Enter a string n”);

rn

    gets(string);

rn

    /*  keep going through each character of the string till its end */

rn

    for (i = 0; string[i] != ‘’; i++)

rn

    {

rn

        length++;

rn

    }

rn

    printf(“The length of the string ‘%s’ = %dn”, string, length);

rn

    for (i = length – 1; i >= 0 ; i–)

rn

    {

rn

        reverse_string[length – i – 1] = string[i];

rn

    }

rn

   /*  Check if the string is a Palindrome */

rn

     for (flag = 1, i = 0; i < length ; i++)

rn

    {

rn

        if (reverse_string[i] != string[i])

rn

            flag = 0;

rn

    }

rn

    if (flag == 1)

rn

       printf (“%s is a palindrome n”, string);

rn

    else

rn

       printf(“%s is not a palindrome n”, string);

c