C Basics Programs with with different data types and IF Conditional statement in C .
Hi Everyone,
In previous blog we have discussed about basics of C and Syntax uses in some basic programs and I hope you have very clear about how to display value by using C Program and also how to take input from user to give output against some basic calculation Program . As we discussed only about int data type in previous blog . Today we will write some program by using different data type. So before starting if you are seeing this blog please go through my previous blog for your reference.
https://dheeraj60.blogspot.com/2020/06/introduction-to-c-program-dotcmdb.html
So Let’s start with some other programs by using different data type but before this I need to share some reserved keywords in C , which have their predefined meaning and cannot be used as a variable name. It is good to avoid using these keywords as variable name.
Below are the 32 Keywords reserved in C
int , long ,return ,register ,short , static , signed , sizeof
auto , break , case , char , continue , const , do , default
struct , switch,typedef,union,unsigned, void ,volatile, while
double , else , enum,extern, if , goto , for , float
These are the reserved Keywords in C , Some of them are familiar with you as we used in previous blog , I will explain some keywords today according to Program and later i will cover all the keywords. I provided keywords because when we write any program we should not put any variable name which matches this keyword.
So let’s start with one Basic C by using float and int data types . Int we already discussed in previous blog , float basically used to take decimal values so in this program we use rate as float . See the below program to find the Simple interest with both hard coded as well as input given by user.
Hard Coded program for Simple Interest by assigned value in variable is as below.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int p,t;
float r,si;
p=100000;
r=8.5;
t=3;
si = p*r*t/100;
printf("The simple intrest is !\n %f",si);
return 0;
}
In above program we are familiar about all the syntax which we discussed in my previous blog apart from float data type and %f :
Here float use to except decimal value and for printing decimal value we need to use %f and for integer we use %d.
We simply use simple interest formula that is si=p*r*t/100 ;
Below is the code source and output in code block.
Same program by taking input from user is as below where user can give any value for principal , rate and time and they will see the output of Simple Interest.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("please enter Principal , rate and time to get the simple intrest \n");
int p,t;
float r,si;
scanf("%d",&p);
scanf("%f",&r);
scanf("%d",&t);
si = p*r*t/100;
printf("The simple intrest is !\n %f",si);
return 0;
}
Output of the above code in code block compiler is as below :
Now Let’s Move to IF conditional Statement in C program with basic example.
Types of Conditional Statement in C
IF
IF –ELSE statement
Nested IF _ELSE Statement
ELSE –IF Statement
Today we will discuss about IF Statement only and other condition statement we will cover in next blog.
IF Statement is used when we need to execute a block of statements only if a given condition is true. If Condition is true then only code is executed.
Syntax of if statement:
The statements inside the body of if only execute if the given condition is true. If the condition returns false then the statements inside if are skipped.
if (condition)
{
//Block of C Code
//Code will only execute if the condition is true
}
Let’s write one simple code to understand how if statement works we take 2 variable with hard coded value and check greater or less number. Below is the code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a , b;
a=20;
b=40;
if (a<b)
printf(" a is less than b !\n");
return 0;
}
As we assigned the value of a and b so make this statement true we put condition a less than b. so it will give the output a is less than b.
Below is the source code and output of above Program in code block.
Below is the Program.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
printf("Enter the value of a \n");
scanf("%d",&a);
printf("Enter the value of b \n");
scanf("%d",&b);
if (a>b)
{
printf(" a is greater than b !\n");
}
if (b>a)
{
printf("b is greater than a ! \n");
}
if (a==b)
{
printf(" a is equal to b! \n");
}
return 0;
}
Below is the source code and output of above Program in code block.
Here we simply use all the operator greater > less < and equal == to check the condition if any condition matches given by the user it will give you the correct output . This is Simple IF statement where we can check multiple condition .
This is all about basics of some C program with decimal values by using data type float and simple IF statement to check the condition. Please try to write some other code related to this topic as in next blog i will come up with different conditional statements in C and idea about Logical Operator in C.
Please go through this blog and let me know if you have any doubts.
Thanks.
Well explained!
ReplyDelete