Subscribe

RSS Feed (xml)

Thursday, December 1, 2011

How To Add Syntax Highlighter for Blogger/Blogspot

Syntax Highlighter are generally used for blogs, which mainly focus on scripting tutorials. We could have seen in many popular blogs using script highlighter to highlight the script. It not only highlight the script, but also give us a clear idea, how the script was formulated. This javascript can be capable of displaying css, xml, jscript, python, c# and much more supported programing languages. Most of the blogs are using the older version of Syntax Highlighter created by Alexgorbatchev. So here I am going to teach you, how to install syntax highlighter 2.1 (latest version) on your blogger / blogspot blogs. Before you could get in to the process, I recommend you to back up your template.














Step 1: 
 I made the installation steps more simple to help people implement it without any mess. Navigate to Blogger dashboard > Layout > Edit HTML  and Expand Widget Templates. Search for and replace it with the below code
<head>
















Step 2: Whenever you publish a post with script, include the script within the < pre > section
<pre class=”brush: js”>


Your script here


</pre>

Friday, September 30, 2011

C Data type - void

Linguistic meaning of void is nothing. Size of void data type is meaningless question.

What will be output of following c code?
#include<stdio.h>
int main(){
    int size;
    size=sizeof(void);
    printf("%d",size);
    return 0;
}

Output: Compilation error

If we will try to find the size of void data type complier will show an error message “not type allowed”. We cannot use any storage class modifier with void data type so void data type doesn’t reserve any memory space. Hence we cannot declare any variable as of void type i.e.
void num;   //compilation error

Use of void data type

1. To declare generic pointer
2. As a function return type
3. As a function parameter.    

Write a C program to check prime number or not without using recursion

#include<stdio.h>

int isPrime(int);

int main(){

    int num,prime;

    printf("Enter a positive number: ");
    scanf("%d",&num);

    prime = isPrime(num);

   if(prime==1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);

   return 0;
}

int isPrime(int num){

    int i=2;

    while(i<=num/2){
         if(num%i==0)
             return 0;
         else
             i++;
    }

    return 1;
}

Prime number program in c using recursion

#include<stdio.h>

int isPrime(int);

int main(){

    int num,prime;

    printf("Enter a positive number: ");
    scanf("%d",&num);

    prime = isPrime(num);

   if(prime==1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);

   return 0;
}

int isPrime(int num){

    static int i=2;

    if(i<=num/2){
         if(num%i==0)
             return 0;
         else{
             i++;
             isPrime(num);
         }
    }

    return 1;
}

Wednesday, September 28, 2011

C interview questions and answers

1. Write a c program without using any semicolon which output will : Hello word.


Solution: 1
void main(){
    if(printf("Hello world")){
    }
}
Solution: 2
void main(){
    while(!printf("Hello world")){
    }
}
Solution: 3
void main(){
    switch(printf("Hello world")){
    }
}

2. Write a C program to Swap two variables without using third variable.

int main()
{
    int a=5,b=10;
//process one
    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

//process two
    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);
//process three
    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);
  
//process four
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);
  
//process five
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);
    getch();

}

3. What is dangling pointer in c?

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.

Initially:

Later: 


4. What is wild pointer in c ?

A pointer in c which has not been initialized is known as wild pointer.

Example:

(q)What will be output of following c program?

void main(){
int *ptr;
printf("%u\n",ptr);
printf("%d",*ptr);

}

Output: Any address
Garbage value

Here ptr is wild pointer because it has not been initialized.
There is difference between the NULL pointer and wild pointer. Null pointer points the base address of segmentwhile wild pointer doesn’t point any specific memory location.

5. What is the meaning of prototype of a function ?

Prototype of a function
Answer: Declaration of function is known as prototype of a function. Prototype of a function means
(1) What is return type of function?
(2) What parameters are we passing?
(3) For example prototype of printf function is:
int printf(const char *, …);
I.e. its return type is int data type, its first parameter constant character pointer and second parameter is ellipsis i.e. variable number of arguments.


6. What are merits and demerits of array in c?

(a) We can easily access each element of array.
(b) Not necessity to declare two many variables.
(c) Array elements are stored in continuous memory location.

