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 Type | Size | Range |
|---|---|---|
char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 255 |
int | 2 or 4 bytes | -32,768 to 32,767 (2 bytes) / -2,147,483,648 to 2,147,483,647 (4 bytes) |
unsigned int | 2 or 4 bytes | 0 to 65,535 (2 bytes) / 0 to 4,294,967,295 (4 bytes) |
short int | 2 bytes | -32,768 to 32,767 |
long int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 bytes | 0 to 4,294,967,295 |
float | 4 bytes | 3.4E-38 to 3.4E+38 (6 decimal digits precision) |
double | 8 bytes | 1.7E-308 to 1.7E+308 (15 decimal digits precision) |
long double | 10 bytes | 3.4E-4932 to 1.1E+4932 |
void | 0 bytes | No 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
- Read the integer
n - Store original number in
original - Reverse the digits of
nto getreversed - If
original == reversed, it is a palindrome - 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)
| Iteration | n | remainder (n%10) | reversed |
|---|---|---|---|
| Start | 121 | - | 0 |
| 1st | 12 | 1 | 0*10+1 = 1 |
| 2nd | 1 | 2 | 1*10+2 = 12 |
| 3rd | 0 | 1 | 12*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.