12.24.2011

Middle level Language

Q. Why C is called middle level language?

Ans.

It is common question in interview that why C language is called a Middle level language? Before find out this answer, you should familiar with following term:
There are following reason that C is called Middle Level Language as:

Low Level Language

LLL is stand for Low Level Language. These language also known Machine Language. Definition and some Basic features of low level Language is as following:
  • The language whose design is governed by the circuitry and the structure of the machine is known as Machine Language.
  •  These language is difficult to learn and use.
  • These language is machine-dependent.
  • In these type language program execution is faster compere high level language because code is directed accepted by the machine, no needs to translator.
  • Example of Low level Language is the Assembly Language.

High Level Language


HLL stands for High Level Language. Basic features of HLL is as  following:
  • These language are particularly oriented towards describing the procedures for solving the problem in a concise, precise and unambiguous manner. 
  • HLL are programming language which is user friendly, easy to learn and writing program, to extent platform independent.
  • Hll language are machine independence so its require Compiler or Translator to translate these code in machine language.
  • Examples of high level languages are C, C++, Java,FORTRAN etc.

Convert string UpperCase

Q. Write a string to accept string through keyboard and convert it Uppercase letter.

Ans.

/*program to convert a string in upper case letter*/
#include<stdio.h>
#include<string.h>
int main()
{

Convert string LowerCase

Q. Write a string to accept string through keyboard and convert it Lower Case letter.

Ans.

/*program to convert a string in lower case letter*/
#include<stdio.h>
#include<string.h>
int main()
{

12.23.2011

Number pattern

Q. Write a C program to display the following pattern:
 1
 2   3
 4   5    6
 7   8    9    10
11  12   13    14  15

Ans.

/*program to design the above pattern*/
#include<stdio.h>
#include<conio.h>
void main()
{

12.17.2011

goto statement

The goto statement is used to alter the normal sequence of program instructions by transferring the control to some other portion of the program.
The syntax is as follows:
goto label;
Here, label is an identifier that is used to label the statement to which control will be transferred. The targeted statement must be preceded by the unique label followed by colon.
label: statement;
It is recommended that as possible as skip the goto statement because when we use goto then we can never be sure how we got to a certain point in our code. They obscure the flow of control.

Note:- goto can never be used to jump into the loop from outside and it should be preferably used for forward jump.

Let us consider a program to illustrate goto and label statement:

Selection sorting

Q. Write a C program to to sort a list of elements using selection sort method.

Ans.

/*program to demonstration of selection method*/
#include<stdio.h>
#include<conio.h>
#define SIZE 10
int main()
{

Bubble sorting

Q. Write a program to accept 10 numbers from user and sort list using Bubble sorting method.

Ans.
Logic of bubble sorting as follows:


In bubble sorting steps:
1. Start from left hand side
2. Compare first two numbers
3. If first_number > second_number than swap both number position. And if first_number < second_number than these compare next two numbers i.e. second_number and third_number.
4. Step-3 process repeat until there are no more numbers left to compared.
5. Bubble sorting completed.

An example on bubble sort.
To understand logic of Bubble Sorting, lets take random numbers:

6  3  7  1  4  5  2

First iteration
6  3  7  1  4  5  2

6  7  1  4  5  2

3  6  7  1  4  5  2

3  6  1  7  4  5  2

3  6  1  4  7  5  2

3  6  1  4  5  7  2

3  6  1  4  5  2  7

Second iteration
3  6  1  4  5  2  7

6  1  4  5  2  7

3  1  6  4  5  2  7
3  1  4  6  5  2  7

3  1  4  5  6  2  7

3  1  4  5  2  6  7

12.10.2011

Palindrome function

Q. Write a C program to check whether a string is palindrome or not using function.

Ans.

/*Program to check whether a string is palindrome or not using function*/
#include<stdio.h>
#include<conio.h>
char string_palin(char str[]);
int main()
{

12.02.2011

String Triangle

Q. Write a program to display the string INDIA in the following fashion:

I
IN
IND
INDI
INDIA
INDIA
INDI
IND
IN
I

Ans.

/*program to display the string as given fashion*/
#include<stdio.h>
#include<conio.h>
int main()
{

11.25.2011

Series Display,Sum

Q. Write a C program to display the given number series and calculate sum of that series.

1  2  4  7  11  16  22 . . . . . . . . .n

Ans.

/*program to display the given series and calculating sum*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i,n;
 int sum=0,ser=1;
 printf("Enter total elements in the series:");
 scanf("%d",&n);
 for(i=1; i<=n; i++)
 {
    printf(" %d",ser);
    sum=sum+ser;
    ser=ser+i;
 }
 printf("\nSum of series = %d",sum);
 getch();
 return 0;
}

Output:-
Enter number of elements in the series : 7

 1  2  4  7  11  16  22
 Sum of series = 63

11.16.2011

Concatenate two strings

Q. Write a program to concatenate two strings without using the strcat() function.

Ans.

/*program to concatenate two strings without using the strcat() function*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 int x=0,y=0,z=0;
 char str1[20],str2[20];
 char add[40];
 printf("Enter first string : ");
 gets(str1);
 printf("\nEnter second string : ");
 gets(str2);
 while(str1[x]!=NULL)
 {
   add[z]=str1[x];
   x++;
   z++;
 }
 while(str2[y]!=NULL)
 {
   add[z]=str1[y];
   y++;
   z++;
 }
 add[z]=NULL;
 puts(add);
 getch();
 return 0;
}

/***************Output******************

Enter first string : Input
Enter second string : Device
InputDevice


*****************************************/

11.09.2011

Character triangle

Q.  Write a C program to print the following triangle.
A






A B A




A B C B A


A B C D C B A
A B C D E D C B A

Ans.


#include<stdio.h>
#include<conio.h>
int main()
{
 char ch,r,c;
 int sp;
 printf("Enter character : ");
 scanf("%c",&ch);
 if(ch>='a' && ch<='z')
   ch=ch-32;
 for(r='A'ch>=r; r++)
 {
   for(sp=ch-r; sp>=1; sp--)
       printf(" ");
   for(c='A'r>=c; c++)
       printf("%c",c);
   for(c=r-1; c>='A'; c--)
       printf("%c",c);
   printf("\n");
 }
 getch();
 return 0;
}

/*************** OUTPUT ******************
Enter character : E

A
A B A
A B C B A
A B C D C B A
A B C D E D C B A
*************************************/

11.06.2011

Decimal to Hexadecimal

Q.  Write a program in C to convert a decimal number to its hexadecimal equivalent.

Ans.

/*c program for convert decimal value to hexadecimal value*/
#include<stdio.h>
#include<conio.h>
void decTohex(int num);
int main()
{
 int n;
 printf("Enter number : ");
 scanf("%d",&n);
 decTohex(n);
 getch();
 return 0;
}
void decTohex(int num)
{
 int i=0,j=0,rem[5];
 for( ; num>15; num=num/16)
 {
   rem[i]=num%16;
   i++;
   j++;
 }
 rem[i]=num;
 for(i=j; i>=0; --i)
 {
   if(rem[i]==10)
      printf("A");
   else if(rem[i]==11)
      printf("B");

   else if(rem[i]==12)
      printf("C");

   else if(rem[i]==13)
      printf("D");

   else if(rem[i]==14)
      printf("E");

   else if(rem[i]==15)
      printf("F");

   else
      printf("%d",rem[i]);
 }
}



/*****************OUTPUT****************


Enter number : 1785
6F9


**************************************/

11.03.2011

Algorithm

An algorithm is a finite set of steps defining the solution of a particular problem. An algorithm can be expressed in human readable language like as English. Algorithm is language depended, well structured and detailed.

Rules for constructing an Algorithm

When you are going to create an algorithms, keep following point in mind as:
  • Input: There should be zero or more values which are to be supplied.
  • Output: At least one result is to be produced.
  • Definiteness: Each step must be clear and unambiguous.
  • Finiteness: If we trace the steps of an algorithm, then for all cases, the algorithm must terminate after a finite number of steps.
  • Effectiveness: Each step must be sufficiently basic that a person using only paper and pencil can in principle carry it out. In addition, not only each step is definite, it must also be feasible.
  • Comment Session: Comment is additional info of program for easily modification. In algorithm comment would be appear between two square bracket []. For example: [ this is a comment of an algorithm ]
The demonstration of for loop algorithm through calculate factorial number C program/algorithm:
Algorithm for calculate factorial number
Figure: Algorithm of demonstration of how to
write "for loop" in algorithm factorial program

Let's understand to algorithm by example:

Q. Write algorithm to calculate the sum and average of two numbers.

Ans.

[procedure for calculate sum and average of two numbers]
Step1 : Start
Step2 : Read two numbers n,m
Step3 : Calculate sum=n+m
Step4 : Calculate avg=sum/2
Step5 : Print sum,avg
Step5 : Stop
[End of procedure for calculate sum and average of two numbers]


Q. Write an algorithm to convert a decimal number into binary.

Ans.

[procedure for convert decimal to binary number]
Step1 : Start
Step2 : Read number num
Step3 : Set x=1
Step4 : B(x)= num MOD 2
Step5 : num=num/2
Step6 : If num is equal to 0 then goto step8
Step7 : x=x+1
Step8 : goto step3
Step9 : Print B(x)
Step10 : x=x-1
Step11 : If x is greater than 0 then goto step8
step12 : Stop