Demerit:
(a) Wastage of memory space. We cannot change size of array at the run time.
(b) It can store only similar type of data.



Friday, April 16, 2010

If a five digit number is input through keyboard ,write a program to print a new number by adding one to each of tis digits.

Note: If a five digit number is input through keyboard ,write a program to print a new number by adding one to each of tis digits for example a number is input is 12391 then the output should be displayed as 23502 .

#include< stdio.h>
#include< conio.h>
int main()
{
long int no,sum=0;
int rem,check=0;
clrscr( );
printf("Enter the required number:");
scanf("%ld",&no);
printf("\nGiven Number: %d",no);
while(no>0) // to store the number in reverse by adding one to each digit
{
rem=no%10;
if(rem!=9)
{
if(check==0) // imp , try to take few minutes here
sum=(10*sum)+(rem+1);
else{
sum=(10*sum)+(rem+2);
check=0;
}
} //if closed
else{
sum=(10*sum)+0;
check=1;
}
no=no/10;
} // while closed

// Afftr adding , again to print the reverse number in origianl form.

no=sum; sum=0;
while(no>0)
{
rem=no%10;
sum=(10*sum)+rem;
no=no/10;
}
printf("\nAfter Adding one: %ld",sum);
getch( );
return 0;
}

Output:
Enter the reuired Number: 12391
Given Number: 12391
After Adding one to each of its digits: 23502

Monday, March 8, 2010

Write a C program to find the greatest common divisor (GCD) of the two given positive integers.

#include< stido.h>
#include< conio.h>
int main( )
{
int a, b, res;
clrscr( );
printf("Enter the two integer values:");
scanf("%d%d", &a, &b);

for( ; ; ) //This is empty for loop.
{
res = a%b;
if( res = = 0) break;
a = b;
b = res;
}
printf("\n The GCF is : %d", res);
getch( );
return 0;
}

Write a C program that prints a given positive integer in reverse order and also sum of the individual digits involved in the given integer.

#include< stdio.h>
#include< conio.h>
int main( )
{
int no, rev=0, rem, sum=0;
clrscr( );
printf("Enter the required integer:");
scanf("%d", &no);
while(no>0)
{
rem = no%10;
rev = (10*rev)+rem;
sum = sum+rem;
no=no/10;
}
printf("\nReverse Number : %d", rev);
printf("\nSum of the individual digits : %d", sum);
getch( );

return 0;
}

Note: If any compile errors, please mail to me..

Write a C program to find the commission on a salesman's total sales

Full Question:
                      The commission on a salesman’s total sales is as follows:
a) If sales <100, then there is no commission.

b) If 100>= sales <=500, then commission = 10% of sales.

c) If sales > 500, then commission = 100+8% of sales above 500

Solution:-

#include< stdio.h>
#include< conio.h>
int main( )
{
int sales;
float comm;
clrscr( );
printf("Enter the total sales:");
scanf("%d", &sales);
if(sales<100)
printf("\nSorry no Commission, please do more sales");
else if(sales>=100 && sales<=500)
printf("\nCommission : %f ", (sales*0.1));
else
{
comm=(sales-500)*0.08;
printf("\nCommission : %f ", (comm+100));
}
getch( );
return 0;
}

Wednesday, February 17, 2010

Write a C program to find the exponential series of 1+x+x2/2!+x3/3!+.......+xn/n!

#include< math.h>
void main( )
{
int x, n, fact, i, j;
float sum=1;
clrscr( );
printf("Enter the 'x' value:");
scanf("%d",&x);
printf("\nEnter the 'n' value:");
scanf("%d",&n);
for(i=1; i< =n ; i++)
{
fact=1;
for( j=i ; j >=1; j--)
fact=fact*j;

sum=sum+(pow(x,i )/ fact);
}
printf("\nSum of the series : %f ",sum);
getch( );
}

Note:- If any errors,, plz mail to me.

Sunday, February 14, 2010

Write a C program to display all Even numbers upto the given range.

