Today we’ll write a program using Character type data. Character type data is used to assign letters in English language and special symbols like summation (+), substriction (-), multiplication (*) and division (/) and etc. A character type symbol needs a byte to keep in memory.
How to Learn to Code C
The following program is to input and print the first letter of your name. Write the codes and run the program.
Code:
Output:#include <stdio.h>int main(){char ch;printf("Enter the first letter of your name: ");scanf("%c", &ch);printf("The first letter of your name is: %c\n", ch);return 0;}
We wrote char as it wanted to input a character type data. For this reason, we wrote %c in the scanf and printf quotation. ( \n ) is for new line.
We can write the program in another way,
Code:
Output:#include <stdio.h>int main(){char ch;printf("Enter the first letter of your name: ");ch = getchar();printf("The first letter of your name is: %c\n", ch);return 0;}
Getchar is also a function that can read character type data. In this program , getchar function assigned a character after reading. If we want to keep a character directly in a variable, we’ve to use quotation like char c = 'A';
Now, see the program like this…
Code:
Output:
#include<stdio.h>int main(){int num1, num2, value;char sign;printf("Please enter a number: ");scanf("%d", &num1);printf("Please enter another number: ");scanf("%d", &num2);value = num1 + num2;sign = '+';printf("%d %c %d = %d\n", num1, sign, num2, value);value = num1 - num2;sign = '-';printf("%d %c %d = %d\n", num1, sign, num2, value);value = num1 * num2;sign = '*';printf("%d %c %d = %d\n", num1, sign, num2, value);value = num1 / num2;sign = '/';printf("%d %c %d = %d\n", num1, sign, num2, value);return 0;}
Write the program and run it. See the output and try to understand. I wish you’ll be able to understand.
No comments:
Post a Comment