Learn C – A Complete Reference from Coder in Me

Hi if you are computer science student you should Learn C at first.

Learn C – The first program for Beginner :

Here we write first code in C language.
for any program, we need some library so we use #include
this library is used for all functions like scanf() and printf().
main() is the function which must be in the every program.
so here we begin with c.

#include <stdio.h>   

int main(){
   // this is a comment , in the next line we print  the output
   printf("Hi this is Coder in Me.");
}

The out put will be

Hi this is Coder in Me.

Datatype

Basic data types are  — int  %d, float %f, char %c

Data Type  code format   size(bytes)   Range                           
char          %c           1          -128 to 127  (unsigned char will be 0 to 255)
int           %d           4          -2,147,483,648 to 2,147,483,647 
long int      %ld          4          -2,147,483,648 to 2,147,483,647    
float         %f           4                                                  
double        %lf          8

Other data type are also available like long int, long double, short int etc.

Input and Output

printf("Hi this is Coder in Me.");
int a=3;
char c='ok';
printf("the variable a and c will be printed like this a=%d   & a=%c",a,c);


// points to remeber

here you can see a=%d  so C compiler will search integer variable so we have given “a” after ‘,’. and after that c will be printed because %c will find c variable as character type.

Learn C

#include <stdio.h>

int main(){

 int a,b;
 char c;
 printf("Please enter the value of integer a and character c with space or enter \n");
 scanf("%d %c",&a,&c);
 b=a%2;
 printf("the value of a b and c is %d \n %d \n %c",a,b,c);
}

In above example, you can see if we want to take input we use scanf and within  ” ” we specify the data type and after that ‘,’  we use & for assigning the input value and use address for that variable. so &a will take the input of a.

Begin with C coderinme

Learn C – Problem 1

So, we will try one simple problem of mathematics in which we just have to write code for Area and Circumference of Circle. Click the below link to find out the problem and solution.

Simple Program to Calculate Area and Circumference of Circle in C

Here, we are giving you an example to calculate Total salary on the basis of basic, da, ha.
In this problem, we will use simple arithmetic operation.
A Problem for simple arithmetic calculation on Basic Salary
Next Problem is to convert time duration in minutes second and hour format.
Program to Conversion into minutes seconds and hour

Now I thought you can understand about basic operation input and output used in C.Ok, then why don’t you do this question.

Calculate amount, If principal and time are given with Compound interest

If Else Condition

We often realize our life gets stuck by some condition, some pros, and cons, If and else and either or neither nor.

In programming we use If else statement for this type of situation, or we use switch case syntax.

Syntax

if( condition){
	logic or statement
}
if( condition){
	logic or statement
        second line
}
else if(new condition)
      1 line statement
if( condition){
	logic or statement
}
else
      1 line statement

For example, we want to check a number is even or odd. What will we do?

int x = 12;
if( x % 2 == 0 )
	printf("%d is even Number",x);
else
	printf("%d is od Number",x);

Let’s practice the first question arises on CoderInMe by a site lover. Suppose that you have an array of size n, Find Difference of sum of Odd and Even it means you need to find the odd numbers and add them, find even numbers and add them finally print the absolute value of subtraction between those two sums of odd and even values.

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

int main() {
	int n,even=0,odd=0,i;
	scanf("%d",&n);
	//enter size of array
	int *a = malloc(n * sizeof(int)); //dynamic array a of size n
	for(i=0; i<n; i++){
	// enter all elemnt
		scanf("%d",&a[i]);
	// check even or odd and adding
	if(a[i]%2==0)
		even+=a[i];
	else
		odd+=a[i];
	}
	printf("%d",abs(odd-even));

	return 0;
}

In the above problem, we define the variable n, i, odd and even. Then we took input from the user.y Because the array is of fix size, so we used a dynamic array.Later we will discuss what is the advantage of a dynamic array. the syntax is same

int *arr = malloc(n * sizeof(int)); //malloc is memory allocation for n element and size of int, because datatype is int.

this means for 4 int element. memory alloc 4*4=16 bit

After that, we used for loop, because we took the array element input. Then we use if condition as we wanna check even numbers and then want to add, and same for the odd numbers also. Finally, we subtract odd sum and even sum and print the absolute value. So for abs() method, we use math.h and for malloc we use  stdlib.h

If you want to practice more problems on If else if then you should try these links

  1. Print the type of triangle if 3 sides are given
  2. Calculate net electric charge if charges /unit is different
  3. Tax Problem

Switch Case(Learn C)

The condition of a Switch case in C is a value.
The case says that if it has the value of whatever is after that case then do whatever follows the colon.

switch (c){
	case 1:printf("ok");
		break;
	case 2:printf("not ok");
		break;
	default : printf("it is ok");
	}

