You are on page 1of 40

Created by Bhanu

chandar(jb)

1.C hello world example #include <stdio.h> int main() { printf("Hello world\n"); return 0; } 2. c program print integer This c program first inputs an integer and then prints it. Input is done using scanf function and number is printed on screen using printf. C programming code #include <stdio.h> int main() { int a; printf("Enter an integer\n"); scanf("%d", &a); printf("Integer that you have entered is %d\n", a); return 0; } Output of program:

3. c program to add two numbers C program to add two numbers: This c language program perform the basic arithmetic operation of addition on two numbers and then prints the sum on the screen. For example if the user entered two numbers as 5, 6 then 11 ( 5 + 6 ) will be printed on the screen. C programming code #include<stdio.h> main() { int a, b, c;

printf("Enter two numbers to add\n"); scanf("%d%d",&a,&b); c = a + b; printf("Sum of entered numbers = %d\n",c); return 0; } Add numbers program executable. Output of program

Addition without using third variable #include<stdio.h> main() { int a = 1, b = 2; /* Storing result of addition in variable a */ a = a + b; /* Not recommended because original value of a is lost * and you may be using it some where in code considering it * as it was entered by the user. */ printf("Sum of a and b = %d\n", a); return 0; }

4. C program to check odd or even using modulus operator #include<stdio.h> main() { int n; printf("Enter an integer\n"); scanf("%d",&n);

if ( n%2 == 0 ) printf("Even\n"); else printf("Odd\n"); return 0; } C program to check odd or even using bitwise operator #include<stdio.h> main() { int n; printf("Enter an integer\n"); scanf("%d",&n); if ( n & 1 == 1 ) printf("Odd\n"); else printf("Even\n"); return 0; } Find odd or even using conditional operator #include<stdio.h> main() { int n; printf("Enter an integer\n"); scanf("%d",&n); n%2 == 0 ? printf("Even number\n") : printf("Odd number\n"); return 0; }

5. C program to perform addition, subtraction, multiplication and division C program to perform basic arithmetic operations i.e. addition , subtraction, multiplication and division of two numbers. Numbers are assumed to be integers and will be entered by the user. C programming code

#include <stdio.h> int main() { int first, second, add, subtract, multiply; float divide; printf("Enter two integers\n"); scanf("%d%d", &first, &second); add = first + second; subtract = first - second; multiply = first * second; divide = first / (float)second; //typecasting printf("Sum = %d\n",add); printf("Difference = %d\n",subtract); printf("Multiplication = %d\n",multiply); printf("Division = %.2f\n",divide); return 0; } In c language when we divide two integers we get integer result for example 5/2 evaluates to 2. As a general rule integer/integer = integer and float/integer = float or integer/float = float. So we convert denominator to float in our program, you may also write float in numerator. This explicit conversion is known as typecasting. Output of program:

6. c program to check leap year c program to check leap year: c code to check leap year, year will be entered by the user. Please read theleap year article before reading the code, it will help you to understand the program. C programming code #include <stdio.h> int main() { int year;

printf("Enter a year to check if it is a leap year\n"); scanf("%d", &year); if ( year%400 == 0) printf("%d is a leap year.\n", year); else if ( year%100 == 0) printf("%d is not a leap year.\n", year); else if ( year%4 == 0 ) printf("%d is a leap year.\n", year); else printf("%d is not a leap year.\n", year); return 0; } Output of program:

7. Check vowel using switch statement #include <stdio.h> main() { char ch; printf("Enter a character\n"); scanf("%c", &ch); switch(ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O':

case 'u': case 'U': printf("%c is a vowel.\n", ch); break; default: printf("%c is not a vowel.\n", ch); } return 0; } Output:

8. Factorial program in c using for loop : Here we find factorial using for loop. #include <stdio.h> 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; printf("Factorial of %d = %d\n", n, fact); return 0; } Factorial Output of code:

Factorial program in c using function #include <stdio.h>

