2078 3

BIT102 ยท 2078 ยท 10 marks

What different data types available in C along with their respective range?Write a program to check whether a given integer is palindrome or not.[5+5]

What different data types available in C along with their respective range?Write a program to check whether a given integer is palindrome or not.[5+5]

C ProgrammingChapter 2: Data Types and ConstantsBasic data types and their ranges

Quick answer

A data type defines the type of data a variable can hold, the amount of memory allocated, and the operations that can be performed on it.

Data Types in C with Their Ranges

(a) Data Types in C

Primary (Fundamental) Data Types in C

Data TypeSizeRange
char1 byte-128 to 127
unsigned char1 byte0 to 255
int2 or 4 bytes-32,768 to 32,767 (2 bytes) / -2,147,483,648 to 2,147,483,647 (4 bytes)
unsigned int2 or 4 bytes0 to 65,535 (2 bytes) / 0 to 4,294,967,295 (4 bytes)
short int2 bytes-32,768 to 32,767
long int4 bytes-2,147,483,648 to 2,147,483,647
unsigned long int4 bytes0 to 4,294,967,295
float4 bytes3.4E-38 to 3.4E+38 (6 decimal digits precision)
double8 bytes1.7E-308 to 1.7E+308 (15 decimal digits precision)
long double10 bytes3.4E-4932 to 1.1E+4932
void0 bytesNo value

Classification of Data Types

Data Types in C
โ”œโ”€โ”€ Primary (Basic)
โ”‚   โ”œโ”€โ”€ int
โ”‚   โ”œโ”€โ”€ char
โ”‚   โ”œโ”€โ”€ float
โ”‚   โ”œโ”€โ”€ double
โ”‚   โ””โ”€โ”€ void
โ”œโ”€โ”€ Derived
โ”‚   โ”œโ”€โ”€ Array
โ”‚   โ”œโ”€โ”€ Pointer
โ”‚   โ””โ”€โ”€ Function
โ””โ”€โ”€ User Defined
    โ”œโ”€โ”€ struct
    โ”œโ”€โ”€ union
    โ””โ”€โ”€ enum

(b) Program to Check Palindrome Integer

Concept

A number is called a palindrome if it reads the same forwards and backwards.

  • Example: 121, 1331, 12321 are palindromes
  • Example: 123, 456 are NOT palindromes

Algorithm

  1. Read the integer n
  2. Store original number in original
  3. Reverse the digits of n to get reversed
  4. If original == reversed, it is a palindrome
  5. Otherwise, it is not a palindrome

C Program

#include <stdio.h>

int main() {
    int n, original, reversed = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &n);

    original = n;

    /* Handle negative numbers - negative numbers are not palindromes */
    if (n < 0) {
        printf("%d is NOT a palindrome.\n", n);
        return 0;
    }

    /* Reverse the digits of the number */
    while (n != 0) {
        remainder = n % 10;       /* Extract last digit */
        reversed = reversed * 10 + remainder;  /* Build reversed number */
        n = n / 10;               /* Remove last digit */
    }

    /* Check if original and reversed are equal */
    if (original == reversed) {
        printf("%d is a PALINDROME.\n", original);
    } else {
        printf("%d is NOT a PALINDROME.\n", original);
    }

    return 0;
}

Dry Run / Trace (for n = 121)

Iterationnremainder (n%10)reversed
Start121-0
1st1210*10+1 = 1
2nd121*10+2 = 12
3rd0112*10+1 = 121
  • original = 121, reversed = 121
  • Since 121 == 121, it is a PALINDROME

Sample Output

Enter an integer: 121
121 is a PALINDROME.

Enter an integer: 123
123 is NOT a PALINDROME.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.