More Topics

November 27th, 2012

Multiple Choice in C: Set 1

QUESTION 1. Choose the correct answer Use of goto enhances the logical clarity of a code Use of goto makes the debugging task easier Use goto when you want to jump out of a nested loop Never user goto   QUESTION 2. The minimum number of temporary variables needed to swap the contents of two ...

Full Article +
November 4th, 2012

Mistakes to avoid: Excessive Commenting

Many comments are unnecessary. They generally make source code hard to maintain. Consider the following statement: a = b; // assign b to a The comment can’t communicate the meaning of the statement more clearly than the code itself, and so is useless.The comment distracts the reader from the code, increasing the volume of text ...

Full Article
April 18th, 2013

C Solutions: Heap sort

#include<stdio.h> #include<conio.h> void manage(int *, int); void heap_sort(int *, int, int); int main() { int a[50]; int i,j,size,temp,k; printf(“\n\t——- Heap sort ——-\n\n”); printf(“Enter the number of elements to be sorted : “); scanf(“%d”,&size); for(i=1; i<=size; i++) { printf(“Enter %d element : “,i); scanf(“%d”,&a[i]); manage(a,i); } j=size; for(i=1; i<=j; i++) { temp=a[1]; a[1]=a[size]; a[size]=temp; size–; heap_sort(a,1,size); ...

Full Article
  • None found
April 18th, 2013

C Solutions: Heap sort

#include<stdio.h> #include<conio.h> void manage(int *, int); void heap_sort(int *, int, int); int main() { int a[50]; int i,j,size,temp,k; printf(“\n\t——- Heap sort ——-\n\n”); printf(“Enter the number of elements to be sorted : “); scanf(“%d”,&size); for(i=1; i<=size; i++) { printf(“Enter %d element : “,i); scanf(“%d”,&a[i]); manage(a,i); } j=size; for(i=1; i<=j; i++) { temp=a[1]; a[1]=a[size]; a[size]=temp; size–; heap_sort(a,1,size); ...

Full Article
February 5th, 2013

C Solutions: Insertion Sort

#include<stdio.h> #include<conio.h> main() { int array[20], n, temp, i, j, v; printf(“\n\n\t Enter the number of terms…: “); scanf(“%d”, &n); printf(“\n Enter the value of v: “); scanf(“%d”, &v); if(v<n) { printf(“\n\t Enter the elments…:”); for(i=0; i<n; i++) scanf(“\n\t\t%d”, &array[i]); for(i=1; i<n; i++) { temp = array[i]; j = i-1; while(array[j]>temp && j>=0) { array[j+1] ...

Full Article
February 2nd, 2013

C Solutions: Bubble Sort

#include<stdio.h> #include<conio.h> main() { int array[20], n, temp, i, j; printf(“\n\n\t Enter The No. of Terms : “); scanf(“%d”,&n); printf(“\n\t Enter The Elements :”); for(i=0; i<n; i++) { scanf(“\n\t\t%d”, &array[i]); } for(i=0; i<n-1; i++) for(j=i; j<n-i-1;j++) if(array[j]>array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } printf(“\n\tThe ascending order list is…:\n”); for(i=0; i<n; ...

Full Article
February 2nd, 2013

C Solutions: To Display numbers(FLOYD) in a triangle

/* To Display numbers(FLOYD) in a triangle */ main() { int n,i,j,k=0; clrscr(); printf(“Enter the number of lines: “); scanf(“%d”,&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { k=k+1; printf(“%d “,k); } printf(“\n”); } }

Full Article
January 13th, 2013

C Solutions: To check whether a Palindrome Number or not

A palindrome number is a number such that if we reverse it, it will not change. For example some palindrome numbers examples are 121, 282, 14941, #include<stdio.h> main() { int n, rev = 0, temp; printf(“Enter a number:\n”); scanf(“%d”,&n); temp = n; while( temp != 0 ) { rev = rev * 10; rev = ...

Full Article
January 12th, 2013

c program to find Combination and Permutation

C program to find Combination (nCr) and Permutation (nPr) : nCr = n!/(r!*(n-r)!) and nPr = n!/(n-r)! #include<stdio.h> long factorial(int); long find_ncr(int, int); long find_npr(int, int); main() { int n, r; long ncr, npr; printf(“Enter the values of n and r: \n”); scanf(“%d%d”,&n,&r); ncr = find_ncr(n, r); npr = find_npr(n, r); printf(“%dC%d = %ld\n”, n, ...

Full Article
January 11th, 2013

C Solutions: To find LCM and HCF/GCD

Following is the code: #include <stdio.h> int main() { int a, b, x, y, t, gcd, lcm; printf(“Enter the 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(“GCD ...

Full Article
November 29th, 2012

C solutions: linear search (sequential search)

#include<stdio.h> #include<conio.h> main() { int array[20],pos,n,key,linear_search(int,int *,int); printf(“\n\n Enter the no. of elements: “); scanf(“%d”,&n); printf(“\n\n Enter the elements: \n”); for(int i=0;i<n;i++) scanf(“%d”,&array[i]); printf(“\n\n Enter the no. to be search: “); scanf(“%d”,&key); pos=linear_search(n,array,key); if(pos==0) printf(“\n %d is not found “,key); else printf(“\n %d is found at %d position “,key,pos); return 0; getch(); } int linear_search(int ...

Full Article
November 22nd, 2012

C Solutions: binary search (Interpolation search)

#include<stdio.h> #include<conio.h> main() { int array[20],pos,n,binary_search(int,int *,int),key; printf(“\n\n Enter the no. of elements: “); scanf(“%d”,&n); printf(“\n\n Enter the elements: \n”); for(int i=0;i<n;i++) scanf(“%d”,&array[i]); printf(“\n\n Enter the no. to be search: “); scanf(“%d”,&key); pos=binary_search(n,array,key); if(pos==0) printf(“\n %d is not found “,key); else printf(“\n %d is found at %d position “,key,pos); return 0; } int binary_search(int n,int ...

Full Article
November 15th, 2012

C Solutions: To find the factorial of a number

Here is the code in C to find out the Factorial of any positive integer number: #include<stdio.h> int main() { int n,i,fact=1; printf(“Enter the value of n: “); scanf(“%d”,&n); for(i=1;i<=n;i++) { fact=fact*i; } printf(“the value for %d! is %d”,n,fact); return 0; }

Full Article