long factorial(int); int 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)); return 0; } 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> long factorial(int); int main() { int num; long f; printf("Enter a number to find factorial\n"); scanf("%d", &num); if (num < 0) printf("Negative numbers are not allowed.\n"); else { f = factorial(num); printf("%d! = %ld\n", num, f); } return 0; }

long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n-1)); } 9. c program to add n numbers This c program add n numbers which will be entered by the user. Firstly user will enter a number indicating how many numbers user wishes to add and then user will enter n numbers. In the first c program to add numbers we are not using an array, and using array in the second code. C programming code #include <stdio.h> int main() { int n, sum = 0, c, value; printf("Enter the number of integers you want to add\n"); scanf("%d", &n); printf("Enter %d integers\n",n); for (c = 1; c <= n; c++) { scanf("%d",&value); sum = sum + value; } printf("Sum of entered integers = %d\n",sum); return 0; }

Output of program:

C programming code using array #include <stdio.h> int main() { int n, sum = 0, c, array[100]; scanf("%d", &n); for (c = 0; c < n; c++) { scanf("%d", &array[c]); sum = sum + array[c]; } printf("Sum = %d\n",sum); return 0; } 10. c program to swap two numbers C program to swap two numbers with and without using third variable, swapping in c using pointers,functions (Call by reference) and using bitwise XOR operator, swapping means interchanging. For example if in your c program you have taken two variable a and b where a = 4 and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4 In our c program to swap numbers we will use a temp variable to swap two numbers.

Swapping is used in sorting that is when we wish to arrange numbers in a particular order either in ascending order or in descending order. Swapping of two numbers in c #include <stdio.h> int main() { int x, y, temp; printf("Enter the value of x and y\n"); scanf("%d%d", &x, &y); printf("Before Swapping\nx = %d\ny = %d\n",x,y); temp = x; x = y; y = temp; printf("After Swapping\nx = %d\ny = %d\n",x,y); return 0; } Swapping of two numbers without third variable You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :#include <stdio.h> int main() { int a, b; printf("Enter two integers to swap\n"); scanf("%d%d", &a, &b); a = a + b; b = a - b; a = a - b; printf("a = %d\nb = %d\n",a,b); return 0; } To understand above logic simply choose a as 7 and b as 9 and then do what is written in program. You can choose any other combination of numbers as well. Sometimes it's a good way to understand a program. Swap two numbers using pointers

#include <stdio.h> int main() { int x, y, *a, *b, temp; printf("Enter the value of x and y\n"); scanf("%d%d", &x, &y); printf("Before Swapping\nx = %d\ny = %d\n", x, y); a = &x; b = &y; temp = *b; *b = *a; *a = temp; printf("After Swapping\nx = %d\ny = %d\n", x, y); return 0; } 11. c program to reverse a number C Program to reverse a number :- This program reverse the number entered by the user and then prints the reversed number on the screen. For example if user enter 123 as input then 321 is printed as output. In our program we use modulus(%) operator to obtain the digits of a number. To invert number look at it and write it from opposite direction or the output of code is a number obtained by writing original number from right to left. To reverse large numbers use long data type or long long data type if your compiler supports it, if you still have large numbers then use strings or other data structure. C programming code #include <stdio.h> main() { int n, reverse = 0; printf("Enter a number to reverse\n"); scanf("%d",&n); while (n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10;

} printf("Reverse of entered number is = %d\n", reverse); return 0; } Output of program:

Palindrome Numbers Palindrome number in c: A palindrome number is a number such that if we reverse it, it will not change. For example some palindrome numbers examples are 121, 212, 12321, -454. To check whether a number is palindrome or not first we reverse it and then compare the number obtained with the original, if both are same then number is palindrome otherwise not. C program for palindrome number is given below. Palindrome number algorithm 1. Get the number from user. 2. Reverse it. 3. Compare it with the number entered by the user. 4. If both are same then print palindrome number 5. Else print not a palindrome number. Palindrome number program c #include<stdio.h> main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or not\n"); scanf("%d",&n); 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; } 12. c program for prime number Prime number program in c: c program for prime number, this code prints prime numbers using c programming language. To check whether a number is prime or not see another code below. Prime number logic: a number is prime if it is divisible only by one and itself. Remember two is the only even and also the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13, 17....etc. Prime numbers have many applications in computer science and mathematics. A number greater than one can be factorized into prime numbers, For example 540 = 22*33*51 Prime number program in c language #include<stdio.h> main() { int n, i = 3, count, c; printf("Enter the number of prime numbers required\n"); scanf("%d",&n); if ( n >= 1 ) { printf("First %d prime numbers are :\n",n); printf("2\n"); } for ( count = 2 ; count <= n ; ) { for ( c = 2 ; c <= i - 1 ; c++ ) { if ( i%c == 0 ) break; } if ( c == i ) { printf("%d\n",i); count++; } i++; } return 0; } There are many logic to check prime numbers, one given below is more efficient then above method. for ( c = 2 ; c //only checking from 2 to square root of number is sufficient.

There are many more efficient logic then written above. C program for prime number or not #include<stdio.h> main() { int n, c = 2; printf("Enter a number to check if it is prime\n"); scanf("%d",&n); for ( c = 2 ; c <= n - 1 ; c++ ) { if ( n%c == 0 ) { printf("%d is not prime.\n", n); break; } } if ( c == n ) printf("%d is prime.\n", n); return 0; } 13. Fibonacci series in c Fibonacci series in c programming: c program for Fibonacci series without and with recursion. Using the code below you can print as many number of terms of series as desired. Numbers of Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms, For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and Computer Science. Fibonacci series in c using for loop /* Fibonacci Series c language */ #include<stdio.h> 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:

14. c program to print Pascal triangle Pascal Triangle in c: C program to print Pascal triangle which you might have studied in Binomial Theorem in Mathematics. Number of rows of Pascal triangle to print is entered by the user. First four rows of Pascal triangle are shown below :1 11 121 1331 Pascal triangle in c #include<stdio.h> long factorial(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++ ) printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c))); printf("\n"); } return 0; } long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); } Output of program:

15. c program to add two numbers using pointers This program performs addition of two numbers using pointers. In our program we have two two integer variables x, y and two pointer variables p and q. Firstly we assign the addresses of x and y to p and q respectively and then assign the sum of x and y to variable sum. Note that & is address of operator and * is value at address operator. C programming code #include<stdio.h> main() {

int first, second, *p, *q, sum; printf("Enter two integers to add\n"); scanf("%d%d", &first, &second); p = &first; q = &second; sum = *p + *q; printf("Sum of entered numbers = %d\n",sum); return 0; } Output of code:

16. linear search in c Linear search in c programming: The following code implements linear search ( Searching algorithm ) which is used to find whether a given number is present in an array and if it is present then at what location it occurs.It is also known as sequential search. It is very simple and works as follows: We keep on comparing each element with the element to search until the desired element is found or list ends. Linear search in c language for multiple occurrences and using function. Linear search c program #include<stdio.h> main() { int array[100], search, c, number; printf("Enter the number of elements in array\n"); scanf("%d",&number); printf("Enter %d numbers\n", number); for ( c = 0 ; c < number ; c++ ) scanf("%d",&array[c]); printf("Enter the number to search\n"); scanf("%d",&search); for ( c = 0 ; c < number ; c++ ) {

if ( array[c] == search ) /* if required element found */ { printf("%d is present at location %d.\n", search, c+1); break; } } if ( c == number ) printf("%d is not present in array.\n", search); return 0; } C program for binary search Output of code:

17. C program for binary search C program for binary search: This code implements binary search in c language. It can only be used for sorted arrays, but it's fast as compared to linear search. If you wish to use binary search on an array which is not sorted then you must sort it using some sorting technique say merge sort and then use binary search algorithm to find the desired element in the list. If the element to be searched is found then its position is printed. The code below assumes that the input numbers are in ascending order. C programming code for binary search #include<stdio.h> main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to find\n"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.\n", search); return 0; } 18. c program to reverse an array c program to reverse an array :- This program reverses the array elements. For example if a is an array of integers with three elements such that a[0] = 1 a[1] = 2 a[2] = 3 then on reversing the array will be a[0] = 3 a[1] = 2 a[0] = 1 Given below is the c code to reverse an array . 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. */ for (c = 0; c < n; c++) a[c] = b[c]; printf("Reverse array is\n"); for (c = 0; c < n; c++) printf("%d\n", a[c]); return 0; } Output of program:

19. c program to add two matrix This c program add two matrices i.e. compute the sum of two matrices and then print it. Firstly user will be asked to enter the order of matrix ( number of rows and columns ) and then two matrices. For example if the user entered order as 2, 2 i.e. two rows and two columns and matrices as First Matrix :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> main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrix "); 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"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d] + second[c][d]; printf("Sum of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) printf("%d\t", sum[c][d]); printf("\n"); }

return 0; } Add matrices program. Output of program:

20. String length This program prints length of string, for example consider the string "c programming" it's length is 13. Null character is not counted when calculating string length. To find string length we use strlen function of string.h. C program to find string length #include<stdio.h> #include<string.h> main() { char a[100]; int length; printf("Enter a string to calculate it's length\n"); gets(a); length = strlen(a); printf("Length of entered string is = %d\n",length); return 0;

} String length program executable. Output of program:

You can also find string length using pointer or without strlen function. Following program shows how to achieve this. C program to find string length without strlen C program to find length of a string using pointers. #include<stdio.h> main() { char array[100], *pointer; int length = 0; printf("Enter a string\n"); gets(array); 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; }

21. Subtract matrices C code to subtract matrices of any order. This program finds difference between corresponding elements of two matrices and then print the resultant matrix. C programming code #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"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &second[c][d]); for (c = 0; c < m; c++) for (d = 0; d < n; d++) difference[c][d] = first[c][d] - second[c][d]; printf("difference of entered matrices:-\n"); for (c = 0; c < m; c++) { for (d = 0; d < n; d++) printf("%d\t",difference[c][d]); printf("\n"); } return 0; } 22. Matrix multiplication in c Matrix multiplication in c language: c program to multiply matrices (two dimensional array), this program multiplies two matrices which will be entered by the user. Firstly user will enter the order of a matrix. If the entered orders of two matrix is such that they can't be multiplied then an error message is displayed on the screen. You have already studied the logic to multiply them in Mathematics. Matrices are frequently used while

doing programming and are used to represent graph data structure, in solving system of linear equations and many more. Matrix multiplication in c language #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]; printf("Enter the number of rows and columns of first 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 number of rows and columns of second matrix\n"); scanf("%d%d", &p, &q); if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.\n"); else { printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) { for ( k = 0 ; k < p ; k++ ) { sum = sum + first[c][k]*second[k][d]; } multiply[c][d] = sum; sum = 0; } } printf("Product of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ )

{ for ( d = 0 ; d < q ; d++ ) printf("%d\t", multiply[c][d]); printf("\n"); } } return 0; } A 3 X 3 matrix multiply in c is shown as example below. Compiler used: GCC Output of program:

23. c program to compare two strings This c program compares two strings using strcmp, without strcmp and using pointers. For comparing strings without using library function see another code below. C program to compare two strings using strcmp #include<stdio.h> #include<string.h> main() { char a[100], b[100]; printf("Enter the first string\n"); gets(a);