#include< stdio.h>
#include< conio.h>
int main( )
{
int x, r;
clrscr( );
printf("Enter the range:");
scanf("%d",&r);
for(x=1; x <=r; x++)
{
if(x%2==0)
printf("%5d", x);
}
getch( );
return 0;
}

Output:-
Enter the range:10
2 4 6 8 10

Tuesday, February 9, 2010

Write a program to compute the sum of the terms 1+3+5+7+9+--------- until the sum>500

#include< stdio.h>
#include< conio.h>
int main()
{
long int i,sum=0;
clrscr();
for(i=1; ;i++)
{
if(i%2!=0)
sum=sum+i;
if(sum>500)
break;
}
printf("\nSum of the 1+3+5+---- until the sum>500 is : %ld",sum);
getch( );
return 0;
}

Write a C program to compute the sum of the terms 1+2+3+.....upto N terms

#include< stdio.h>
#include< conio.h>
int main()
{
int n,i,sum=0;
clrscr();
printf("Enter the 'n' value:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
sum=sum+i;
}
printf("\nSum of the 'n' terms: %d",sum);
getch();
return 0;
}

Monday, January 11, 2010

Write a C program that adds first seven terms of the following series 1/1! +2/2! + 3/3! + ..........

#include< stdio.h>
#include< conio.h>
int main( )
{
int n,i,j,fact;
float sum=0;
clrscr();
for(i=1; i<=7; i++) { fact=1; for(j=i; j>=1; j--) // to find the factorial
fact=fact*j;

sum=sum+(float)i/fact;
}
printf("\nSum : %f",sum);
getch();
return 0;
}

Write a C program to find a peculiar two digit number which is three times the sum of its digits?

example:- 27
=3(2+7)
=3(9)
=27

#include< stdio.h>
#include< conio.h>
int main()
{
int n,sum=0,rem,temp;
clrscr();
printf("Enter any two digit number:");
scanf("%d",&n);
for(temp=n;temp>0;temp=temp/10)
{
rem=temp%10;
sum=sum+rem;
}
if(n==(sum*3))
printf("\nIts a Peculiar two digit number");
else
printf("\nNot a Peculiar two digit number");
getch();
return 0;
}

Thursday, November 26, 2009

Write a C program to print the given alphabet pyramid -2


A B C D E F G F E D C B A
  A B C D E F E D C B A
    A B C D E D C B A
      A B C D C B A
        A B C B A
          A B A
            A 


#include<stdio.h>
#incldue<conio.h>
int main( )
{
  int r,c,askey,sp;
  clrscr( );
  
  for( r=7; r>=1; r-- )
  {
    for(sp=6; sp>=r; sp--)
        printf("  "); //2 spaces

    askey=65;
    for(c=1; c<=r; c++ )
      printf("%2c", askey++ );
    
    for(c=r-1; c>=1; c-- )
       printf("%2c", --askey);
   
   printf("\n");
  }
  getch();
  return 0;
}

Output:-
A B C D E F G F E D C B A
  A B C D E F E D C B A
    A B C D E D C B A
      A B C D C B A
        A B C B A
          A B A
            A
 

Write a C program to print the given alphabet pyramid.

A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
-----------------------------------

#include<stdio.h>
#include<conio.h>
int main( )
{
  int r,c,askey;
  clrscr( );
  
  for( r=7; r>=1; r-- )
  {
    askey=65;
    for(c=1; c<=r; c++ )
      printf("%2c", askey++ );
    askey--; 
    for(c=r; c>=1; c-- )
       printf("%2c", askey--);
   
   printf("\n");
  }
  getch();
  return 0;
}
 
Output:- 
A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

Note:- 
'r' and 'c' means rows and columns .
'askey' variable is for disp. values.

Thursday, November 12, 2009

Write a C program to find the roots of a quadratic equation using Pointers and Functions.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(math.h)
void quadratic_roots(double *,double *, double *);
int main(void)
{
double a, b, c;
clrscr( );
printf("Enter the values of a,b and c :");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\nThe required quadratic equation is : ax^2 + bx + c = 0");
printf("\nGiven Values: a = %lf \n b= %lf \n c= %lf ",a ,b, c);
quadratic_roots(&a,&b,&c);
getch( );
return 0;
}

