Archive for the ‘Programing Question in C’ Category

pring num from 1 to 100(any range) widout using loops

August 29, 2007

#include<iostream>
using namespace std;

void print(int start,int max)
{
if(start<=max)
{
cout<<start<<” “;
print(++start,max);
}
}

int main ()
{
int start,max;
cout<<“\n enter the starting num :”;
cin>>start;
cout<<“\n enter the max no. :”;
cin>>max;
print(start,max);

cout<<endl<<endl;
system(“pause”);
return 0;
}

Generate Fib numbers

August 28, 2007

// recursive version

     int fib( n )
{
if ( n < 2 )
return 1;
else
return fib ( n-1 ) + fib ( n-2 );
}
//iterative version
int fib( n )
{
int f1 =1, f2 = 1;

if ( n < 2 )
return 1;
for ( i = 1; i < N; i++)
{
f = f1 + f2;
f1= f2;
f = f1;
}
return f;
}

Find the factorial of number

August 28, 2007

// recursive version

int Factorial( int Num )
{
if ( num > 0 )
return Num * Factorial ( Num 1 );
else
return 1;
}

// iterative version

int Factorial( int Num )
{
int I
int result = 1;

for ( I= Num; I > 0; I- – )
{
result = result * I;
}

return result;
}

display binary for any integer

August 27, 2007

#include <stdio.h>
#include <stdlib.h>

void
bits ( int i )
{
if ( i / 2 > 0 )
{
bits (i/2);
printf ( “%d”, i%2 );
}
else
printf ( “%d”, i%2 );
}

int
main (void)
{
int decimal = 27;
bits (decimal);
return EXIT_SUCCESS;
}