printf("Enter the second string\n"); gets(b); if( strcmp(a,b) == 0 ) printf("Entered strings are equal.\n"); else printf("Entered strings are not equal.\n"); return 0; } C program to compare two strings without using strcmp Here we create our own function to compare strings. int compare(char a[], char b[]) { int c = 0; while( a[c] == b[c] ) { if( a[c] == '\0' || b[c] == '\0' ) break; c++; } if( a[c] == '\0' && b[c] == '\0' ) return 0; else return -1; } 24. string copying in c programming 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. C program to copy a string #include<stdio.h> #include<string.h> main() { char source[] = "C program"; char destination[50]; strcpy(destination, source); printf("Source string: %s\n", source); printf("Destination string: %s\n", destination); return 0;

} 25. c program to concatenate strings This program concatenates strings, for example if the first string is "c " and second string is "program" then on concatenating these two strings we get the string "c program". To concatenate two strings we use strcat function of string.h, to concatenate without using library function see another code below which uses pointers. C code #include<stdio.h> #include<conio.h> #include<string.h> 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); getch(); return 0; } string concatenation. Output:

26. c palindrome program, c program for palindrome C program for palindrome or palindrome in c programming: palindrome program in c language, c code to check if a string is a palindrome or not and for palindrome number. This program works as follows :- at first we copy the entered string into a new string, and then we reverse the new string and then compares it with original string. If both of them have same sequence of characters i.e. they are identical then the entered string is a palindrome otherwise not. To perform copy, reverse and compare operations we use

strcpy, strrev and strcmp functions of string.h respectively, if you do not wish to use these functions see c programming code for palindrome without using string functions. Some palindrome strings examples are "dad", "radar", "madam" etc. C program for palindrome #include <stdio.h> #include <string.h> main() { char a[100], b[100]; printf("Enter the string to check if it is a palindrome\n"); gets(a); 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; } Download palindrome executable Output of program:

Palindrome number in c #include <stdio.h> main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or not\n"); scanf("%d",&n); 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; } 27. C program to shutdown or turn off computer C Program to shutdown your computer: This program turn off i.e shutdown your computer system. Firstly it will asks you to shutdown your computer if you press 'y' the your computer will shutdown in 30 seconds, system function of "stdlib.h" is used to run an executable file shutdown.exe which is present in C:\WINDOWS\system32 in Windows XP. You can use various options while executing shutdown.exe for example -s option shutdown the computer after 30 seconds, if you wish to shutdown immediately then you can write "shutdown -s -t 0" as an argument to system function. If you wish to restart your computer then you can write "shutdown -r". If you are using Turbo C Compiler then execute your file from folder. Press F9 to build your executable file from source program. When you run from within the compiler by pressing Ctrl+F9 it may not work. C programming code for Windows XP #include <stdio.h> #include <stdlib.h> main() { char ch; printf("Do you want to shutdown your computer now (y/n)\n"); scanf("%c",&ch); if (ch == 'y' || ch == 'Y') system("C:\\WINDOWS\\System32\\shutdown -s"); return 0; } C programming code for Windows 7 #include <stdio.h> #include <stdlib.h> main() { char ch;

printf("Do you want to shutdown your computer now (y/n)\n"); scanf("%c",&ch); if (ch == 'y' || ch == 'Y') system("C:\\WINDOWS\\System32\\shutdown /s"); return 0; } To shutdown immediately use "C:\\WINDOWS\\System32\\ shutdown /s /t 0". To restart use /r instead of /s. C programming code for Ubuntu Linux #include <stdio.h> int main() { system("shutdown -P now"); return 0; } You need to be logged in as root user for above program to execute otherwise you will get the messageshutdown: Need to be root, now specifies that you want to shutdown immediately. '-P' option specifes you want to power off your machine. You can specify minutes as: shutdown -P "number of minutes" For more help or options type at terminal: man shutdown.
//44. Program to find factorial of a number using recursion. #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("enter number: "); scanf("%d",&n); if(n<0) printf("invalid number"); else printf("%d!=%d",n,fact(n)); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } int fact(int x) { if(x==0) return 1; else return(x*fact(x-1)); } //45. Program to find whether a string is palindrome or not. #include<stdio.h>

#include<conio.h> void main() { char s1[20],s2[20]; clrscr(); printf("enter a string: "); scanf("%s",s1); strcpy(s2,s1); strrev(s2); if(strcmp(s1,s2)==0) printf("string is a palindrome"); else printf("not a palindrome string"); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //40. Program to show table of a number using functions. #include<stdio.h> #include<conio.h> void main() { void table(); clrscr(); table(); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } void table() { int n,i,r; printf("enter a no to know table: "); scanf("%d",&n); for(i=1;i<=10;i++) { r=n*i; printf("%d*%d=%d\n",n,i,r); } } //38. Program to swap two numbers using functions. #include<stdio.h> #include<conio.h> void main() { void swap(int,int); int a,b,r; clrscr(); printf("enter value for a&b: "); scanf("%d%d",&a,&b); swap(a,b); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } void swap(int a,int b)

{ int temp; temp=a; a=b; b=temp; printf("after swapping the value for a & b is : %d %d",a,b); } //39. Program to find factorial of a number using functions. #include<stdio.h> #include<conio.h> void main() { int a,f; int fact(int); clrscr(); printf("enter a no: "); scanf("%d",&a); f=fact(a); printf("factorial= %d",f); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } int fact(int x) { int fac=1,i; for(i=x;i>=1;i--) fac=fac*i; return(fac); } //26. Program to add two number using pointer. #include<stdio.h> #include<conio.h> void main() { int *p1,*p2,sum; clrscr(); printf("enter two no's: "); scanf("%d%d",&*p1,&*p2); sum=*p1 *p2; printf("sum=%d",sum); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //23. Program to display sum of series 1+1/2+1/3+.......+ 1/n. #include<stdio.h> #include<conio.h> void main() { int n,i,sum=0; clrscr(); printf("Enter any no: "); scanf("%d",&n); printf("1");

for(i=2;i<=n-1;i++) printf(" 1/%d ",i); for(i=1;i<=n;i++) sum=sum+i; printf(" 1/%d",n); printf("\nSum=1/%d",sum 1/n); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //24. Program to display series and find sum of 1+3+5+........+n. #include<stdio.h> #include<conio.h> void main() { int n,i,sum=0; clrscr(); printf("Enter any no: "); scanf("%d",&n); for(i=1;i<n;i=i 2) { printf("%d ",i); sum=sum i; } printf("%d",n); printf("\nsum=%d",sum n); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //21. Program to find factorial of a number. #include<stdio.h> #include<conio.h> void main() { int n,i,fact=1; clrscr(); printf("Enter any no: "); scanf("%d",&n); for(i=n;i>=1;i--) { fact=fact*i; } printf("Factorial=%d",fact); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //22. Program to find whether given no is a prime no or not. #include<stdio.h> #include<conio.h> void main() { int i,n,r=0; clrscr();

printf("Enter any no: "); scanf("%d",&n); for(i=2;i<=n-1;i++) { if(n%i==0) r=1; break; } if(r==0) printf("prime no"); else printf("Not prime"); getch(); //Visit: jabroo.blogspot } //20. Program to print Fibonacci series up to 100. #include<stdio.h> #include<conio.h> void main() { int a=1,b=1,c=0,i; clrscr(); printf("%d\t%d\t",a,b); for(i=0;i<=10;i++) { c=a b; if(c<100) { printf("%d\t",c); } a=b; b=c; } getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //17. Program to print stars Sequence 1. #include<stdio.h> #include<conio.h> void main() { int i,j; clrscr(); for(i=1;i<=5;i++ ) { for(j=1;j<=i;j++) printf("*"); printf("\n"); } getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //18. Program to print stars Sequence 2. #include<stdio.h> #include<conio.h>

