About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Monday, September 2, 2013

C Program to Calculate Addition of All Elements in Array

#include<stdio.h>
#include<conio.h>

void main()
{
 int i,a[50],sum,n;
 printf("Enter no of elements :");
 scanf("%d",&n);

 /* Reading values into Array */

 printf("Enter the values :");
 for(i=0;i < n;i++)
 scanf("%d",&a[i]);

 /* computation of total */

 sum=0;
 for(i=0;i < n;i++)
 sum=sum+a[i];

 /* printing of all elements of array */

 for(i=0;i < n;i++)
 printf("a[%d]=%d",i,a[i]);

 /* printing of total */

 printf("Sum=%d",sum);
}

C Program to Delete duplicate elements from an array

#include<stdio.h>
#include<conio.h>
main()
{
int a[20],i,j,k,n;
clrscr();

printf("Enter array size : ");
scanf("%d",&n);

printf("Accept Numbers : ",n);
for(i=0;i<n;i++)
 scanf("%d",&a[i]);

clrscr();

printf("Original array is : ");
for(i=0;i<n;i++)
 printf(" %d",a[i]);

printf("Updated array is  : ");
for(i=0;i<n;i++)
{
   for(j=i+1;j<n;)
   {
      if(a[j]==a[i])
      {
         for(k=j;k<n;k++)
             a[k]=a[k+1];
          n--;
      }
      else
         j++;
   }
}

for(i=0;i<n;i++)
    printf("%d ",a[i]);
getch();
}

Output :
Enter array size : 5
Accept Numbers : 1 2 2 3 4
Original array is : 1 2 2 3 4
Updated array is  : 1 2 3 4

Write a C program using pointers to read in an array of integers and print its elements in reverse order

#include<stdio.h>
#include<conio.h>
#define MAX 30

void main()
{
int size,i,arr[MAX];
int *ptr;
clrscr();

ptr=&arr[0];

printf("Enter the size of array : ");
scanf("%d",&size);

printf("Enter %d integers into array:n",size);
   for(i=0;i<size;i++)
   {
   scanf("%d",ptr);
   ptr++;
   }

ptr=&arr[size-1];

printf("Elements of array in reverse order are:n");

 for(i=size-1;i>=0;i--)
   {
   printf("nElement%d is %d :",i,*ptr);
   ptr--;
   }

getch();
}
Output :
Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11

Program to read integers into an array and reversing them using pointers Explanation :

  1. We have declared one pointer variable and one array.
int size,i,arr[MAX];
int *ptr;
  1. Address of first element of array is stored inside pointer variable.
ptr=&arr[0];
  1. Accept Size of an Array.
printf("Enter the size of array : ");
scanf("%d",&size);
  1. Now we have accepted element one by one using for loop and scanf statement .
printf("Enter %d integers into array:n",size);
   for(i=0;i<size;i++)
   {
   scanf("%d",ptr);
   ptr++;
   }
  1. Increment pointer variable so that it will then point to next element of array.
  2. After accepting all elements store address of last element inside pointer variable.
ptr=&arr[size-1];
  1. Again using reverse for loop and printf statement print an array.
for(i=size-1;i>=0;i--)
   {
   printf("nElement%d is %d :",i,*ptr);
   ptr--;
   }

C Program to Implement Stack Operations Using Array

Program for implementing a stack using arrays.It involves
various operations such as push,pop,stack empty,stack full and
display.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
/* stack structure*/
struct stack  {
          int s[size];
          int top;
           }st;
//-------------------------------------
int stfull()
{
 if(st.top>=size-1)
    return 1;
 else
    return 0;
}
//-------------------------------------
void push(int item)
{
 st.top++;
 st.s[st.top] =item;
}
//-------------------------------------
int stempty()
{
 if(st.top==-1)
    return 1;
 else
    return 0;
}
//-------------------------------------
int pop()
 {
 int item;
 item=st.s[st.top];
 st.top--;
 return(item);
 }
