Sunday, May 17, 2015

Basic Programs in C Language

C is a popular programming language from modern languages and
it seed of modern programming languages(like c++,java,etc).
C language is developed by Dennis Ritchie at AT&T Bell Labs.
This language is used to write the chip level programs and drivers code.C is a basic language to improve programming skills.Because to improve our coding skills, we should learn C language from fundamental(Data types,loops,etc) to high level(Read file from local drive).Means all modern languages have their own predefined methods.For example if we want to sort the array elements in java we have sort method but C language doesn't have that type of functions .So we want improve our coding skills we learn C language from basic level. That's why,I am giving basic fundamental programs to improve our programming skills.Now a days, in every interviewer will ask at least two or three questions from C language irrespective of you attending programming language interview(Java,.Net,Ruby,etc).Let's us start with basic programming questions.



When i am learning C language,my first executed program is Hello World Program.Who will learn this language first time,they run Hello World program before running any other program.Now i will explain our first program with step by step.

1)First line of program has header files.Header files are contains some basic predefined function to execute our program.



#include <stdio.h>
means here we including stdio.h header file to compiler for compile and run program.This file has standard input ,output functions.For example input function is scanf,this is used to read characters from client. And output function is to print on the command line.
2)
void main()
{
   //some code
}
Our actual program will start with main() function only.Program execution start from main().void is a predefined keyword which returns nothing."{","}"are indicates where program starting ,where program ending respectively.

3) We write one predefined function in the place of some code in step 2.That is printf("Hello World");
printf() is a standard output function in C language.It is available in stdio.h,that's why we included stdio.h in program.

Let's see below program

1)#include<stdio.h>

void main(){
printf("Hello World");// this function display the "Hello World" on console.
}

Output:
Hello World

2)Write a  C program for add two integer numbers?
/**
 * Adding two integer numbers
* @ author rrvtechdiamond
*/
#include<stdio.h>
void main()
{
  //here declare 'a'  variable with  "int"  data type and assign value 6 to variable 'a'
   int a = 6;
   int b = 5;//here declare 'b' variable with  "int"  data type and assign value 5 to variable 'b'
   int c = 0;//here declare 'c' variable with  "int"  data type and assign zero to 'c' variable 
   c = a+b;//assign addition of two numbers to ' c' variable
   printf("The addition of two integers is:%d",c);//  print the result on console
   getch();


Output:
The addition of two integers is:11

3)Find the difference of two numbers using C program?

#include<stdio.h>

void main(){
     int a = 10;
     int b = 5;
     int c = a-b;
     printf("The Subtraction of two numbers is:  %d",c);
     getch();
}

Output:
The Subtraction of two numbers is: 5

4)Find the biggest number among 3 number using  C program?

#include<stdio.h>

void main()
{
   int a = 1,b = 10, c =  6;  // here declare and assign three variables with different values
 if(a > b && a > c){ //Here && operator is used like a logical AND means if both conditions are                                      //true then only this block  will execute 
     printf("The biggest number is: %d",a);
 }else if(b > a && b > c){
    printf("The biggest number is: %d",b);
}else //here  no need to check because above two conditions are not execute then this block will               //execute then which value is contains c that is bigger
    printf("The biggest number is: %d",c);
}

}

Output :
The biggest number is: 10

Let's see here up to now  declaring values directly in program.But up coming programs are read variable values from console.Then see below programs.

5)How to read values from console using C program?
 #include<stdio.h>
void main(){
int  a;//here declare two variables a and b
int b;
printf("Enter  a and b values");
scanf("%d",&a); //scanf function is predefined library function is used to read data from console /                            //and printf() function is also predefined function is used to print data on console
scanf("%d",&b);//here %d is used to represent integer values and generally we declare variable //like a or b or c then &a means address of variable.When we enter value in console that value is //stored into this address place .

printf("You entered values are %d,%d",a,b);

}

Output:

Enter a and b values 10
15

You entered values are 10,15

6)Find the given number is even or not using ternary operator?

First we should know what is ternary operator.Means we use three operands.Syntax of this one is

Syntax:
(condition) ? if true this : if false this;

Ex:

int a = 10;
int b = 3;
(a>b)?a :b;
Output:
10

Let see the program


#include<stdio.h>

void main(){
int a;
printf("Please enter number:");
scanf("%d",&a);
(a%2 == 0)?printf("The given number %d is even number ",a):printf("The given number  %d is not even number",a);

getch();

}

Output:

1)Please enter number:
5
The given number 5 is not even number

2)
Please enter number

10
The given number 10 is even number


See This links also



Tribonacci Series
Floyds-Triangle   
Octal to Decimal