void main() { int i,j,k; clrscr(); for(i=1;i<=5;i++) { for(j=5;j>=i;j--) printf(" "); for(k=1;k<=i;k++) printf("*"); printf("\n"); } getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //11. Program to find that entered year is leap year or not. #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("enter any year: "); scanf("%d",&n); if(n%4==0) printf("year is a leap year"); else printf("year is not a leap year"); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //12. Program to find whether given no is even or odd. #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("enter any no: "); scanf("%d",&n); if(n%2==0) printf("no is even"); else printf("no is odd"); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //8. Program to print a table of any number. #include<stdio.h> #include<conio.h> void main()

{ int gs,bs,da,ta; clrscr(); printf("enter basic salary: "); scanf("%d",&bs); da=(10*bs)/100; ta=(12*bs)/100; gs=bs+da+ta; printf("gross salary=%d",gs); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //5. Program to show swap of two no's without using third variable. #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("enter value for a & b: "); scanf("%d%d",&a,&b); a=a+b; b=a-b; a=a-b; printf("after swapping the value of a & b: %d %d",a,b); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. } //6. Program to reverse a given number. #include<stdio.h> #include<conio.h> void main() { int n,a,r=0; clrscr(); printf("enter any no to get its reverse: "); scanf("%d",&n); while(n>=1) { a=n%10; r=r*10 a; n=n/10; } printf("reverse=%d",r); getch(); //Visit: jabroo.blogspot.com for computer tips and tricks. }

C program for palindrome without using string functions


#include <stdio.h> #include <string.h> main()

char text[100]; int begin, middle, end, length = 0; gets(text); while ( text[length] != '\0' ) length++; end = length - 1; middle = length/2; for( begin = 0 ; begin < middle ; begin++ ) { if ( text[begin] != text[end] ) { printf("Not a palindrome.\n"); break; } end--; } if( begin == middle ) printf("Palindrome.\n");

return 0;

selection sort in c
Selection sort in c: c program for selection sort to sort numbers. This code implements selection sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.

Selection sort algorithm implementation in c


#include<stdio.h> main() { int array[100], n, c, d, position, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); for ( c = 0 ; c < ( n - 1 ) ; c++ ) { position = c; for ( d = c + 1 ; d < n ; d++ ) { if ( array[position] > array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; }

} printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); } return 0;

C Program to implement Insertion sort.


Insertion sorting technique is the elementary sorting technique. Insertion sort sorts one element at a time, It is just like manual sorting by humans. Insertion sort is better for small set of elements. Insertion sort is slower than heap sort, shell sort, quick sort,and merge sort. Read more about C Programming Language . #include<stdio.h> #include<conio.h> void inst_sort(int []); void main() { int num[5],count; clrscr(); printf("\nEnter the Five Elements to sort:\n"); for (count=0;count<5;count++) scanf("%d",&num[count]); inst_sort(num); printf("\n\nElements after sorting: \n"); for(count=0;count<5;count++) printf("%d\n",num[count]); getch(); } // Function for Insertion Sorting void inst_sort(int num[]) { int i,j,k; for(j=1;j<5;j++) { k=num[j]; for(i=j-1;i>=0 && k<num[i];i--) num[i+1]=num[i]; num[i+1]=k; } }

C Program To Sort An Array Using Bubble Sort


C Program To Sort An Array Using Bubble Sort. Bubble Sort is the simplest and easiest sorting technique. In this technique, the two successive items A[i] and A[i+1] are exchanged whenever A[i]>=A[i+1]. The larger values sink to the bottom of the array and hence it is called sinking sort. The end of each pass smaller values gradually "bubble" their way upward to the top(like air bubbles moving to surface of water) and hence called bubble sort. Read more about C Programming Language . #include<stdio.h> int main() { int a[50], n, i, j, temp = 0; printf("Enter how many numbers you want:\n");

scanf("%d", &n); printf("Enter the %d elements:\n", n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } printf("\n\t\tThe given array is:\n"); for (i = 0; i < n; i++) { printf("\n\t\t%d", a[i]); } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } printf("\n\n\n\t\tThe sorted array using Buble sort is:\n"); for (i = 0; i < n; i++) { printf("\n\t\t%d", a[i]); } return 0; }

You might also like