//-------------------------------------
void display()
{
 int i;
 if(stempty())
    printf("n Stack Is Empty!");
 else
  {
  for(i=st.top;i>=0;i--)
     printf("n%d",st.s[i]);
  }
}
//-------------------------------------
void main(void)
{
int item,choice;
char ans;
st.top=-1;
clrscr();
printf("ntt Implementation Of Stack");
do
{
 printf("n Main Menu");
 printf("n1.Pushn2.Popn3.Displayn4.exit");
 printf("n Enter Your Choice");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
        printf("n Enter The item to be pushed");
        scanf("%d",&item);
        if(stfull())
            printf("n Stack is Full!");
        else
            push(item);
        break;
  case 2:
        if(stempty())
            printf("n Empty stack!Underflow !!");
        else
          {
          item=pop();
          printf("n The popped element is %d",item);
          }
        break;
  case 3:
        display();
        break;
  case 4:
        exit(0);
 }
 printf("n Do You want To Continue?");
 ans=getche();
}while(ans =='Y'||ans =='y');
getch();
}

Explanation of the C Porgram : Stack Using Array

Step 1 : Declare One Stack Structure

#define size 5

struct stack
{
     int s[size];
     int top;
}st;
  1. We have created ‘stack’ structure.
  2. We have array of elements having size ‘size’
  3. To keep track of Topmost element we have declared top as structure member.

Step 2 : Push/Pop Operation

While pushing remember one thing in mind that we are incrementing the top and then adding element. and while removing or poping the element, we are firstly removing the element and then decrementing the top.
void push(int item) {
    st.top++;
    st.s[st.top] =item;
}

C Program to find Factorial of Number without using function

#include<stdio.h>
#include<conio.h>
void main()
{
int i,number,factorial;
printf("Enter the number : ");
scanf("%d",&n);

factorial = 1;
for(i=1;i<=n;i++)
      factorial = factorial * i;

printf("Factorial of %d is %d",n,factorial );
getch();
}
Output :
Enter the number : 5
Factorial of 5 is 120

Explanation of Program :

Before Explaining the program let us see how factorial is calculated -
Factorial of 5 = 5!
               = 5 * 4!
               = 5 * 4  * 3!
               = 5 * 4  * 3 * 2!
               = 5 * 4  * 3 * 2 * 1!
               = 5 * 4  * 3 * 2 * 1 * 0!
               = 5 * 4  * 3 * 2 * 1 * 1
               = 120
Firstly accept the number whose factorial is to be found.
printf("nEnter the number : ");
scanf("%d",&n);
We have to iterate from 1 to (n-1) using for/while/do-while loop. In each iteration we are going to multiply the current iteration number and result.
for(i=1;i<=n;i++)
      factorial = factorial * i;

Some Precautions to be taken :

Precaution 1 : Initialize ‘factorial’ variable

factorial = 1;
before going inside loop we must initialize factorial variable to 1 since by default each c variable have garbage value. If we forgot to initialize variable then garbage value will be multiplied and you will get garbage value as output.

Precaution 2 : For loop must start with 1

Suppose by mistake we write following statement -
for(i=0;i<=n;i++)
      factorial = factorial * i;
then in the very first iteration we will get factorial = 0 and in all successive iteration we will get result as 0 since anything multiplied by Zero is Zero

Precaution 3 : Try to accept lower value to calculate factorial

We have 2 bytes to store the integer in Borland C++ compiler, so we can have maximum limit upto certain thousand. Whenever we try to accept value greater than 20 we will get factorial overflow.

Program to Print All ASCII Value Table in C Programming

#include
#include
void main()
{
int i=0;
char ch;
clrscr();
for(i=0;i<256 1="" c="" ch="ch" i="" pre="" printf="">
Output :
Å É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » ░ ▒ ▓ │ ┤ ╡ ╢
╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐
▀ α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■     ☺ ☻ ♥ ♦ ♣ ♠
♫ ☼ ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ ← ∟ ↔ ▲ ▼ ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6
7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] ^
_ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ ⌂ Ç ü é â ä à å
ç ê ë è ï î ì Ä
Above Fig. Clearly shows all ASCII Values.

Program to Check Whether Given Number is Perfect Or Not ?

#include
int main()
{
int n,i=1,sum=0;
  printf("Enter a number: ");
  scanf("%d",&n);
  while(i