In the above example if c is  an integer and we want to print different value for different integer value, then we use case switch here case 1 means if the value of c is 1 then output will be ok, for c=2 output will be not ok, for other it will be ‘it is ok’ .

Let’s practice a simple and easy program in which we will choose a number from 1 to 7. and it will print Mon-Sun accordingly.For that, we use a switch statement in below link.

Program to Print day according to 1-7 using Switch

Menu driven program using switch

Iteration for loop, while loop, do while(Learn C)

Often we want to do/print the same thing again and again so that we use for loop or while in our programming. for loop is very simple in c;
suppose i is a variable. i.e; int i;
then we will write

int i;
for(i=0 ; i<10; i++)  
	printf("%d", i);

means sample code is    for( start point ; end point ; increment)
above starting is i=0; to 9 and it will run 10 times and print from 0—9 because after 1 iteration i will be 1 and then 2 and so on.

If we want to write the same problem in while we will write like this

int i;
while(i<10)  {
	printf("%d", i);
	i++;
	}

So, If you want to practice about for loop/while loop then please practice these problems.

In the following example, we have to find the sum of numbers that are lying between that range like 12 to 32, then we will add from 12…..32.

Sum of numbers between the range

In this example, we have to use compound interest formula, A=P(1+R/100)<sup>n</sup>. where A is the amount, P Principal, R rate, n Time. click the link for the problem and solution

Compound interest using while loop.

You are a programmer then must know that 3=11, and 143=10001111 so we have to write a program for that conversion.Beginning with the process, at each step, we check the divisibility of the given decimal number by two (2) and keep on noting the remainder at every single step sideways. Finally, to get the equivalent binary form, we assemble all the remainders noted earlier, by writing the remainders in the reverse order. click the link for the problem and solution

Convert Decimal to binary

If we find the sum of cubes of each digit in the number and the sum is same as the given number, then the number is an Armstrong number. Code will be in the below link

Check it’s Armstrong number or not

More Practice Problems:

Equivalent Resistance for series and parallel

Find all the numbers that are divisible between the range

As you know CoderInMe is the platform where you can learn the basic coding and programming like Learn C,  C++, Java, Python, PHP, Shell, MySQL.solution for Hackerrank and Hackerearth problems. competitive interview and coding experience for Companies like ThoughtWorks, TCS, NEC tech etc.

Function/Method (Learn C)

A small piece of code, that we can use in our problem when ever required, It is just like a formula or a process where we give the input into the function, after the logical and arithmetic operation, it will return or display and output. e.g. if the formula is c=a+b, so when ever I will put the value of a & b, the c will be the sum of them. similarly

, if function sum is given like

int sum (int a, int b){
	return a+b;
}
or
int sum (int a, int b){
	int c=a+b;
	return c;
}
or
void  sum (int a, int b){
	printf("%d", a+b);
}

so if we will call sum(2,3) it will either print 5 or return 5. so now you can see function is of 2 types , 1 it will return any value, 2 it will print a value inside the function, Syntax will be like

void fun1(datatype var1,datatype var2,datatype var3,... )
{
	// statement
	// output printf
}

In the above , we don’t want to return any value from function so we used void before function name, then after the bracket we defined the inputs and datatype as the variables.

datatype fun1(datatype var1,datatype var2,datatype var3,... )
{
	// statement
	// operation
	return value;
}

If we want to return a value then we use above code sample like if we want to return int we will replace datatype as int, or float etc. in the body of function after process we will return the value as same data type.

Let’s see example where you can see.

suppose we want to find LCM HCF of Two Numbers so the program will be like this

#include <stdio.h>
int LCMFun(int a, int b){
int  max, HCF,LCM;
	max = ( a < b ) ? a : b;
while(1)
{
	if( max % a == 0 && max % b == 0 )
	{
	LCM = max;
	
	HCF = ( a * b ) / LCM;
	printf("HCF: %d",HCF);
	break;
	}
	max++;
}
	retunr LCM;
}
void lcmHCf(int a, int b){
	int  max, HCF,LCM;
	max = ( a < b ) ? a : b;
while(1)
 {
	if( max % a == 0 && max % b == 0 )
	{
	LCM = max;
	printf("LCM: %d\n",LCM);
	HCF = ( a * b ) / LCM;
	printf("HCF: %d",HCF);
	break;
	}
	max++;
   }
}
int main()
{
int a,b;
printf("Enter two positive integers: ");
scanf("%d %d", &a,&b);
int lcmvalue= LCMFun(a,b);
//return lcm and print hcf above function
lcmHCf(a,b);
// no return , printf in functins
return 0;
}

 

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.