Tuesday, June 7, 2011

C Program to convert a Decimal number to Decimal Number

#include <stdio.h>

void main()
{
int   num, bnum, dec = 0, base = 1, rem ;

printf(“Enter a binary number(1s and 0s)\n”);
scanf(“%d”, &num);           /*maximum five digits */

bnum = num;

while( num > 0)
{
rem = num % 10;
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
}

printf(“The Binary number is = %d\n”, bnum);
printf(“Its decimal equivalent is =%d\n”, dec);

C Program to convert a Decimal number to Binary Number

#include<stdio.h>
void main()
{
    int i,n1,t,w,a[100],i1=0;
    float n,f2;
   
    clrscr();
    printf("[b]PROGRAM FOR DECIMAL TO BINARY CONVERSION[/b]\n");
    printf("******************************************\n");
    printf("\n\nENTER A NUMBER:");
    scanf("%f",&n);
    w=n;
    f2=w;
   
    if(f2==n)
    {
        printf("");f2=0;
    }
    else
    {
        f2=n-f2;
    }
    n1=n;
   
    for(i=0;n1>=1;i++)
    {
        t=n1%2;
        n1=n1/2;
        i1++;
        a[i]=t;
    }
   
    printf("\n\nBINARY EQVALENT=>");
   
    for(i=(i1-1);i>=0;i--)
    {
        printf("%d",a[i]);
    }
    printf(".");
   
    for(i=1;i<=6;i++)
    {
        f2=f2*2;
        if(f2>=1)
        {
            printf("1");
            f2=f2-1;
        }
        else
        {     printf("0");
        }
    }
    getch();
}

Saturday, June 4, 2011

C Program to check whether a number is a Peterson Number or not

#include<stdio.h>
#include<conio.h>
void main()
{
         int n,c,s=0,m,i,f=1;
         clrscr();
         printf("\n ENTER A VALUE : ");
         scanf("%d",&n);
         m=n;
         while(n>o)
         {
              c=n%10;
              for(i=0;i<c;i++)
                   f=f*i;
              s=s+f;
              f=1;
              n=n/10;
         }
         if(s==m)
              printf("\n THE ENTERED NUMBER IS PETERSON
NUMBER.");
          else
              printf("\n THE ENTERED NUMBER IS NOT A
PETERSON NUMBER.");
          getch();
}

C Program without a Header File

It is possible to write a c program without using a header file .

For this we have to do the following :
We will first have to save the program with the extension .c  . eg: wht.c
After this execute the file. The compiler will itself include the header files.
This will work in the case of C++ also.

Example Problem:
void main()
{
int a,b,c;
printf("Enter the values of a and b ");
scanf("%d %d",&a,&b);
c= a+b;
c++
printf("The value is %d",c);
}

After writing the code , save with .c extension and then execute. 
Goodluck.....................................

C Program to run a program without the main() function....

 #include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()

{
printf(" hello ");
}

Tuesday, April 26, 2011

C Program to print local Date and Time

#include <STDIO.H> 
#include <TIME.H> 
int current_day(void) 
{
 struct tm *local; 
  time_t t; 
 t = time(NULL); 
 local = localtime(&t); 
 return local->tm_wday; 

int main(int argc, char *argv[]) 

  printf("Today: %d\n", current_day()); 
  exit(0); 
}
 

Friday, April 1, 2011

C Program for bisection method........!


#include[stdio.h] 
#include[math.h] 
#define epsln 1e-6 
void main() 

double g1,g2,g,v,v1,v2,dx; 
int found,converged,i; 
found=0; 
printf(" enter the first guess\n"); 
scanf("%lf",&g1); 
v1=g1*g1*g1-15; 
printf("value 1 is %lf\n",v1); 


while (found==0) 

printf("enter the second guess\n"); 
scanf("%lf",&g2); 
v2=g2*g2*g2-15; 
printf(" value 2 is %lf\n",v2); 
if (v1*v2>0) 
{found=0;} 
else 
found=1; 

printf("right guess\n"); 
i=1; 


while (converged==0) 

printf("\n iteration=%d\n",i); 
g=(g1+g2)/2; 
printf("new guess is %lf\n",g); 
v=g*g*g-15; 
printf("new value is%lf\n",v); 
if (v*v1>0) 

g1=g; 
printf("the next guess is %lf\n",g); 
dx=(g1-g2)/g1; 

else 

g2=g; 
printf("the next guess is %lf\n",g); 
dx=(g1-g2)/g1; 

if (fabs(dx)< epsln )
{
converged=1;

i=i+1; 

printf("\nth calculated value is %lf\n",v); 
}