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...

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you que...

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using s...