Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Showing posts with label MSBTE. Show all posts
Showing posts with label MSBTE. Show all posts

Thursday, 24 October 2013

[C Program] for Implementation of Insertion Sort

//Program for insertion sort

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

//Insertion fuction
void insertion(int a[10],int n)
{
int temp,i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[j]>a[i])
{
temp=a[j];
a[j]=a[i];
for(k=i;k>j;k--)
{
a[k]=a[k-1];
}
a[k+1]=temp;
}
}
}
}

//Main function
void main()
{
int a[10],i,n;
clrscr();
printf("Enter the array size:");
scanf("%d",&n);
printf("\nEnter the elements into array:");
for(i=0;i<n;i++)
{
scanf("%d\t",&a[i]);
}
printf("\nEntered array:  ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
insertion(a,n);
printf("\n\nSorted list:    ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
//end of main

/*Output

Enter the array size:6
                                                                             
Enter the elements into array:31 56 80 111 65 71

Entered array:  31      56      80      111     65      71                    
                                                                             
Sorted list:    31      56      65      71      80      111 */

Sunday, 8 September 2013

Function with Parameters and return Value in C programming

In C++, a parameter can be passed by:

  1. value,
  2. reference, or
  3. const-reference

Each parameter's mode is determined by the way it is specified in the function's header (the mode is the same for all calls to the function). For example:
void f( int a, int &b, const int &c );
Parameter a is a value parameter, b is a reference parameter, and c is a const-reference parameter.

Value Parameters

When a parameter is passed by value, a copy of the parameter is made. Therefore, changes made to the formal parameter by the called function have no effect on the corresponding actual parameter.
Program

#include<stdio.h>
#include<conio.h>
void main()
{
float r ,a;
float cir(float);
clrscr();
printf("Enter the Radius=\n");
scanf("%f",&r);
a=cir(r);
printf("\nThe area is %f",a);
getch();
}
float cir(float p)
{
return(3.142*p*p);
}

Output :
Enter The Radius
4
The Area =50.240002

Saturday, 7 September 2013

C Program to Display Fibonacci Series Numbers with Simple Example and Explanation

The Fibonacci Sequence is the series of numbers:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The next number is found by adding up the two numbers before it.

The 2 is found by adding the two numbers before it (1+1)
Similarly, the 3 is found by adding the two numbers before it (1+2),
And the 5 is (2+3),
and so on!
Example: the next number in the sequence above would be 21+34 = 55
It is that simple!

Here is a longer list:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, ...

Program

#include<stdio.h>
#include<conio.h>
 void main()
{
int f1,f2,f3=0,n=0;
clrscr();
f1=0;
f2=1;
printf("enter the num");
scanf("%d",&n);

while(f3<n)
{
f3=f1+f2;
printf("%d\n",f3);
f1=f2;
f2=f3;
}
printf("Fibonacci series=%d",f3);
getch();
}

Output:

Enter the number 4
1
2
3
5
Fibonacci series=5

Program Inter Conversion of Decimal, Binary, Octal and Hexadecimal numbers [C Programming]

There are infinite ways to represent a number. The four commonly associated with modern computers and digital electronics are: decimal, binary, octal, and hexadecimal.

Decimal (base 10) is the way most human beings represent numbers. Decimal is sometimes abbreviated as dec.

Decimal counting goes:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, and so on.
Binary (base 2) is the natural way most digital circuits represent and manipulate numbers. (Common misspellings are “bianary”, “bienary”, or “binery”.) Binary numbers are sometimes represented by preceding the value with '0b', as in 0b1011. Binary is sometimes abbreviated as bin.

Binary counting goes:
0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000, 10001, and so on.
Octal (base 8) was previously a popular choice for representing digital circuit numbers in a form that is more compact than binary. Octal is sometimes abbreviated as oct.

Octal counting goes:
0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21, and so on.
Hexadecimal (base 16) is currently the most popular choice for representing digital circuit numbers in a form that is more compact than binary. (Common misspellings are “hexdecimal”, “hexidecimal”, “hexedecimal”, or “hexodecimal”.) Hexadecimal numbers are sometimes represented by preceding the value with '0x', as in 0x1B84. Hexadecimal is sometimes abbreviated as hex.

Hexadecimal counting goes:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, and so on.
All four number systems are equally capable of representing any number. Furthermore, a number can be perfectly converted between the various number systems without any loss of numeric value.

At first blush, it seems like using any number system other than human-centric decimal is complicated and unnecessary. However, since the job of electrical and software engineers is to work with digital circuits, engineers require number systems that can best transfer information between the human world and the digital circuit world.

It turns out that the way in which a number is represented can make it easier for the engineer to perceive the meaning of the number as it applies to a digital circuit. In other words, the appropriate number system can actually make things less complicated.

Program

#include<stdio.h>
#include<conio.h>
#include<math.h>
int bintodec(int,int);
char digittochar(int digit)
{
if(digit<=0 && digit<=9)
return digit+48;
else
{
if(digit==10)
return'A';
else if(digit==11)
return'B';
else if(digit==12)
return'C';
else if(digit==13)
return'D';
else if(digit==14)
return'E';
else
return'F';
}
}
void rev(char a[])
{
char temp;
int i,j;
for(j=0;a[j]!='\0';j++);
j--;
      for(i=0;i<j;i++,j--)
       {
temp=a[i];
a[i]=a[j];
a[j]=temp;
       }
  }
  void dectohex(int, char[],char[],int);
  int main()
    {
      char hex[20];
      int base,choice,flag,res=0;
      int num;
      clrscr();
      while(1)
{
flag=1;
printf("Press:\n");
printf("\n\n\t1:Decimal to hexadecimal");
printf("\n\n\t2:Decimal to octal");
printf("\n\n\t3:Deciaml to binary");
printf("\n\n\t4: Binary to decimal");
printf("\n\n\t0:exit");
choice=getche();
switch(choice)
{
  case'1':base=16;
  break;
  case'2':base=8;
  break;
  case'3':base=2;
  break;
  case'4':base=10;
  break;
  case'0':exit(0);
  break;
  default:flag=0;
}

if(flag)
 {
   printf("Enter the number");
   scanf("%d",&num);
   dectohex(num,hex,base);
   if(base==16)
   printf("\nHexadecimal=%s",hex);
   else if(base==8)
   printf("\nOctal=%s",hex);
   else if(base==2)
   printf("\nBinart=%s",hex);
   else
   printf("\nDecimmal=%d\n",bintodec(num,base));
 }
      }

    void dectohex(int num  ,char a[] ,int base )
      {
int i=0,term;
while (num)
{
 term=num% base;
 a[i]=digittochar(term);
 num=num/base;
 i++;
}
a[i]='\0';
rev(a);
      }

      int bintodec(int num,int base)
       {
int i,temp,dec=0;
for(i=0;num>0;i++)
   {
      temp=num%10;
dec=pow(2,i)*temp+dec;
num=num/10;
   }
       return();
    }
}

Command-line arguments in the C language [Simple Example and Explanation]


The C language provides a method to pass parameters to the main() function. This is typically accomplished by specifying arguments on the operating system command line (console). 

The prototype for main() looks like:

int main(int argc, char *argv[]) 
 { 
 … 
 } 

There are two parameters passed to main(). The first parameter is the number of items on the command line (int argc). Each argument on the command line is separated by one or more spaces, and the operating system places each argument directly into its own null-terminated string. The second parameter passed to main() is an array of pointers to the character strings containing each argument (char *argv[]). 

For example, at the command prompt: 

test_prog 1 apple orange 4096.0 

There are 5 items on the command line, so the operating system will set argc=5 . The parameter argv is a pointer to an array of pointers to strings of characters, such that: 
argv[0] is a pointer to the string “test_prog” 
argv[1] is a pointer to the string “1” 
argv[2] is a pointer to the string “apple” 
argv[3] is a pointer to the string “orange” 
and 
argv[4] is a pointer to the string “4096.0”

Program:

//progam for file handling with command line argument//


#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{
FILE *fs,*ft;
char ch;
if(argc!=3)
{
puts("Improper number of arguments");
exit(0);
}
fs=open(argv[1],"r");
if(fs==NULL)
{
puts("can not open sourse file ");
exit(0);
}
ft=fopen(argv[2],"w");
if(fs==NULL)
{
puts("can not open target file");
fclose(fs);
exit(0);
}
while(1)
{
 ch=fgetc(fs);
 if(ch==EOF)
   break;
 else
   fputc(ch,ft);
}
fclose(fs);
fclose(ft);
}

Friday, 6 September 2013

Simple Example with Explanation For Call By Reference Function in C Programming [With Output]

If data is passed by reference, a pointer to the data is copied instead of the actual variable as is done in a call by value. Because a pointer is copied, if the value at that pointers address is changed in the function, the value is also changed in main(). Let’s take a look at a code example:
Program

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
void swap(int *,int *);
clrscr();
puts("Enter the values of x & y \n");
scanf("%d%d",&x,&y);
swap(&x,&y);
printf("X=%d \ny=%d",x,y);
getch();
}
void swap(int *a,int *b)
{
int z;
z=*a;
*a=*b;
*b=z;
}

The output of this call by reference source code example will look like this:

Output:

Enter The value of x and y
2
4
X=4
Y=2

Let’s explain what is happening in this source code example. We start with an integer b that has the value 10. The function call_by_reference() is called and the address of the variable b is passed to this function. Inside the function there is some before and after print statement done and there is 10 added to the value at the memory pointed by y. Therefore at the end of the function the value is 20. Then in main() we again print the variable b and as you can see the value is changed (as expected) to 20.

Simple C Program with example to Print Armstrong Number [With Output]

This C Program print armstrong number from 1 to 1000. An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself. Hence 153 because 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.

Here is source code of the C Program to print armstrong number from 1 to 1000.

The C program is successfully compiled and run on a Linux system. The program output is also shown below.


Program:

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

void main()
{
 int r,i,sum=0;
 int n,temp=0;
 clrscr();
 printf("Enter the number\n");
 scanf("%d",&n);
 temp=n;
 while(n>0)
 {
   r=r%10;
   sum=sum+(r*r*r);
   n=n/10;
 }
 if(temp==sum)
  {
    printf("Number is not Armstrong");
  }
 else
  {
    printf("Number is  Armstrong");
  }
 getch();
}

Output:
Enter The number
153
Number Is armstrong

C Program to Find the Area and Perimeter of Rectangle and Square [With Output]

Here is a simple program to find the area of the rectangle and square. Also the below program will find the perimeter of the rectangle.
Program

  #include<stdio.h>
  #include<conio.h>
  void main()
   {
    int l,,area,peri,sqr,side;
    clrscr();
    printf("\nEnter the length, width of rectangle & side of Square");
    scanf("%d%d%d",&l,&b,&side);
    area=l*b;
    peri=2*l+2*b;
    sqr=4*sqr;
    printf("\nArea of Rectangle=%d",area);
    printf("\n Perimeter=%d",peri);
    printf("\nSquare=%d",sqr);
    getch();
    }

Output:

Enter the length, width of rectangle & side of Square
3
5
6
Area of rectangle=15
Perimeter=16
Square=24

Simple Example and Explanation of Recursion in C Programming

Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to "repeat the process". This makes it sound very similar to a loop because it repeats the same code, and in some ways it is similar to looping. On the other hand, recursion makes it easier to express ideas in which the result of the recursive call is necessary to complete the task. Of course, it must be possible for the "process" to sometimes be completed without the recursive call. One simple example is the idea of building a wall that is ten feet high; if I want to build a ten foot high wall, then I will first build a 9 foot high wall, and then add an extra foot of bricks. Conceptually, this is like saying the "build wall" function takes a height and if that height is greater than one, first calls itself to build a lower wall, and then adds one a foot of bricks. 


Program

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

int sum(int n)
 {
  if(n==0)
   return n;
   else
    return n%10+sum(n/10);
  }
 void main()
  {
    int num;
    clrscr();
    printf("Enter the any digit of integer number");
    scanf("%d",&num);
    printf("sum of digit =%d",sum(num));
    getch();
  }

[Write a] C Program to Print the Transpose of Matrix

This c program prints transpose of a matrix. It is obtained by interchanging rows and columns of a matrix. For example if a matrix is
1 2
3 4
5 6
then transpose of above matrix will be
1 3 5
2 4 6
When we transpose a matrix then the order of matrix changes, but for a square matrix order remains same.



Program

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

   void main()
    {
      int m1[10][10],tr[10][10],i,j,r,c;
      clrscr();
      printf("\n How many rows & columns in the matrix");
      scanf("%d%d",&r,&c);
      puts("\n Enter the elements:");
      for(i=0;i<r;i++)
for(j=0;j<c;j++)
 {
   scanf("%d",&m1[i][j]);
   tr[j][i]=m1[i][j];
 }
printf("\nThe transpose is:\n");
for(i=0;i<c;i++)
 {
   for(j=0;j<c;j++)
    printf("%d",tr[i][j]);
    printf("\n");
 }
printf("\n");
getch();
      }

Output:
How many rows and columns of matrix
2
3
Enter the elements 
1
2
3
4
4
5
The transpose is:
140
240
340

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine