Some Programs Using Different Types Of Data And Variables In C

Today in our C programming tutorial for beginners we are going to learn about how to take input from users and print output in a c program. Also, today I will show you some programs using different types of data and variables. If you read the last article and have been confused about data types and variables. Just read this article carefully. Hopefully, your all confusion will be cleared.

 

Look at the following code:

#include <stdio.h>
int main ()
{
int a,b,sum,multi;
printf(“Give the values of a and b:n”);
scanf(“%d %d”,&a,&b);//(1)
sum=a+b;
multi=a*b;
printf(“Summation of %d and %d is %dn”,a,b,sum);//(2)
printf(“Multiplication of %d and %d is %dn”,a,b,multi);//(3)

return 0;
}

We write this program to take two values a and b as input from the user and print the summation and multiplication of a and b as output. In c we will always use scanf() function to take input and printf() function to give output. In our 1st program using C, I have already discussed printf() function also the basic part of a c program. If you haven’t read this article you can read this by clicking here.

However, in the first line, we declared the variables for this program. Actually, we needed 4 variables a and b for taking 2 inputs from the user and sum and multi for calculating summation and multiplication. You can use variables randomly as you want instead of these 4 variables by following the rules for naming variables. As we are dealing with only integer types of data. That so why I declared these 4 variables as integer type data. Then we use a printf() to ask the user to give the values of a and b. Then we used scanf(“%d %d”,&a,&b); at line (1) to take input a and b. %d represents we are taking integer type data as input. As we are taking 2 integer types of data we used 2 %d. Whatever we are taking as input should be between ” and “. Coma(,) after ” means our input-taking process was completed now we are going to store them in memory. &a means we are storing 1st input in the memory contained by variable a. Actually &a points to the storage of a. Then the &b stores the 2nd input to the memory space contain by b variable. You must always be aware in c program to use a semicolon(;) after each statement.

 

 

 

Then we store the summation of a and b on sum and multiplication of a and b on multi. Then print them on the (2) and (3) lines. Here n used to print a newline. For example for this code if you take 4 and 8 as input our output should be like that:

 

 

 

Here we can see Summation and multiplication are shown in 2 different lines this happened because of using n in the 1st printf statement. Also, there was a blank line before the process returned line that is because of the n used in the second printf statement. I hope you understand the whole program. Now, your task is to print both lines of summation and multiplication without using n then you will understand the whole thing clearly.

Now I think, you are eligible to write a program to take any input and to give any output by knowing data types and format specifiers for them.

Now, look at the following program:

#include <stdio.h>
int main ()
{
float a,b,d;
scanf(“%f %f”,&a,&b);
d=a/b;
printf(“Division using floating data type d=%fn”,d);

return 0;
}

 

In this program, we need to take 2 floating types of data and print their division. As division can be a fraction that so why we have used floating type of data. As format specifier for float type of data is %f that so why we used %f to take input and to give output. Suppose we are taking 17 and 8 as input so the output will be 2.125000.

As you see there was 6 digit after the. this is specified for the float data type. But we can print less or higher than 6 digits in the fractional part. Look at the following code:

#include <stdio.h>
int main ()
{
float a,b,d;
scanf(“%f %f”,&a,&b);
d=a/b;
printf(“Division without specified d=%fn”,d);
printf(“Division using 3 digit after integer %0.3fn”,d);

return 0;
}
Now the output will be like this:

 

 

 

You can see when we used %0.3f in the 2nd printf function then it gives the output 2.125 means 3 digits in the fractional part. So, if you use %0.kf then it will give k digit after. in the fractional part as output.

 

Now, look at the last program for today:

#include <stdio.h>
int main ()
{
char ch,p;
scanf(“%c %c”,&ch,&p);
printf(“ch=%c and p=%cn”,ch,p);
printf(“Summation of ascii value of %c and %c is %dn”,ch,p,ch+p);//advance level.

return 0;
}

 

 

This is an example of character type data. We used %c to take input and to print the output as they are character type data and %c is the format specifier for character type of data. Here you have to take 2 characters as input. Then it will store the 1st input on ch and the second input on p. Then it will print ch and p by the 1st printf.

But in the second printf here you can see we used a %d at the end. This is because we are printing the summation of ch and p. As we can’t make a summation between two characters but we can sum their ASCII value. As ASCII values of characters are numerals that so why we used a %d to give the summation of the ASCII value of ch and p means ch+p as output.

 

 

 

 

First, run the program on your computer then take two inputs like t and Z. Then see the output. You will understand the whole thing.

That’s it for today. Now I am giving you a task. Write a program that will take 2 integer data and 2 character data and 2 float types of data as input and give 3 outputs in 3 lines. 1st line to print the difference between the integers. 2nd line is to print the difference between the characters. And 3rd line is to print the difference between the floating type of data and also print 8 digits in the fractional part. You must use 1 scanf function to take the 6 inputs and use 3 lines to give the output. Then comment below your code or output by giving the following input to your code.

Input:  99999 1234 T 8 23.786987 11.12

What is your output for these inputs? Comments below.

 

 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *