Monday 1 July 2013

Write a program using user defined function to print the larger and the smaller integer

#include<stdio.h>
#include<conio.h>
void main()
{
int max_num(int a[],int n);
int max,i,n;
int a[50];
printf("Enter n number:");
scanf("%d",&n);
printf("Enter the numbers:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=max_num(a,n);
printf("The largest number is %d",max);
getch();
}
int max_num(int a[],int n)
{
int i,m=0;
for(i=0;i<n;i++)
{
if(a[i]>m)
m=a[i];
}
return m;
}

Sunday 30 June 2013

Write a program using user defined function to print all the Armstrong numbers between 1 and 500.

01.
#include<stdio.h>
main()
{
int N, temp, N1, N2, N3;
printf("All Armstrong numbers between 1 and 500:\n\n");
N = 001;
while (N <= 500)
{
N1 = N - ((N / 10) * 10);
N2 = (N / 10) - ((N / 100) * 10);
N3 = (N / 100) - ((N / 1000) * 10);
temp = (N1*N1*N1) + (N2*N2*N2) + (N3*N3*N3);
if (temp == N)
{
printf("\nAmstrong Number:%d", temp);
}
N++;
}
getch ();
}


02.


#include<stdio.h>
int main(){
    int num,r,sum,temp;

    for(num=1;num<=500;num++){
         temp=num;
         sum = 0;

         while(temp!=0){
             r=temp%10;
             temp=temp/10;
             sum=sum+(r*r*r);
         }
         if(sum==num)
             printf("%d ",num);
    }

    return 0;
}

Write a user defined function to compute xy.x and y should be integer but the function should return a double value (since y may be negative

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int x,y;
    double power();
    printf("Enter value of x : ");
    scanf("%d",&x);
    printf("Enter value of y : ");
    scanf("%d",&y);
    printf("%d to power %d is = %f\n",x,y,power(x,y));
    getch();
}
double power(x,y)
int x,y;
{
    double p;
    p=1.0;
    if(y>=0)
        while(y--)
            p*=x;
    else while(y++)
            p/=x;
    return(p);
}

Write a program using a recursive function to find the gcd (greatest common divisor) of two given numbers.

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

int gcd ( int a, int b ) 
{
    int r;
    if ( ( r = a%b ) == 0 )
       return b;
    else
       return gcd ( b , r );
}
void main(void){
   int p,q;
   printf("\n Enter two numbers: ");
   scanf("%d %d",&p,&q);
   printf("\n The gcd is  %d ",gcd(p,q));
   getch( );
}

Write a program using a recursive function & LOOP to find the nth Fibonacci number.

01.

#include<stdio.h>
#include<conio.h>
int fibo(int n)
{
if(n==1||n==0)
return n;
else
return(fibo(n-1)+fibo(n-2));
}
int main()
{
int n,i;
puts("enter any number:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%d\t",fibo(i));
}
}



02.

#include<stdio.h>
#include<conio.h>
int Fib(int x, int range);

int main(){
int r;
printf("Enter the MAx range for Fibonacci Series:\n");
scanf("%d", &r);
Fib(r,r);
printf("\n");
}
Fib(int x, int range )
{
static int sum=0,pre=0,p=1;
if (sum>=range)
return 0;
else
{
if (pre==0 && p==1 && sum==0)
{sum=pre+p;
printf("%d %d %d", pre,p,sum);
}
else
{ pre=p;
p=sum;
sum=pre+p;
printf(" %d ", sum);
}
}
Fib(sum,range);
}






Using LOOP

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

main()
{
   int n, first = 0, second = 1, next, c;
   printf("Enter the number of terms\n");
   scanf("%d",&n);

   printf("First %d terms of fibonacci series are :\n",n);

   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }

   getch();
   return 0;
   }

Friday 28 June 2013

Write a C program to find the value of nCr n and r should be taken from the keyboard.




#include<stdio.h>
int main(){
  int n,r,ncr;
  printf("Enter any two numbers->");
  scanf("%d %d",&n,&r);
  ncr=fact(n)/(fact(r)*fact(n-r));
  printf("The NCR factor of %d and %d is %d",n,r,ncr);
  return 0;
}
 int fact(int n){
  int i=1;
  while(n!=0){
      i=i*n;
      n--;
  }
  return i;
 }



Algorithm:

In the mathematics nCr has defined as
nCr = n! /((n-r)!r!)

Write a C program to find all the factors of a given integer.

#include<stdio.h>
#include<conio.h>
void main( )
{
int no,x;
printf("Enter the required number:");
scanf("%d",&no);
printf("\nThe factors are:");
for(x=1; x<=no; x++)
{
if(no%x==0)
printf("\n%d",x);
}
getch( );
}