Recent Posts

Thursday, 15 December 2011

Top 20 Important C Language Programs, with explanation


Fibonacci series: Any number in the series is obtained by adding the previous two numbers of the series.
Let f(n) be n'th term.
f(0)=0;
f(1)=1;
f(n)=f(n-1)+f(n-2); (for n>=2)
Series is as follows
011
(1+0)
2 (1+1)
3 (1+2)
5 (2+3)
8 (3+5)
13 (5+8)
21 (8+13)
34 (13+21)
...and so on
Program: to generate Fibonacci Series(10 terms)
#include<stdio.h>
int main() {
//array fib stores numbers of fibonacci series
int i, fib[25];
//initialized first element to 0
fib[0] = 0;
//initialized second element to 1
fib[1] = 1;
//loop to generate ten elements
for (i = 2; i < 10; i++) {
//i'th element of series is equal to the sum of i-1'th element and i-2'th element.
fib[i] = fib[i - 1] + fib[i - 2];
}
printf("The fibonacci series is as follows \n");
//print all numbers in the series
for (i = 0; i < 10; i++) {
printf("%d \n", fib[i]);
}
return 0;
}
Output:
The fibonacci series is as follows
01123581
3
21
34
Explanation:
The first two elements are initialized to 0, 1 respectively. Other elements in the series are generated by looping
and adding previous two numbes. These numbers are stored in an array and ten elements of the series are
printed as output.


Generally when we use printf("") statement, we have to use a semicolon at the end. If printf is used inside an if
condition, semicolon can be avoided.
Program: Program to print some thing with out using semicolon(;)
#include <stdio.h>
int main() {
//printf returns the length of string being printed
if (printf("Hello World\n")) //prints Hello World and returns 11
{
//do nothing
}
return 0;
}
Output:
Hello World
Explanation:
The if statement checks for condition whether the return value of printf("Hello World") is greater than 0. printf
function returns the length of the string printed. Hence the statement if (printf("Hello World")) prints the string
"Hello World".

Generally when use printf("") statement we have to use semicolon at the end.
If we want to print a semicolon, we use the statement: printf(";");
In above statement, we are using two semicolons. The task of printing a semicolon without using semicolon
anywhere in the code can be accomplished by using the ascii value of ' ; ' which is equal to 59.
Program: Program to print a semicolon without using semicolon in the code.
#include <stdio.h>
int main(void) {
//prints the character with ascii value 59, i.e., semicolon
if (printf("%c\n", 59)) {
//prints semicolon
}
return 0;
}
Output:
;
Explanation:
If statement checks whether return value of printf function is greater than zero or not. The return value of function
call printf("%c",59) is 1. As printf returns the length of the string printed. printf("%c",59) prints ascii value that
corresponds to 59, that is semicolon(;).


strcmp() function compares two strings lexicographically. strcmp is declared in stdio.h
Case 1: when the strings are equal, it returns zero.
Case 2: when the strings are unequal, it returns the difference between ascii values of the characters that differ.
a) When string1 is greater than string2, it returns positive value.
b) When string1 is lesser than string2, it returns negative value.
Syntax:
int strcmp (const char *s1, const char *s2);
Program: to compare two strings.
#include<stdio.h>
#include<string.h>
int cmpstr(char s1[10], char s2[10]);
int main() {
char arr1[10] = "Nodalo";
char arr2[10] = "nodalo";
printf(" %d", cmpstr(arr1, arr2));
//cmpstr() is equivalent of strcmp()
return 0;
}/
/s1, s2 are strings to be compared
int cmpstr(char s1[10], char s2[10]) {
//strlen function returns the length of argument string passed
int i = strlen(s1);
int k = strlen(s2);
int bigger;
if (i < k) {
bigger = k;
}
else if (i > k) {
bigger = i;
}
else {
bigger = i;
}
//loops 'bigger' times
for (i = 0; i < bigger; i++) {
//if ascii values of characters s1[i], s2[i] are equal do nothing
if (s1[i] == s2[i]) {
}
//else return the ascii difference
else {
return (s1[i] - s2[i]);
}
}
//return 0 when both strings are same
//This statement is executed only when both strings are equal
return (0);
}
Output:
-32
Explanation:
cmpstr() is a function that illustrates C standard function strcmp(). Strings to be compared are sent as arguments
to cmpstr().
Each character in string1 is compared to its corresponding character in string2. Once the loop encounters a
differing character in the strings, it would return the ascii difference of the differing characters and exit.
strcat(string1,string2) is a C standard function declared in the header file string.h
The strcat() function concatenates string2, string1 and returns string1.
Program: Program to concatenate two strings
#include<stdio.h>
#include<string.h>
char *strct(char *c1, char *c2);
char *strct(char *c1, char *c2) {
//strlen function returns length of argument string
int i = strlen(c1);
int k = 0;
//loops until null is encountered and appends string c2 to c1
while (c2[k] != '\0') {
c1[i + k] = c2[k];
k++;
}
return c1;
}
int main() {
char string1[15] = "first";
char string2[15] = "second";
char *finalstr;
printf("Before concatenation:"
" \n string1 = %s \n string2 = %s", string1, string2);
//addresses of string1, string2 are passed to strct()
finalstr = strct(string1, string2);
printf("\nAfter concatenation:");
//prints the contents of string whose address is in finalstr
printf("\n finalstr = %s", finalstr);
//prints the contents of string1
printf("\n string1 = %s", string1);
//prints the contents of string2
printf("\n string2 = %s", string2);
return 0;
}
Output:
Before concatenation:
string1 = first
string2 = second
After concatenation:
finalstr = firstsecond
string1 = firstsecond
string2 = second
Explanation:
string2 is appended at the end of string1 and contents of string2 are unchanged.
In strct() function, using a for loop, all the characters of string 'c2' are copied at the end of c1. return (c1) is
equivalent to return &c1[0] and it returns the base address of 'c1'. 'finalstr' stores that address returned by the
function strct().

0 comments:

Post a Comment

**FRESHER JOBS**