Lets write a C program to print/display ASCII value of a user entered character.
Note: In C programming language, every alphabet, number and symbol has corresponding ASCII value(a integer number representing the character).
Data Type
char is character data type in C programming language. It stores single character information in it and has a corresponding integer value for the character, which is its ASCII value.
Source Code: C Program to Print ASCII Value of a Character
#include < stdio.h >
int main()
{
char c;
printf("Enter a character\n");
scanf("%c", &c);
printf("ASCII value of %c is %d\n", c, c);
return 0;
}
Output 1: Enter a character A ASCII value of A is 65
Output 2: Enter a character a ASCII value of a is 97
Output 3: Enter a character 1 ASCII value of 1 is 49
Output 4: Enter a character 0 ASCII value of 0 is 48
Output 5: Enter a character $ ASCII value of $ is 36
Video Tutorial: C Program to Find ASCII Value of a Character
Lets write a C program to perform addition of 2 numbers without using plus symbol or the addition operator(+).
In this video tutorial we are using ~(tilde symbol) bitwise complement operator to perform the operation to get to the anticipated result.
Example: If user enters 2 numbers. a = 8 and b = 7. To add 8 and 7 we use result = a + b. And the result will be 15. But in this C program we are not using plus(+) operation, but still we must get the same result 15(for above input).
Logic To Add Two Numbers without using Plus Operator
If num = 7; result = ~num; will give 1s complement of 7, that is -8. result = -(~num); will give 8. result = -(~num)-1; will give 7.
Now using this logic, we ask the user to enter 2 numbers. If user enters a = 8 and b = 7. We use the below logic to perform addition, without using plus symbol:
Source Code: C Program To Add Two Numbers without using Plus Operator
#include < stdio.h >
int main()
{
int a, b, add;
printf("Enter 2 numbers for addition\n");
scanf("%d%d", &a, &b);
add = a-~b-1;
printf("Addition of %d and %d is %d\n", a, b, add);
return 0;
}
Output 1: Enter 2 numbers for addition 1 2 Addition of 1 and 2 is 3
Output 2: Enter 2 numbers for addition -1 2 Addition of -1 and 2 is 1
Output 3: Enter 2 numbers for addition -1 -2 Addition of -1 and -2 is -3
Output 4: Enter 2 numbers for addition -5 5 Addition of -5 and 5 is 0
Output 5: Enter 2 numbers for addition 5 -10 Addition of 5 and -10 is -5
Video Tutorial: C Program To Add Two Numbers without using Plus Operator
Note: These kind of tricky C programming questions can be asked in your viva or company interviews or competitive exams or even in your academic exams. So be prepared.
Lets write a C program to perform subtraction of 2 numbers without using minus symbol or the subtraction operation(-).
In this video tutorial we are using ~(tilde symbol) bitwise complement operator to perform the operation to get to the anticipated result.
Example: If user enters 2 numbers. a = 15 and b = 5. To subtract 10 from 5 we use result = a – b. And the result will be 10. But in this C program we are not using minus(-) operation, but still we must get the same result 10(for above input).
Logic To Subtract Two Numbers without using Minus Operator
If num = 10; result = ~num; will give 1s complement of 10, that is -11. result = ~num+1; will give 2s complement of 10, that is -10.
Now using this logic, we ask the user to enter 2 numbers. If user enters a = 10 and b = 5. We use the below logic to perform subtraction, but without using minus symbol:
a = 10, b = 5; sub = a + ~b + 1; sub = 10 + (-6) + 1; sub = 10 – 6 + 1; sub = 10 – 5; sub = 5;
This would give proper required result.
Source Code: C Program To Subtract Two Numbers without using Minus Operator
#include < stdio.h >
int main()
{
int a, b, sub;
printf("Enter 2 numbers\n");
scanf("%d%d", &a, &b);
sub = a+~b+1;
printf("Subtraction of %d and %d is %d\n", a, b, sub);
return 0;
}
Output 1: Enter 2 numbers 4 10 Subtraction of 4 and 10 is -6
Output 2: Enter 2 numbers 5 10 Subtraction of 5 and 10 is -5
Output 3: Enter 2 numbers 10 5 Subtraction of 10 and 5 is 5
Output 4: Enter 2 numbers 5 5 Subtraction of 5 and 5 is 0
Output 5: Enter 2 numbers 5 -5 Subtraction of 5 and -5 is 10
Video Tutorial: C Program To Subtract Two Numbers without using Minus Operator
Note: These kind of tricky C programming questions can be asked in your viva or company interviews or competitive exams or even in your academic exams. So be prepared.
We ask the user to input the number of rows of Floyd’s Triangle, we store it inside variable num. We assign 1 to variable nn(natural number). Variable num has the number of natural numbers to be printed in particular row. The inner while loop prints the natural numbers upto num.
For Example, if user enters num = 5, the following Triangle will be printed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.
Source Code: C Program To Print Floyd’s Triangle In Reverse
#include < stdio.h >
int main()
{
int num, nn = 1, count;
printf("Enter no of rows of Floyd's Triangle\n");
scanf("%d", &num);
printf("\n");
while(num)
{
count = 1;
while(count <= num)
{
printf("%d ", nn);
nn++;
count++;
}
printf("\n");
num--;
}
return 0;
}
We ask the user to input the number of rows of Floyd’s Triangle, we store it inside variable num. We assign 1 to variables count and count1. Variable count is used to print the natural numbers for Floyd’s Triangle. Variable count1 is used to keep track of outer while loop. Variable count2 is used to keep track of inner while loop.
For Example, if user enters num = 5, the following Triangle will be printed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.
Important Note: Also note that first row has 1 number. Second row has 2 numbers. Third row has 3 numbers and so on. So row number and total numbers in that particular row are always equal in any Floyd’s Triangle.
Outer While loop In our C program, row number and the total numbers to be printed for that row is present inside variable count1.
Inner While loop Inner while loop prints natural numbers in each row. Variable count2 is assigned to 1 for each iteration of outer while loop, so that the numbers gets printed from the first position in any selected row.
Source Code: C Program To Print Floyd’s Triangle
#include<stdio.h>
int main()
{
int num, count = 1, count1 = 1, count2;
printf("Enter no of rows for Floyd's Triangle\n");
scanf("%d", &num);
printf("\n");
while(count1 <= num)
{
count2 = 1;
while(count2 <= count1)
{
printf("%d ", count);
count++;
count2++;
}
count1++;
printf("\n");
}
return 0;
}
Output 1: Enter no of rows for Floyd’s Triangle 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Output 2: Enter no of rows for Floyd’s Triangle 14