Adding 2 numbers  – real/floating point numbers

0004

Exercise 1 : Modify your code from the previous example to use floating point numbers 5.1 and 6.1

  1. Teacher modifies variable declarations, but leaves prinf to output integer values.
  2. The erroneous result is observed and explained: the binary data stored at the variables are calculated and interpreted to show an integer result.  
  3. Teacher modifies the code of printf() to use %d and now the correct result is shown.


Exercise 2:

Observe what is output when %d is changed to each of the following, one at a time:

%d for int

%f for float

%lf for double

%c for char

%s for string

Video Demonstration

https://youtu.be/Be_QtiweYTY

You can download the files here: https://drive.google.com/file/d/1nVXCOXTJkPYNSs2jDwxGGbz9mvuKooDK/view?usp=sharing

Code

#include <stdio.h>
#include <stdlib.h>

int main()
{
   //declare and identify variables
   float a,b;
   float c;

   //Initialize the variables
   a=5.1;
   b=6.1;

   //Perform processing c <-- a+b
   c=a+b;

   printf("The sum of %.1f and %.1f is %.f \n", a,b,c);


}

© 2021  Vedesh Kungebeharry. All rights reserved. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s