C (Chapter 1) : Starting Programming
For any type of consultation, query or doubt. You may contact to the following: (+91) 9804 436 193 debabrataguha20@gmail.com
and join the group https://www.facebook.com/groups/331231161093613/
You have only 5 things to learn in this chapters. After that, you will be able to solve all the problems where we do not need for loop or if statement.
1.1 First Thing to learn:
Let’s start with some problems in the real life world. Like if I ask you what is your height and suppose you give me the answer like 6 feet. Now If I ask you that I need the length in cm, because I have to fill an army examination form where it is mandatory to give your height in cm . So how can you give me the length in cm?
Obvious answer is to search it in google. There are multiple small apps which can convert a length in foot to centimeter equivalent. And the answer would be 182.88 cm. You just need to write “foot to cm converter” in google search engine.
Now write 6 in 1st box and in another box the centimeter equivalent will automatically come.
So my question is now , how does google transform the length from foot to cm. Or what about those small apps which can convert temperature from Centigrade to Fahrenheit and lots more. So We will start learning how simply we can write this logic by our own.
In C, memorize 1st few lines. For next few chapters we would copy these lines without knowing the meaning. Later we would learn the meaning of following lines. From the following 2 options anyone you can choose to write
#include < stdio.h> #include < stdio.h>
void main() int main()
{ {
} return 1;
}
If you are using turbo c , then make a habit to use the following:
#include < stdio.h>
#include<conio.h>
void main()
{
clrscr();
getch();
}
Remember ,
· We will write the required code inside of main function. We need to focus on that only for the time being.
· We will use turbo C format code now on. If you are using some other version of compiler, just remove 3 following lines:
#include<conio.h>
clrscr();
getch();
· Lines inside main() are terminated by a semicolon ( ; )
Now If I want to add 2 numbers and print the sum, I will write the following code in C:
#include < stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
a=10;
b=5;
c=a+b;
printf (“%d”, c );
getch();
}
Here we are working with 3 variables or boxes. 1 variable or box can hold any value inside. For example, box “a” is holding 10 , variable “b” is holding 5 and initially the variable “c” is not having any value. We would calculate the value of “c” at the runtime.
while running the program, C is putting the sum ( means 15 ) into the variable “c”.
Another thing, we are mentioning “ int a,b,c “, just because of initially we will handle integer values only like 1,2,3,4,0,-1,-2 etc , not any decimal values like 1.5, 1.3389 etc.
Now, printf() line basically prints something in the screen when you execute/run the program.
If we write , printf(“hi”) it would print “hi” in the screen.
If we write , printf(“Debabrata”) it would print “Debabrata” in the screen.
If we write , printf(“Debabrata Guha”) it would print “Debabrata Guha” in the screen.
If we write , printf(“c”) it would print “c”
If we write , printf(“%d”, c) without quotation , it would print the value of c, that is “15” in above program. %d would be replaced by the variables written outside the quote sequentially.
If we write , printf(“%d , %d and %d”,a,b,c) it would print “10, 5 and 15” in above program. %d is basically a blank space which will be filled up by the value of c.
If we write , printf(“hi %d , hello %d ”, a,b) it would print “hi 10, hello 5”
So If we write , printf(“answer is : %d ”, c) it would print “answer is : 15”
Now moving into our 2nd program,
If I ask you to take 2 numbers as input and show the addition and subtraction of them.
You can write the following program:
#include < stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,d;
a=10;
b=5;
c=a+b;
printf( “%d”, c);
d=a-b;
printf(“%d”,d);
getch();
}
You can also write it in an alternative way like below:
#include < stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
a=10;
b=5;
c=a+b;
printf( “%d”, c);
c=a-b;
printf(“%d”,c);
getch();
}
Here in the alternative way we are calculating the sum and storing it into “c” and printing it , again we are calculating the difference and storing it inside “c” and printing it back.
Now, suppose we have a temperature in centigrade and we want the equivalent temperature in Fahrenheit. In this case we have to use the old physics formula as below:
c/5=(f-32)/9
or, 9c/5=f-32
or, f=(9c/5)+32
#include < stdio.h>
#include<conio.h>
void main()
{
clrscr();
int c,f;
c=100;
f=(9*c/5)+32;
printf(”%d Centigrade = %d Fahrenheit” , c , f);
getch();
}
So the output would be “100 centigrade= 212 Fahrenheit”
And please be noted, in computer for multiplication you have to use “*” and for division you have to use “/”.
1.2 Second Thing to learn:
Now if we wish to make it more real time like the google app we saw earlier “Foot to cm converter”. We need to allow the user to enter the temperature in centigrade as per their choice and our program will return the temperature in Fahrenheit. Like in google we entered 6 by our choice and in another box it showed 182.88 cm.
So our programming would be:
#include < stdio.h>
#include<conio.h>
void main()
{
clrscr();
int c,f;
printf(“Enter temperature in centigrade”);
scanf(“%d”,&c);
f=(9*c/5)+32;
printf(” %d Centigrade = %d Fahrenheit ” , c, f);
}
Here you will find 1 new lines as underlined above. No need to understand the meaning of that line as of now. For the time being, memorize this line. Only remember, highlighted printf lines is being used to prompt a message to user saying “Enter the value” , or can prompt “Please give a value” etc etc. and scanf() helps to take a number input from keyboard and store it inside the variable c. If you enter “abcd” from keyboard, it will throw an error. Scanf(“%d”) only takes numbers from keyboard. And you need to put an ampersand character “&” before the variable.
Suppose now we will take 2 number input from users and will print the sum and product of them. Then the program will be like following:
#include < stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
printf(“Enter 1st number”);
scanf(“%d”,&a);
printf(“Enter 2nd number”);
scanf(“%d”,&b);
c=a+b;
printf(“the sum is “+c);
c=a*b;
printf(“the product is “+c);
}
}
Here we have written scanf 2 times to take 2 input from keyboard by user.
Similarly if we want to take an input in decimal values , we would use scanf(“%f”). For any character input we will use scanf(“%c”).
scanf(“%d”)can take input of numbers like 0, 1 , 2 , -3 , -9 , 999 etc
scanf(“%f”) can take input of numbers like 0.35 , 1.27 , 2.333333 , -3.12 , -9.0 , 999.9999999 etc
scanf(“%c”) can take input of characters like : “H”, “h” , “5”, “*”, “, “
1.3 Third Thing to learn:
Now we can create any small app and give it to our younger brother or sister. We can create following apps by the programming logic we learnt.
1. Centigrade to Fahrenheit conversion app.
2. Fahrenheit to Centigrade conversion app.
3. Meter to KM, CM conversion and vice versa. We had a chapter in mathematics in our childhood days.
4. Mensuration mathematics like entering 2 sides of rectangle and print area and perimeter.
5. Area , perimeter calculation for a triangle, circle and square.
6. In algebra, you will be given the value of a & b and you need to calculate (a+b)2
These apps will be useful in mathematics or physics homework. They can quickly complete their homework without doing any calculation and complete their homework in rapid speed like in 5-10 mins instead of spending the full evening. They just need to execute the program written by you and input the values. For giving this idea, I think mathematics teacher will not be happy at all.
So now for the app, where the user will give radius of a circle and will expect the area and perimeter of the same, he/she will get the result always in integer. Because till now, we are dealing with only integers. But in real scenario, area and perimeter of circle almost always comes in decimal format, as we are multiplying the radius with PI (that is 3.14).
So here we will declare the variable with “float” datatype instead of “int”. and all the %d would be replaced with %f. Everything else will be same. Please follow the below program.
#include < stdio.h>
#include<conio.h>
void main()
{
clrscr();
float a,b,c;
Printf(“Enter the radius”);
Scanf(“%f”,&a);
b=2 * 3.14 * a;
c=3.14 * r * r;
printf(“the perimeter is %f “,b);
printf(“the area is %f “,c);
}
}
Now take another example:
int a=9;
int b=2;
int c=a/b;
printf(c);
Above program will print the result as 4. But if we declare the variable as float , it would print the correct answer as 4.5 . Integer variable never shows the decimal part, it discards the point values.
1.4 Fourth Thing to learn:
Suppose , it is needed to do determine x5 or y4 in any program. The way you will follow now is to write x*x*x*x*x and y*y*y*y.
But what will happen in following 2 cases:
1. if I need to determine x34 , will you write x 34 times?
2. If I need to determine xy and x ,y are unknown and will be given by user in run time. In this case you would not be able to write the code even.
So there is a short-cut to this. Simply write,
float z=pow(x,y);
or directly you can write
float z=pow(5,3);
remember, x and y are integer, but the result of xy will be in float and you need to write a new line #include<math.h> to use pow.
Now you must have a confusion why the result is in float datatype. Because in case of 53 , the answer 125 would be surely an integer and it can be easily stored in int z. then why float z?
Ok now answer me, what will happen if user want to see 5½ and 5¾ it means root over and 50.75. pow(5,0.5) and pow(5,0.75) will always come in decimal data type. So to accommodate both integer and decimal type the result is always in float datatype.
We have an alternative for , we can use both pow(5,0.5) & sqrt(5).
So now assume there is program where user needs to enter 2 number as x & y, in return he/she will get
#include < stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int x,y;
float z;
printf(“Enter 1st number”);
scanf(“%d”,&x);;
printf(“Enter 2nd number”);
scanf(“%d”,&y);
z= sqrt( pow(x,2) + pow(y,2) );
printf(“the result is %d “ , z);
}
1.5 Fifth Thing to learn:
To calculate the remainder after mathematical division in C, we use ‘%’. Suppose user will give 2 number as input you need to calculate result, remainder after division, we need to write the program in following way:
#include < stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,d;
printf(“Enter 1st number”);
scanf(“%d”,&a);
printf(“Enter 2nd number”);
scanf(“%d”,&b);
c=a/b ;
d=a%b ;
printf(“the result is %d “, c);
printf(“the remainder is %d “, d);
}
So if user enters 2 numbers as 38 & 5. The output will be as follows:
the result is 7 the remainder is 3
Remember,
Here result came as 7 because integer never can be 7.6
To print the above 2 lines in 2 different line you can use “\n” inside printf command. The code will look like below:
printf(“the result is %d \n“, c);
printf(“the remainder is %d \n“, d);
So we are done with the 5 things, now we can build all the small apps where we do not need the help of IF statement or FOR loop. We will learn IF & FOR in immediately next chapters.
So now we can start solving the exercise. So before starting the exercise, we need to maintain 2 small best practice like below:
1. 1. Try to maintain alignment as much as possible. Alignment means the gap we give before each line.
2. 2. Try to give a meaningful names to variables.
Instead of giving a,b,c,d , try to give names like num1 , num2, div, rem respectively. Later when you will be dealing with a hundred line program, it will be tough to handle and remember those multiple variables. Better to call them by their meaningful names.
That’s all for this chapter. Now we can jump into the exercise.
Comments
Post a Comment