Tuesday, February 25, 2014

Computer Programming C language

Introduction of C



Introduction of C-Programming


C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Eqquipment Corporation PDP-11 computer in 1972.
The Unix operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons.
Easy to learn
Structured language
It produces efficient programs.
It can handle low-level activities.
It can be compiled on a variety of computers.


Facts about C


C was invented to write an operating system called UNIX.
C is a successor of B language which was introduced around 1970
The language was formalized in 1988 by the American National Standard Institue (ANSI).
By 1973 UNIX OS almost totally written in C.
Today C is the most widely used System Programming Language.
Most of the state of the art software have been implemented using C


Why to use C ?


C was initially used for system development work, in particular the programs that make-up the operating system. C was adoped as a system development language because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:
Operating Systems
Language Compilers
Assemblers
Text Editors
Print Spoolers
Network Drivers
Modern Programs
Data Bases
Language Interpreters
Utilities


C Program File

All the C programs are writen into text files with extension ".c" for example hello.c. You can use "vi" editor to write your C program into a file.
This tutorial assumes that you know how to edit a text file and how to write programming insturctions inside a program file.


C Compilers

When you write any program in C language then to run that program you need to compile that program using a C Compiler which converts your program into a language understandable by a computer. This is called machine language (ie. binary format). So before proceeding, make sure you have C Compiler available at your computer. It comes alongwith all flavors of Unix and Linux.
If you are working over Unix or Linux then you can type gcc -v or cc -v and check the result. You can ask your system administrator or you can take help from anyone to identify an available C Compiler at your computer.
If you don't have C compiler installed at your computer then you can use below given link to download a GNU C Compiler and use it.

Factorial program in c





Factorial program in c using for loop






#include <stdio.h>
#include<conio.h>
void main()
{
int c, n, fact = 1;
 
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);
 
for (c = 1; c <= n; c++)
fact = fact * c;
 
printf("Factorial of %d = %d\n", n, fact);
 
getch();
}
---------------------------------------------------------------------------------
                                          OR

Factorial program in c using function

#include <stdio.h>
#inclued<conio.h>
long factorial(int);

void main()
{
int number;
long fact = 1;

printf("Enter a number to calculate it's factorial\n");
scanf("%d", &number);

printf("%d! = %ld\n", number, factorial(number));

getch();
}

long factorial(int n)
{
int c;
long result = 1;

for (c = 1; c <= n; c++)
result = result * c;

return result;
}
---------------------------------------------------------------------------------
Factorial program in c using recursion
#include<stdio.h>
#include<conio.h>
long factorial(int);
void main()
{
  int n;
  long f;
  printf("Enter an integer to find factorial\n");
  scanf("%d", &n); 
  if (n < 0)
    printf("Negative integers are not allowed.\n");
  else
  {
    f = factorial(n);
    printf("%d! = %ld\n", n, f);
  }
getch();
long factorial(int n)
{
  if (n == 0)
    return 1;
  else
    return(n * factorial(n-1));
}

Tuesday, February 18, 2014

programs in c

Print "Hello" in C Program

# include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf ("Hello.....");
getch();
}

Result: Hello......


Add Two Numbers In C:

# include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
int first_num,second_num,sum;
first_num=10;
second_num=20;
sum=first_num+second_num;
printf("%d The Sum is=", sum);

getch();
}

Result:The Sum is=30


OR
# include <stdio.h>
#include<conio.h>
void main()
{
clrscr();

int first_num,second_num,sum;
printf("Enter the First Number= ");
scanf("%d",&first_num);
printf(Enter the Second Number=");
scanf("%d",&second_num);
sum=first_num+second_num;
printf("%d The Sum is=", sum);

getch();
}

Result:
Enter the First Number=30
Enter the Second Number=20
The Sum is=50