Beruflich Dokumente
Kultur Dokumente
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
return 0;
}
Output of program:
return 0;
}
C programming code
#include <stdio.h>
int main()
{
int r;
long number = 0, c, sum = 0, temp;
printf("Enter an integer upto which you want to find
armstrong numbers\n");
scanf("%ld",&number);
printf("Following armstrong numbers are found from 1
to %ld\n",number);
return 0;
}
C Armtrong or not
#include <stdio.h>
int main()
{
int number, sum = 0, temp, remainder;
REVERSE
#include <stdio.h>
int main()
{
int n, reverse = 0;
printf("Enter an integer\n");
scanf("%d",&number);
temp = number;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
printf("Entered number is an armstrong number.\n");
else
printf("Entered number is not an armstrong
number.\n");
return 0;
}
int main()
{
int n, reverse = 0, temp;
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}
return 0;
}
C program to find nCr and nPr: This code calculate nCr
which is n!/(r!*(n-r)!) and nPr = n!/(n-r)!
C program to find nCr using function
#include<stdio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
main()
{
int n, r;
long ncr, npr;
long factorial(int);
return 0;
}
long find_ncr(int n, int r)
{
long result;
return 0;
int main()
{
int number;
long fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &number);
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
return 0;
long factorial(int n)
{
int c;
long result = 1;
result = factorial(n)/factorial(n-r);
return result;
}
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 for loop
return result;
}
Factorial program in c using recursion
#include<stdio.h>
long factorial(int);
int main()
{
int n;
long f;
Loop
#include <stdio.h>
if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
int 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;
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
Output of program:
}
Recursion is a technique in which a function calls itself,
for example in above code factorial function is calling
itself. To solve a problem using recursion you must first
express its solution in recursive form. C program to find
hcf and lcm: The code below finds highest common factor
and least common multiple of two integers. HCF is also
known as greatest common divisor(GCD) or greatest
common factor(gcf).
C programming code
#include <stdio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest common divisor of %d and %d =
%d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n",
x, y, lcm);
return 0;
}
int main() {
long x, y, hcf, lcm;
printf("Enter two integers\n");
scanf("%ld%ld", &x, &y);
hcf = gcd(x, y);
lcm = (x*y)/hcf;
printf("%ld ",factorial(i)/(factorial(c)*factorial(ic)));
printf("\n");
}
return 0;
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
Code find maximum or largest element present in an
array. It also prints the location or index at which
maximum element occurs in array. This can also be done
by using pointers (see both codes).
C programming code
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
1
11
121
1331
Pascal triangle in c
#include <stdio.h>
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in
pascal triangle\n");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
for (c = 0; c <= (n - i - 2); c++)
printf(" ");
for (c = 0 ; c <= i; c++)
Output of program:
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in array\n");
scanf("%d",&n);
#include <stdio.h>
int main()
{
long array[100], *maximum, size, c, location = 1;
Output of program:
#include <stdio.h>
#include <stdio.h>
int main()
{
int array[100], search, c, n, count = 0;
int main()
{
int array[100], search, c, n, position;
#include <stdio.h>
return -1;
if ( x == 1 )
printf("x is equal to one.\n");
else
printf("For comparison use == as = is the assignment
operator.\n");
}
Time required to search an element using linear search
main()
{
int x = 1;
return 0;
}
Output:
x is equal to one.
C program examples
Example 1 - C hello world program
/* A very simple c program printing a string on screen*/
#include <stdio.h>
main()
{
printf("Hello World\n");
return 0;
}
return 0;
}
Output:
#include <stdio.h>
Value is 1
main()
{
int number;
Value is 2
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
Value is 3
Example 5 - c program for prime number
#include <stdio.h>
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");
void my_function();
main()
{
printf("Main function.\n");
}
return 0;
my_function();
return 0;
}
#include <stdio.h>
main(int argc, char *argv[])
{
int c;
printf("Number of command line arguments passed:
%d\n", argc);
void my_function()
{
printf("Welcome to my function. Feel at home.\n");
}
Example 9 - Using comments in a program
#include <stdio.h>
main()
{
// Single line comment in c source code
return 0;
}
/*
* Multi line comment syntax
* Comments help us to understand code later easily.
* Will you write comments while developing
programs ?
*/
#include <stdio.h>
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 <stdio.h>
printf("%f\n", variable.constant);
printf("%s\n", variable.pointer);
#include <stdio.h>
#include <string.h>
return 0;
int main()
{
char a[100], b[100];
}
Example 11 - c program for Fibonacci series
#include <stdio.h>
main()
{
int n, first = 0, second = 1, next, c;
strcpy(b,a);
strrev(b);
if( strcmp(a,b) == 0 )
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");
return 0;
}
This c program generates pseudo random numbers using
rand and random function(Turbo C compiler only). As the
random numbers are generated by an algorithm used in a
function they are pseudo random, this is the reason why
pseudo word is used. Function rand() returns a pseudo
random number between 0 and RAND_MAX.
RAND_MAX is a constant which is platform dependent
and equals the maximum value returned by rand function.
}
Example 12 - c graphics programming
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
example
initgraph(&gd, &gm,"C:\\TC\\BGI");
a % b = 99 % 100 = 99
setcolor(BLUE);
line(350, 250, 450, 50);
getch();
closegraph( );
return 0;
}
C program for palindrome
int c, n;
int main()
{
struct date d;
getdate(&d);
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
int n, max, num, c;
C programming code
#include<stdlib.h>
int main()
{
system("C:\\Windows\\System32\\ipconfig");
return 0;
randomize();
for (c = 1; c <= n; c++)
{
num = random(max);
printf("%d\n",num);
}
getch();
return 0;
Output of program:
C programming code
#include <stdio.h>
int main()
{
int n, c, d, a[100], b[100];
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for (c = 0; c < n ; c++)
scanf("%d", &a[c]);
/*
* Copying elements into array b starting from end of
array a
*/
for (c = n - 1, d = 0; c >= 0; c--, d++)
b[d] = a[c];
/*
* Copying reversed array into original.
* Here we are modifying original array, this is
optional.
*/
end--;
return 0;
{
int n, c, *pointer;
scanf("%d",&n);
pointer = (int*)malloc(sizeof(int)*n);
if( pointer == NULL )
exit(EXIT_FAILURE);
for ( c = 0 ; c < n ; c++ )
scanf("%d",(pointer+c));
C programming code
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
reverse_array(pointer, n);
s = (int*)malloc(sizeof(int)*n);
array[position-1] = value;
if( s == NULL )
exit(EXIT_FAILURE);
return 0;
free(s);
}
Array is passed to function and a new array is created and
contents of passed array (in reverse order) are copied into
it and finally contents of new array are copied into array
passed to function.
This code will insert an element into an array, For
example consider an array a[10] having three elements in
it initially and a[0] = 1, a[1] = 2 and a[2] = 3 and you
want to insert a number 45 at location 1 i.e. a[0] = 45, so
we have to move elements one step below so after
insertion a[1] = 1 which was a[0] initially, and a[2] = 2
Output of program:
If the arrays are not sorted then you can sort them first
and then use the above merge function, another method is
to merge them and then sort the array. Sorting two smaller
arrays will take less time as compared to sorting a big
array. Merging two sorted array is used in merge sort
algorithm.
C program for bubble sort: c programming code for
bubble sort to sort numbers or arrange them in ascending
order. You can easily modify it to print numbers in
descending order.
Bubble sort algorithm in c
/* Bubble sort code */
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use
< */
{
swap
= array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}
Download Bubble sort program.
Output of program:
t
= list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
You can also sort strings using Bubble sort, it is less
efficient as its average and worst case complexity is high,
there are many other fast sorting algorithms.
Insertion sort in c: c program for insertion sort to sort
numbers. This code implements insertion sort algorithm
to arrange numbers of an array in ascending order. With a
little modification it will arrange numbers in descending
order.
Insertion sort algorithm implementation in c
/* insertion sort ascending order */
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
d--;
}
}
printf("Sorted list in ascending order:\n");
Output of program:
two matrices and then print it. Firstly user will be asked to
enter the order of matrix ( number of rows and columns )
return 0;
}
as
First Matrix :-
Output of program:
12
34
Second matrix :45
-1 5
then output of the program ( sum of First and Second
matrix ) will be
57
29
C programming code
#include <stdio.h>
int main()
{
C programming code
Output of program:
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10],
difference[10][10];
printf("Enter the number of rows and columns of
matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
34
example if a matrix is
12
56
then transpose of above matrix will be
135
246
When we transpose a matrix then the order of matrix
changes, but for a square matrix order remains same.
C programming code
#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix
");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix \n");
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&matrix[c][d]);
}
}
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
transpose[d][c] = matrix[c][d];
}
}
Output of program:
printf("\n");
Mathematics.
return 0;
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
char array[100];
printf("Enter a string\n");
scanf("%s", array);
printf("You entered the string %s\n",array);
return 0;
}
Input string containing spaces
#include <stdio.h>
int main()
{
char a[80];
gets(a);
printf("%s\n", a);
return 0;
}
Matrices are frequently used while doing programming
it using pointers.
printf("%s\n",array);
return 0;
length = strlen(a);
return 0;
}
Download String length program.
Output of program:
this.
C program to find string length without strlen
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
Download Compare Strings program.
Output of program:
pointer = array;
while(*(pointer+length))
length++;
printf("Length of entered string = %d\n",length);
return 0;
}
Function to find string length
int string_length(char *s)
{
int c = 0;
while(*(s+c))
c++;
return c;
}
This c program compares two strings
using strcmp, without strcmp and using pointers. For
comparing strings without using library function see
another code below.
}
char destination[50];
strcpy(destination, source);
return 0;
}
#include<stdio.h>
int compare_string(char*, char*);
int main()
{
char first[100], second[100], result;
main()
{
char source[100], target[100];
if ( result == 0 )
printf("Both strings are same.\n");
else
printf("Entered strings are not equal.\n");
copy_string(target, source);
printf("Target string is \"%s\"\n", target);
return 0;
return 0;
first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;
else
return -1;
}
This program copy string using library function strcpy, to
copy string without using strcpy see source code below in
which we have made our own function to copy string.
main()
{
char source[] = "C program";
anagram strings, here every character 'a', 'b' and 'c' occur
only one time in both strings. Our algorithm tries to find
comparing their corresponding counts.
Output of program:
#include <stdio.h>
int check_anagram(char [], char []);
int main()
{
char a[100], b[100];
int flag;
printf("Enter first string\n");
gets(a);
printf("Enter second string\n");
gets(b);
flag = check_anagram(a, b);
if (flag == 1)
printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
else
printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);
return 0;
C programming code
/* String reverse in c*/
#include <stdio.h>
#include <string.h>
int main()
{
char arr[100];
Output of program:
main()
{
char string[100];
printf("Enter a string\n");
gets(string);
reverse(string);
printf("Reverse of entered string is \"%s\".\n", string);
return 0;
length = string_length(string);
begin = string;
end = string;
#include <stdio.h>
#include <string.h>
int main()
{
char s[100], r[100];
int n, c, d;
printf("Input a string\n");
gets(s);
n = strlen(s);
for (c = n - 1, d = 0; c >= 0; c--, d++)
r[d] = s[c];
begin++;
end--;
}
}
int string_length(char *pointer)
{
int c = 0;
while( *(pointer + c) != '\0' )
c++;
r[d] = '\0';
printf("%s\n", r);
return c;
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
gets(a);
reverse(a, 0, strlen(a)-1);
printf("%s\n",a);
return 0;
Output of program:
}
void reverse(char *x, int begin, int end)
{
char c;
if (begin >= end)
return;
c
= *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
reverse(x, ++begin, --end);
}
#include <stdio.h>
int main()
{
char p[100], q[100];
int c, d;
printf("Input a string\n");
gets(p);
c = 0;
C programming code
d = 0;
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
p[c] = '\0';
printf("String obtained on concatenation is \"%s\"\n",
p);
return 0;
}
String concatenation using pointers
return 0;
}
#include <stdio.h>
int main()
{
char original[100], add[100];
strrev(arr);
concatenate_string(original, add);
Output of program:
return 0;
}
void concatenate_string(char *original, char *add)
{
while(*original)
original++;
while(*add)
{
*original = *add;
add++;
original++;
}
*original = '\0';
}
#include <stdio.h>
#include <string.h>
int main()
{
char s[100], r[100];
int n, c, d;
printf("Input a string\n");
gets(s);
n = strlen(s);
for (c = n - 1, d = 0; c >= 0; c--, d++)
r[d] = s[c];
r[d] = '\0';
printf("%s\n", r);
return 0;
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int string_length(char*);
void reverse(char*);
gets(a);
reverse(a, 0, strlen(a)-1);
main()
{
char string[100];
printf("Enter a string\n");
gets(string);
reverse(string);
printf("Reverse of entered string is \"%s\".\n", string);
printf("%s\n",a);
return 0;
}
void reverse(char *x, int begin, int end)
{
char c;
if (begin >= end)
return;
return 0;
}
c
= *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
at the end and then move towards the middle of the string.
begin++;
end--;
}
}
int string_length(char *pointer)
{
int c = 0;
while( *(pointer + c) != '\0' )
c++;
return c;
}
return 0;
}
Download Delete element from array program.
Output of program:
int main()
{
system("C:\\WINDOWS\\System32\\shutdown /s");
return 0;
}
#include <stdio.h>
int main() {
system("shutdown -P now");
return 0;
}
2...
26
25
24
23
22
21
20
...
64
32
16
102
101
100
Binary Theory
Hexadecimal
Practical Implications
C Standards
Types
Number Representation
26
25
24
23
22
21
20
Quotient
Remainder
20310 2 =
101
10110 2 =
50
5010 2 =
25
2510 2 =
12
1210 2 =
610 2 =
310 2 =
110 2 =
Kilobyte
220
Megabyte
230
Gigabyte
40
Terrabyte
250
Petabyte
260
Exabyte
Not
Usually represented by !, not simply inverts the value,
so 0 becomes 1 and 1 becomes 0
Table 2.6. Truth table for not
Input
Output
And
To remember how the and operation works think of it as
"if one input and the other are true, result is true
Table 2.7. Truth table for and
Input 1
0
Input 2
0
Input 2
Output
Output
0
Or
To remember how the or operation works think of it as "if
one input or the other input is true, the result is true
Table 2.8. Truth table for or
Input 1
Exclusive Or (xor)
Exclusive or, written as xor is a special case of or where
the output is true if one, and only one, of the inputs is true.
This operation can surprisingly do many interesting tricks,
but you will not see a lot of it in the kernel.
Input 2
Output
Operation
Usage in C
Hexadecimal
Binary
Decimal
not
0000
and
&
0001
or
0010
xor
0011
0100
0101
0110
0111
1000
1001
1010
10
1011
11
1100
12
1101
13
1110
14
1111
15
Remainder
20310 16 =
12
11 (0xB)
1210 16 =
12 (0xC)
To get the top (blue) four bits, we would invert the mask.
You will note this gives a result of 0x90 when really we
want a value of 0x09. To get the bits into the right
position we use the right shift operation.
Setting the bits requires the logical or operation. However,
rather than using 1's as the mask, we use 0's. You should
draw a diagram similar to the above figure and work
through setting bits with the logical oroperation.
Flags
Often a program will have a large number of variables
that only exist as flags to some condition. For example, a
state machine is an algorithm that transitions through a
number of different states but may only be in one at a
time. Say it has 8 different states; we could easily declare
8 different variables, one for each state. But in many cases
it is better to declare one 8 bit variable and assign each bit
to flag flag a particular state.
Flags are a special case of masking, but each bit
represents a particular boolean state (on or off). An n bit
variable can hold n different flags. See the code example
below for a typical example of using flags -- you will see
variations on this basic code very often.
Example 2.1. Using flags
1
#include <stdio.h>
name hex
binary
printf("FLAG1 or FLAG4
set!\n");
10 // ... and so on
35
#define FLAG8 0x80 // 10000000
return 0;
}
int main(int argc, char *argv[])
{
15
20
// check flags with a logical and. If the
flag is set (1)
// then the logical and will return 1,
causing the if
// condition to be true.
if (flags & FLAG1)
25
printf("FLAG1 set!\n");
Everybody knows that 5826 means five-thousand eighthundred twenty-six. But only because they have been
taught that 1, 10, 100, and 1000 are part of the
calculation even though they are never written. All that is
actually written is a total of twenty-one (5 and 8 and 2 and
6). Only the interpretation that people supply, which is
purely mental and unwritten, informs what is written with
its intended value.
Hexadecimal operates the same way. Each digit is
"weighted" by a "multiplier," with the results all added
10
11
12
13
14
15
Though the digits are the same (5826) the values come
out quite different, because the base and its "multiplier
values" are different.
You will see hexadecimal numbers some of whose digits
are letters instead of numbers. That's because the number
of digits needed by any number system is the number's
base. So base base 2 needs 2 digits, base 10 needs 10, and
base 16 needs 16. Base 2 has 0 and 1. Base 10 has 0
through 9. Base 16 borrows 0 though 9 but needs another
6. For those, we could invent some symbols. However, for
convenience we employ the first 6 letters of the alphabet
(A through F) instead. When we run out of digits at 9, we
use A as the next digit. So A represents the value 10. B
comes next, and represents 11. The hexadecimal digits
and thevalue they stand for are:
Hexadeci Valu
mal digit e
0