C - exit() function

The exit() function is use to terminate a program. The header file for exit() function is stdlib.h

Example 1: WAP to find a input number is prime or not using exit() function.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    int n, i;
    printf("Enter number: ");
    scanf("%d", &n);
    for (i=2; i<=n/2; i++)
    {
        if(n%2==0)
        {
            printf("\n%d is not a Prime Number",n);
            getch();
            exit(0);
        }
    }
    printf("\n%d is a Prime Number",n);
}
Output 1:
Enter number: 20
20 is not a Prime Number

Output 2:
Enter number: 11
11 is a Prime Number

C - Gotoxy function

Gotoxy function is use to print the statement at appropriate position on the screen. The parameters passed to gotoxy() are column number and row number.

Example 2: Print message "Hello" in the 5th row and 20th column. (Run in Turbo C++ IDE)

#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   gotoxy(20,5); //gotoxy(col, row);
   printf("Hello");
   getch();
}
Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10
Input numbers are: 1 2 3 4 5 6 7 8 9 10

Exercise Questions - gotoxy()

  1. Rewrite the Shopping list program using gotoxy.

    Solution: C

  2. Print table from 1 to 10.

    Solution: C

  3. Print message that will scroll on the console window.