C Program To Check Whether a Character is an Alphabet or Not

Lets write a C program to check if user input character is an alphabet or not.

Related Read:
Relational Operators In C
Logical Operators In C

Note: In C programming language, every character variable holds an ASCII value rather than the character itself. You can check ASCII value of all the characters here: C Program To Print All ASCII Characters and Code

Also watch video tutorial: C Program to Print ASCII Value of a Character

Note: ASCII value range of lower case alphabets:
ASCII value of a is 97.
ASCII value of b is 98.
ASCII value of c is 99.
and so on till z ..
ASCII value of z is 122.

ASCII value range of upper case alphabets:
ASCII value of A is 65.
ASCII value of B is 66.
ASCII value of C is 67.
and so on till z ..
ASCII value of Z is 90.

Source Code: C Program To Check Whether a Character is an Alphabet or Not

#include < stdio.h >

int main()
{
    char c;

    printf("Enter a character\n");
    scanf("%c", &c);

    if( (c >= 'a' && c <= 'z') ||
        (c >= 'A' && c <= 'Z') )
    {
        printf("%c is an alphabet\n", c);
    }
    else
    {
        printf("%c is not an alphabet\n", c);
    }

    return 0;
}

Output 1:
Enter a character
5
5 is not an alphabet

Output 2:
Enter a character
a
a is an alphabet

Output 3:
Enter a character
$
$ is not an alphabet

Output 4:
Enter a character
S
S is an alphabet

Output 5:
Enter a character
#
# is not an alphabet

Logic To Check Whether a Character is an Alphabet or Not

We accept the character from user as input. We check if the user entered character is greater than or equal to a and less than or equal to z OR the user entered character is greater than or equal to A and less than or equal to Z. If these conditions are met, then user entered character is an alphabet or else its not.

Video Tutorial: C Program To Check Whether a Character is an Alphabet or Not


[youtube https://www.youtube.com/watch?v=uY3d6SXBBNE]

YouTube Link: https://www.youtube.com/watch?v=uY3d6SXBBNE [Watch the Video In Full Screen.]

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to Calculate the Sum of Natural Numbers Between Range

Lets write a C program to calculate sum of all natural numbers between the user entered range of numbers, using while loop.

We assume that user enters smaller number first and biggest number next.

Related Read:
while loop in C programming
C Program to Print Natural Numbers Between Two Numbers using While loop

Source Code: C Program to Calculate the Sum of Natural Numbers Between Range

 
#include < stdio.h >

int main()
{
    int min, max, sum = 0;

    printf("Enter 2 positive numbers\n");
    scanf("%d%d", &min, &max);

    printf("\n");

    while(min <= max)
    {
        sum = sum + min;
        min++;
    }
    printf("Sum = %d\n", sum);
    return 0;
}

Output 1:
Enter 2 positive numbers
1
5
Sum = 15

Output 2:
Enter 2 positive numbers
10
15
Sum = 75

OR

 
#include < stdio.h >

int main()
{
    int min, max, sum = 0;

    printf("Enter 2 positive numbers\n");
    scanf("%d%d", &min, &max);

    printf("\n");

    while(min <= max)
    {
        sum = sum + min;
        printf("%d ", min);
        min++;

        if(min > max)
        {
            printf(" = %d\n", sum);
        }
        else
        {
            printf("+ ");
        }
    }

    return 0;
}

Output 1:
Enter 2 positive numbers
1
5

1 + 2 + 3 + 4 + 5 = 15

Output 2:
Enter 2 positive numbers
10
15

10 + 11 + 12 + 13 + 14 + 15 = 75

Output 3:
Enter 2 positive numbers
25
30

25 + 26 + 27 + 28 + 29 + 30 = 165

C Program to Calculate the Sum of Natural Numbers Between Range


[youtube https://www.youtube.com/watch?v=PqqCDtrVCmc]

YouTube Link: https://www.youtube.com/watch?v=PqqCDtrVCmc [Watch the Video In Full Screen.]


Logic To Calculate the Sum of Natural Numbers Between Range

We ask the user to enter minimum and maximum number(i.e., the range) and we store it inside variable min and max. We check if variable min is less than or equal to max. Until this condition is true, while loop keeps iterating. Inside while loop we increment the value of min by one for each iteration. We also add the value of min to the value of sum on each iteration. At the end, when min is greater than max, control exits while loop and we printout the value of sum – which has the sum of all the numbers between the range entered by the user.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to Print Natural Numbers Between Two Numbers using While loop

Lets write a C program to print natural numbers between two user entered numbers, using while loop.

We assume that user enters smaller number first and biggest number next.

Related Read:
while loop in C programming
Assignment Operators in C

Source Code: C Program to Print Natural Numbers Between Two Numbers using While loop

 
#include < stdio.h >

int main()
{
    int min, max;

    printf("Enter 2 positive numbers\n");
    scanf("%d%d", &min, &max);

    printf("Natural numbers between %d and %d are:\n", min, max);
    while(min <= max)
    {
        printf("%d  ", min);
        min++;
    }

    printf("\n");

    return 0;
}

Output 1:
Enter 2 positive numbers
10
20
Natural numbers between 10 and 20 are:
10 11 12 13 14 15 16 17 18 19 20

Output 2:
Enter 2 positive numbers
25
35
Natural numbers between 25 and 35 are:
25 26 27 28 29 30 31 32 33 34 35

C Program To Find Factorial of a Number using While Loop


[youtube https://www.youtube.com/watch?v=-IHXdLQQzYc]

YouTube Link: https://www.youtube.com/watch?v=-IHXdLQQzYc [Watch the Video In Full Screen.]


Logic To Print Natural Numbers Between Two Numbers using While loop

We ask the user to enter 2 numbers, and store it inside variables min and max. While loop keeps iterating till min is less than or equal to max. Inside while loop we keep incrementing the value of variable min by one for each iteration. We also display the value of min to the console window.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

range Input Type: HTML5

Browsers which support range input type know that the field is used for entering a number with in a given range.

form-input-type-range-type-html5

User Interface(UI) enhancement wise, you may get a slider control to select a range of number. You can even use min, max and step attributes to provide additional control for the field – for setting the range.

HTML file
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< !DOCTYPE html>
<html>
<head>
<title>range Input Type: HTML5</title>
<link href="myStyle.css" rel="stylesheet" />
<script src="myScript.js"></script>
</head>
<body>
 
<form>
<label>Range(2 to 10): </label>
 <input type="range" min="2" max="10" id="get" step="0.2" onchange="fetch()"/>
 <input type="number" min="2" max="10" step="0.2" id="put" />
 <input type="submit"/>
</form>
 
</body>
</html>

Here we have 2 input fields of type range and number respectively. Both having minimum allowed value as 2 and maximum allowed value as 10, and a step of 0.2 (fractional value). The number input field is taken for the sake of illustrating the value selected by the user using the range input field – by using(sliding) the slider.

JavaScript file
myScript.js

1
2
3
4
5
function fetch()
{
var get  = document.getElementById("get").value;
document.getElementById("put").value = get;
}

Here we fetch the value selected by the user using the slider and assign the value to the number input field.

CSS file associated with this is same as present at pattern and title Attribute of Form Field: HTML5

Form Input Type – range: HTML5


[youtube https://www.youtube.com/watch?v=YecwKiFBt0s]

YouTube Link: https://www.youtube.com/watch?v=YecwKiFBt0s [Watch the Video In Full Screen.]



Note: Browsers which do not support range input type, simply fall back to text input type. However, you need to validate the user entered data at server-side for more security. Client side validation is done mainly for the sake of creating the illustration of speed of validating the data and for a better user experience.