Showing posts with label C language. Show all posts
Showing posts with label C language. Show all posts

Friday, December 4, 2020

Function in C language.

Learn programming for free

Function in C language.


Function is basically a block of statement which is used to perform some specific task.


In c language we have 4 type of function.When we declare a function then it tells the compiler about return type of function,parameter of function and it's function name.


In C language we divide large program into blocks and call them as and when they required.

We will discuss function in depth in this blog.


Types of function.


  1. Function without parameter and without return type.
  2. Function without parameter and with return type.
  3. Function with parameter and without return type.
  4. Function with parameter and with return type.

We will discuss each of them with very basic example:


1.Function without parameter and without return type:


#include<stdio.h>
void sum()
{
int a,b,sum=0;
printf("Enter first number");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
sum = a+b;
printf("Sum of two number is %d",sum);
}
int main()
sum();
return 0; 
}


2.Function without parameter and with return type:


#include<stdio.h>
int sum()
{
int a,b,sum=0;
printf("Enter first number");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
sum = a+b;
return sum;
}
int main()
int x = sum();
printf("Sum of number is: %d",x);
return 0; 
}


3.Function with parameter and without return type:


#include<stdio.h>
void sum(int a,int b)
{
int sum = a+b;
printf("Sum of number is %d",sum);
}
int main()
int a,b;
printf("Enter first number");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
sum(a,b);
return 0; 
}

4.Function with parameter and with return type:

#include<stdio.h>
int sum(int a,int b)
{
int sum = a+b;
return sum;
}
int main()
int a,b,x = 0;
printf("Enter first number");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
x = sum(a,b);
printf("Sum of number is %d",x);
return 0; 
}

Do you want to learn c language in depth then visit:  learn c language in depth.

Thursday, December 3, 2020

Loops in c language.

 Learn programming for free



Loops in c language.


In c language loops is basically of three types.

  1. For loop
  2. while loop
  3. do while loop

We will learn all of them in depth with a question so that it becomes easy to learn and understand.


Question: Write a program in c language which will take input from user and print all number in between 1 to entered number?


1.For loop


#include<stdio.h>
int main()
{
int n,i=1;
printf("Enter number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\n",i);
}
return 0;
}


Syntax of for loop is:

for(initialization;condition;increment/decrement)
{
    
        //logic

}

Explanation:

According to question user will enter a number and we need to print all number between 1 to that number.So we take input from user and inside for loop we initialize our for loop to start from 1 and go till entered number.For each iteration we increment i so that when matched conditions found it terminates for loop and comes out of loop.

And for each iteration we have printed the number.


2.While loop


#include<stdio.h>
int main()
{
int n,i=1;
printf("Enter number");
scanf("%d",&n);
while(i<=n)
{
printf("%d",i);
i++;
}
return 0;
}

While loop is also called as entry controlled loop because it first checks the conditions.If conditions found to be true then only it enters in loop otherwise it skips the loop.

So in above example we have first initialize a variable i = 1.And before entering in while loop we checked the condition since 1<=n ( suppose n=10) then it will enter into loop and print that number.

One important point that we should keep in our mind that inside our while loop we must increment our variable otherwise it will lead to infinite loop condition.


3. do-while loop


#include<stdio.h>
int main()
{
int n,i=1;
printf("Enter number");
scanf("%d",&n);
do
{
printf("%d",i);
i++;
}while(i<=n);
return 0;
}


do-while loop is called as exit-controlled loop because it first executes a statement then check conditions.If our initial condition is found to be false then also it will execute for once.That's why it is known as exit-controlled loop. 

If you want to learn c language in depth then do visit: learn c language in depth.

Getting started with C language.

 Learn programming for free


Getting started with C language.


In this blog we will learn basic of c language like how to take input,how to print output etc.These are very basic things that every student should know before moving on.

I will explain all these thing with very basic example,which is easy to understand.


#include<stdio.h>

int main()

{

int a,b,sum=0;

printf("Enter first number");

scanf("%d",&a);

printf("Enter second number");

scanf("%d",&b);

sum=a+b;

printf("Sum of two number is %d",sum);

return 0;

}


Explanation:

This is a simple program which takes two number from user and print their sum as a result.


  1. #include<stdio.h>


In programming language we call it as a header file.This header files includes all information required to carry out our particular program.


Like in our case it is included because we need to take input from user and print output.

      

    2. int main()


This is a function don't worry we will learn it in depth here just remember it that it is simply a function.

      

    3. printf();


This statement is used to print  information on screen.Like in our case it is will print Enter first number.

      

    4. scanf();


This statement is used to take input from user.In our case it will take input from user as an integer.

      

    5. sum = a+b;


I don't think this statement needs any explanation.This is a simple arithmetic operation.It will store sum of a and b into sum variable.

      

    6. Finally it will print sum of two number as a result.








       






C language

 Learn programming for free


C language

C is powerful general purpose programming language.It was developed by Dennis Ritchie in 1972.It used for many purpose like developing system software,operating system,compilers etc.It was initially used for developing system works.C language is procedural programming language using top-down approach.


In this blog you will get to learn all topics of  c language in depth.Here we have divided it into topics so that it will be ease for user to learn faster.


Features of c language:

  1. It is general purpose programming language.
  2. Modularity.
  3. Fast and efficient. 
  4. Mother of C++ programming language.
  5. It is mid level programming language.
  6. Rich in libraries and functions.
  7. It is simple,machine independent.
  8. Portable.

Topics: