Skip to main content

C Basics Programs with with different data types and IF Conditional statement in C #DOTC_Mdb

 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 you are familiar with Calculation Program by using C Program and try to make one program where user can input anything for multiplication , addition , subtraction , division etc and give the correct output just like small calculator . Write any simple program for Calculation , Simple interest , Printing any value etc.

 

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. < means less operator in c and > means greater operator in c

Below is the source code and output of above Program in code block.





 Let’s take one another example of IF statement where user can input any value and we can use multiple if statement to check greater, less or equals to value which user has given :

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.

  



Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Basic SQL Commands in Oracle Database #Diksha Oracle Training Centre

  Basic SQL Commands in Oracle Database.   Hello Everyone, In my previous blog I discussed about SQL and Classification of SQL . Today I will discuss about SQL basic commands which widely used in RDBMS. The Topics of SQL command which I am going to cover in this blog are mainly divided into four Categories: ·         DDL:   DDL consists of commands which are used to define and design the database. ·         DML:   DML consists of commands which are mainly used to manipulate the data in the database. ·        DCL: DCL Consists of commands which deal with the user permissions/access and controls in the database. ·       TCL:   TCL Consist of commands which deal with the transaction in the database. If you want to explore theory part please follow my below blog as in today blog I will discuss about how to use the commands. h...

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...