Character input in c

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Character function in c:

Character Input
To read a single character as input we use the getchar() function.

In the following example we will take a character as input from the user
and will print the ASCII code of the character.

#include <stdio.h>

int main(void)

char ch;

printf("Enter any character: ");

ch = getchar();

printf("Entered character: %c\n", ch);

printf("ASCII value: %d\n", ch);


return 0;

Output

Enter any character: A

Entered character: A

ASCII value: 65

Write a program in C to get a character input from the user and then
check if it is a digit
We will first take the character as input using the getchar() function then
we will use the isdigit() function to check if the entered character is a
digit. If the function returns non-zero value then the character is a digit
else it is not.

#include <stdio.h>

#include <ctype.h>

int main(void)

char ch;

printf("Enter character: ");

ch = getchar();

if (isdigit(ch)) {

printf("Entered character is a digit.");

else

{
printf("Entered character is not digit.");

return 0;

Output

Enter character: 6

Entered character is a digit.

isalnum():

#include<stdio.h>

int main()

char ch;

printf(“enter any character\n”);

scanf(“%c”,&ch);

if(isalnum(ch))

printf(“\nentered character is alphanumeric”);

else

printf(“\nentered character is not alphanumeric”);

return 0;
}

islower():

#include<stdio.h>

int main()

char ch;

printf(“enter any character\n”);

scanf(“%c”,&ch)

if(islower(ch))

printf(“\nentered character is lower case”);

else

printf(“\nentered character is not lower case”);

return 0;

isupper():

#include<stdio.h>

int main()

char ch;

printf(“enter any character\n”);

scanf(“%c”,&ch)
if(isupper(ch))

printf(“\nentered character is upper case”);

else

printf(“\nentered character is not upper case”);

return 0;

}
Program to print number of days in month
#include<stdio.h>
int main()
{
int month;
/* Input month number from user */
printf("Enter month number (1-12): ");
scanf("%d",&month);
if(month ==1)
{
printf("31 days");
}
elseif(month ==2)
{
printf("28 or 29 days");
}
elseif(month ==3)
{
printf("31 days");
}
elseif(month ==4)
{
printf("30 days");
}
elseif(month ==5)
{
printf("31 days");
}
elseif(month ==6)
{
printf("30 days");
}
elseif(month ==7)
{
printf("31 days");
}
elseif(month ==8)
{
printf("31 days");
}
elseif(month ==9)
{
printf("30 days");
}
elseif(month ==10)
{
printf("31 days");
}
elseif(month ==11)
{
printf("30 days");
}
elseif(month ==12)
{
printf("31 days");
}
else
{
printf("Invalid input! Please enter month number between (1-12).");
}

return0;
}

Program to print days in a month using logical OR operator


/**
* C program to print number of days in a month using logical operator
*/
#include<stdio.h>
intmain(){
int month;

/* Input month number from user */


printf("Enter month number (1-12): ");
scanf("%d",&month);

/* Group all 31 days conditions together using logical OR operator */


if(month==1|| month==3|| month==5|| month==7|| month==8||
month==10|| month==12)
{
printf("31 days");
}
elseif(month==4|| month==6|| month==9|| month==11)
{
/* Group all 30 days months together */
printf("30 days");
}
elseif(month==2)
{
printf("28 or 29 days");
}
else
{
printf("Invalid input! Please enter month number between (1-12).");
}
return0;
}
We saw two approaches to code this problem. As I always say if...else is
not recommended to use for fixed value condition checking. We must
use switch...case statement to perform action based on fixed choices
(constant month number 1-12 in this case).
Learn - Program to print number of days in month using
switch case.
For this problem you can also define constant number of days in array to
optimize the solution. Below is another approach to solve the given
problem using array. But to understand the below approach you must
have at least basic knowledge of arrays.

Program to print days in a month using array


/**
* C program to print number of days in a month using array
*/
#include<stdio.h>
intmain(){
/* Constant number of month declarations */
constint MONTHS[]={31,28,31,30,31,30,31,31,30,31,30,31};
int month;
/* Input month number from user */
printf("Enter month number (1-12): ");
scanf("%d",&month);
if(month >=1&& month <=12)
{
/* Print number of days */
printf("%d days", MONTHS[month -1]);
}
else
{
printf("Invalid input! Please enter month number between (1-12).");
}

return0;}

Output

Program to print days in a month USING SWITCH CASE:


main()
{
int month;

/* Input month number from user */


printf("Enter month number(1-12): ");
scanf("%d", &month);

switch(month)
{
case 1:
printf("31 days");
break;
case 2:
printf("28/29 days");
break;
case 3:
printf("31 days");
break;
case 4:
printf("30 days");
break;
case 5:
printf("31 days");
break;
case 6:
printf("30 days");
break;
case 7:
printf("31 days");
break;
case 8:
printf("31 days");
break;
case 9:
printf("30 days");
break;
case 10:
printf("31 days");
break;
case 11:
printf("30 days");
break;
case 12:
printf("31 days");
break;
default:
printf("Invalid input! Please enter month number between 1-
12");

}
Switch case with character:
Example: Simple Calculator
1. // Program to create a simple calculator
2. #include<stdio.h>
3.
4. int main() {
5. char operator;
6. double n1, n2;
7.
8. printf("Enter an operator (+, -, *, /): ");
9. scanf("%c", &operator);
10. printf("Enter two operands: ");
11. scanf("%lf %lf",&n1, &n2);
12.
13. switch(operator)
14. {
15. case'+':
16. printf("%.1f + %.1f = %.1f",n1, n2, n1+n2);
17. break;
18.
19. case'-':
20. printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
21. break;
22.
23. case'*':
24. printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
25. break;
26.
27. case'/':
28. printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
29. break;
30.
31. // operator doesn't match any case constant +, -, *, /
32. default:
33. printf("Error! operator is not correct");
34. }
35.
36. return0;
37. }

Output

Enter an operator (+, -, *,): -

Enter two operands: 32.5

12.4

32.5 - 12.4 = 20.1

Switch with char case

You might also like