0% found this document useful (0 votes)
7 views

c lab

The document provides C programs to check whether a character is an alphabet, digit, or special character, as well as to determine if a number is an Armstrong number, prime number, or palindrome. It includes detailed logic and code examples for each program, explaining the conditions and operations used. The document serves as a tutorial for basic C programming concepts related to character and number classification.

Uploaded by

supremkhadka03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

c lab

The document provides C programs to check whether a character is an alphabet, digit, or special character, as well as to determine if a number is an Armstrong number, prime number, or palindrome. It includes detailed logic and code examples for each program, explaining the conditions and operations used. The document serves as a tutorial for basic C programming concepts related to character and number classification.

Uploaded by

supremkhadka03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Write a C program to input a character from user and check whether given

character is alphabet, digit or special character

Logic to check alphabet, digit or


special character
 A character is alphabet if it in between a-z or A-Z.
 A character is digit if it is in between 0-9.
 A character is special symbol character if it neither alphabet nor
digit.
Step by step descriptive logic to check alphabet, digit or special character.

1. Input a character from user. Store it in some variable say ch.


2. First check if character is alphabet or not. A character is
alphabet if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <=
'Z')).

3. Next, check condition for digits. A character is digit if(ch >= '0' &&
ch <= '9').

4. Finally, if a character is neither alphabet nor digit, then character


is a special character.
Let us implement the above logic in a C program.

Program to check alphabet, digit or


special character
/**
* C program to check alphabet, digit or special character
*/

#include <stdio.h>

int main()
{
char ch;

/* Input character from user */


printf("Enter any character: ");
scanf("%c", &ch);

/* Alphabet check */
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("'%c' is alphabet.", ch);
}
else if(ch >= '0' && ch <= '9')
{
printf("'%c' is digit.", ch);
}
else
{
printf("'%c' is special character.", ch);
}

return 0;
}

Note: You can also use ASCII character codes for checking alphabets, digits
or special characters as shown in below program.

Program to check alphabet, digit or


special character using ASCII value
/**
* C program to check alphabet, digit or special character using ASCII value
*/

#include <stdio.h>

int main()
{
char ch;

/* Input a character from user */


printf("Enter any character: ");
scanf("%c", &ch);

if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
{
printf("'%c' is alphabet.", ch);
}
else if(ch >= 48 && ch <= 57)
{
printf("'%c' is digit.", ch);
}
else
{
printf("'%c' is special character.", ch);
}

return 0;
}

C Program to Check Armstrong


Number
A positive integer is called an Armstrong number (of order n ) if

abcd... = an + bn + cn + dn +

In the case of an Armstrong number of 3 digits, the sum of cubes of each digit
is equal to the number itself. For example, 153 is an Armstrong number
because

153 = 1*1*1 + 5*5*5 + 3*3*3

Check Armstrong Number of three digits


#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;

while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;

result += remainder * remainder * remainder;

// removing last digit from the orignal number


originalNum /= 10;
}

if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);

return 0;
}
Run Code

Output

Enter a three-digit integer: 371


371 is an Armstrong number.

C Program to Check Whether a


Number is Prime or Not
A prime number is a positive integer that is divisible only by 1 and itself. For
example: 2, 3, 5, 7, 11, 13, 17.
Program to Check Prime Number
#include <stdio.h>

int main() {

int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

// 0 and 1 are not prime numbers


// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;

for (i = 2; i <= n / 2; ++i) {

// if n is divisible by i, then n is not prime


// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}

// flag is 0 for prime numbers


if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}
Run Code

Output
Enter a positive integer: 29
29 is a prime number.

In the program, a for loop is iterated from i = 2 to i < n/2 .

In each iteration, whether n is perfectly divisible by i is checked using:

if (n % i == 0) {
flag = 1;
break;
}

If n is perfectly divisible by i , n is not a prime number. In this case, flag is set


to 1, and the loop is terminated using the break statement.
Notice that we have initialized flag as 0 during the start of our program.
So, if n is a prime number after the loop, flag will still be 0. However, if n is a
non-prime number, flag will be 1.

C Program to Check Whether a


Number is Palindrome or Not
Program to Check Palindrome
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
// reversed integer is stored in reversed variable
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

// palindrome if orignal and reversed are equal


if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return 0;
}
Run Code

Output

Enter an integer: 1001


1001 is a palindrome.

Here, the user is asked to enter an integer. The number is stored in variable n .
We then assigned this number to another variable orignal . Then, the reverse
of n is found and stored in reversed .

If original is equal to reversed , the number entered by the user is a


palindrome.
Before we wrap up, let's put your understanding of this example to the test!
Can you solve the following challenge?

You might also like