void quadratic_roots(double *aa, double *bb, double *cc)
{
double root1, root2;
if((*bb)*(*bb)= = 4*(*aa)*(*cc))
{
root1= -(*bb)/2*(*aa);
printf("\nRoots are equal");
printf("\nvalue : %lf", root1);

else if((*bb)*(*bb) > 4*(*aa)*(*cc))
{
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("\nThe roots of the equation are: %lf and %lf ", root1, root2);
}
else
{
printf("\nImaginary roots");
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("roots are %f+i(%f) , %f-i(%f)",r1, r2, r1, r2);
}
}

Wednesday, November 11, 2009

Write a C function that defines two strings as parameters, as follows, and checks whether the second string (str2) is a substring of the first one (str1).


int substring_finder(char* str1, char* str2);
The function should return an integer as follows:
2 if str2 is a substring of str1 and also of same length
o e.g. str1 = "Hello" and str2= "Hello"
1 if str2 is a substring of str1 and of different lengths
o e.g. str1 = "Hello World" and str2= "Hello"
0 if str2 is not a substring of str1
o e.g. str1 = "Hello" and str2= "Helo"



#include(string.h) //  note place '<' & '>' in place of '(' & ')'
#include(stdio.h)
int substring_finder(char *,char *);
int main()
{
  char *str1,*str2;
  int fl;
  clrscr();
  printf("Enter the main string:");
  gets(str1);
  printf("Enter the sub-string:");
  gets(str2);
  fl=substring_finder(str1,str2);

  if(fl==0)
    printf("\n'str2' is not a substring of 'str1'");
  else if(fl==1)
    printf("\n'str2' is a substring of 'str1'");
  else if(fl==2)
    printf("\nBoth are equal");
 

getch();
return 0;
}
int substring_finder(char *str,char *sub)
{
  int flag;
  if(strcmp(str,sub)==0)
     flag=2;
  else if(strstr(str,sub))
     flag=1;
  else
     flag=0;

  return flag;
}



Output:- Enter the main string: Hello world
Enter the sub string: world
'str2' is a substring of 'str1'

Write a C program to merge two unsorted one dimensional arrays in descending order

Write a program to merge two unsorted 1D arrays in descending order
Note: Define the sizes of each array as a constant i.e. your program should run for arrays of any size.



#include(stdio.h)  // note '<' & '>' in place of '(' & ')'
            // size of the array is 20
#define MAX 20
int main()
{
 int f[MAX],s[MAX];
 int m,n; // to read the size of two arrays
 int i,j,temp;
 clrscr();
 printf("Enter the 1st array size(1-20) :");
 scanf("%d",&m);
 printf("Enter the 2nd array size(1-20) :");
 scanf("%d",&n);
 printf("\nEnter the 1st array elements:");
 for(i=0; i< m; i++)
   scanf("%d",&f[i] );


 printf("\nEnter the 2nd array elements:");
 for(j=0; j< n; j++)
   scanf("%d", &s[j] );


 for(j=0; j< n; j++)  // to store 's' elements to 'f'
 {
    f[i]=s[ j];
    i++;
 }
 printf("\nBefore descending, the merged array is:\n");
 for(i=0; i< m+n; i++)
    printf("%5d",f[i] );


 for(i=0; i< m+n; i++)// to arrange the 'f' elements in Descending oder
 {
    for(j=i; j< (m+n)-1; j++)
    {
       if(f[i]< f[ j+1])
       {
     temp=f[i];
     f[i]=f[ j+1];
     f[ j+1]=temp;
       }
    }
 }
 printf("\nAfter descending, the merged array is:\n");
  for(i=0; i< m+n; i++)
    printf("%5d",f[i] );
getch();
 return 0;
}


Output:-


Enter the 1st array size(1-20) :4
Enter the 2nd array size(1-20) :5


Enter the 1st array elements:12
3
45
12


Enter the 2nd array elements:78
5
43
1
23


Before descending, the merged array is:
   12    3   45   12   78    5   43    1   23
After descending, the merged array is:
   78   45   43   23   12   12    5    3    1