Wednesday 21 November 2012

c language

c programming examples
C programming examples :- These
programs illustrate various programming
elements, concepts such as using
operators, loops, functions, single and
double dimensional arrays, performing
operations on strings, files, pointers etc.
Browse the code from simple c program
to complicated ones you are looking for,
every one of them is provided with
output. C program download with
executable files, so that you save on your
computer and run programs without
compiling the source code. All programs
are made using c programming
language and Codeblocks, most of these
will work under Dev c++ compiler also.
Download software you need to develop
codes. The first program prints "Hello
World" on screen.
C programming codes
Hello world
Print Integer
Addition
Odd or Even
Add, subtract, multiply and divide
Check vowel
Leap year
Add digits
Factorial
HCF and LCM
Decimal to binary conversion
ncR and nPr
Add n numbers
Swapping
Reverse number
Palindrome number
Print Pattern
Diamond
Prime numbers
Find armstrong number
Generate armstrong number
Fibonacci series
Print floyd's triangle
Print pascal triangle
Addition using pointers
Maximum element in array
Minimum element in array
Linear search
Binary search
Reverse array
Insert element in array
Delete element from array
Merge arrays
Bubble sort
Insertion sort
Selection sort
Add matrices
Subtract matrices
Transpose matrix
Multiply two matrices
Print string
String length
Compare strings
Copy string
Concatenate strings
Reverse string
Find palindrome
Delete vowels
C substring
Sort a string
Remove spaces
Change case
Swap strings
Character's frequency
Anagrams
Read file
Copy files
Merge two files
List files in a directory
Delete file
Random numbers
Add complex numbers
Print date
Get IP address
Shutdown computer
c program examples
Example 1 - C hello world program
/* A very simple c program printing a
string on screen*/
#include
main()
{
printf("Hello World\n");
return 0;
}
Output of above program:
"Hello World"
Example 2 - c program to take input from
user using scanf
#include
main()
{
int number;
printf("Enter an integer\n");
scanf("%d",&number);
printf("Integer entered by you is %d
\n", number);
return 0;
}
Output:
Enter a number
5
Number entered by you is 5
Example 3 - using if else control
instructions
#include
main()
{
int x = 1;
if ( x == 1 )
printf("x is equal to one.\n");
else
printf("For comparison use == as = is
the assignment operator.\n");
return 0;
}
Output:
x is equal to one.
Example 4 - loop example
#include
main()
{
int value = 1;
while(value<=3) { printf("Value is %d\n", value); value++; } return 0; } Output: Value is 1 Value is 2 Value is 3 Example 5 - c program for prime number #include
main()
{
int n, c;
printf("Enter a number\n");
scanf("%d", &n);
if ( n == 2 )
printf("Prime number.\n");
else
{
for ( c = 2 ; c <= n - 1 ; c++ ) { if ( n % c == 0 ) break; } if ( c != n ) printf("Not prime.\n"); else printf("Prime number.\n"); } return 0; } Example 6 - command line arguments #include
main(int argc, char *argv[])
{
int c;
printf("Number of command line
arguments passed: %d\n", argc);
for ( c = 0 ; c < argc ; c++) printf("%d. Command line argument passed is %s\n", c+1, argv[c]); return 0; } Above c program prints the number and all arguments which are passed to it. Example 7 - Array program #include
main()
{
int array[100], n, c;
printf("Enter the number of elements
in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Array elements entered by you are:\n"); for ( c = 0 ; c < n ; c++ ) printf("array[%d] = %d\n", c, array[c] ); return 0; } Example 8 - function program #include
void my_function();
main()
{
printf("Main function.\n");
my_function();
printf("Back in function main.\n");
return 0;
}
void my_function()
{
printf("Welcome to my function. Feel at
home.\n");
}
Example 9 - Using comments in a program
#include
main()
{
// Single line comment in c source code
printf("Writing comments is very useful.
\n");
/*
* Multi line comment syntax
* Comments help us to understand
code later easily.
* Will you write comments while
developing programs ?
*/
printf("Good luck c programmer.\n");
return 0;
}
Example 10 - using structures in c
programming
#include
struct programming
{
float constant;
char *pointer;
};
main()
{
struct programming variable;
char string[] = "Programming in
Software Development.";
variable.constant = 1.23;
variable.pointer = string;
printf("%f\n", variable.constant);
printf("%s\n", variable.pointer);
return 0;
}

No comments:

Post a Comment