Powered By Blogger

Friday, April 27, 2012

Program : 7


PROGRAM - 7

a)  Write a program to calculate the following: 
Sum = 1-x2 /2!+x4 /4!-…..+x10 /10!
Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
       double sum=1,x,f=1,j,i,p,k=1;
       clrscr();
       printf("enter the value of x");
       scanf("%lf",&x);
       for(i=2;i<=10;i+=2)
       {
        f=1;
        for(j=2;j<=i;j++)
        {
           p=pow(x,i);
           f=f*j;
        }
        k=-k;
        sum=sum+(k*(p/f));

       }
       printf("the value of expression is %lf",sum);
       getch();
}
/*
INPUT:1.enter the value of x 2
      2.enter the value of x 7
OUTPUT:1.the value of expression is -0.416155
       2.the value of expression is -21.726113
*/


b)  i) A perfect number is a number that is the sum of all its divisors except itself. Six is the perfect number. The only numbers that divide 6 evenly are 1,2,3 and 6 (i.e 1+2+3=6).
ii) An abundant number is one that is less than the sum of its divisors (Ex: 12<1+2+3+4+6).
iii) A Deficient number is one that is greater than the sum of its divisors (Ex: 9>1+3)

      Write a program to classify N integers (Read N from keyboard) each as perfect, abundant or deficient.

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
   int n,sum=0,i;
   clrscr();
   printf("enter the number");
   scanf("%d",&n);
   for(i=1;i<=n/2;i++)
   {
     if(n%i==0)
     sum=sum+i;
   }
   if(sum==n)
   printf("PERFECT NUMBER");
   if(sum<n)
   printf("DEFICIENT NUMBER");
   if(sum>n)
   printf("ABUNDANT NUMBER");
   getch();
}

/*
INPUT:enter the number6
OUTPUT:PERFECT NUMBER
*/

For the range from 0 to N

Program:

#include<stdio.h>
#include<conio.h>
main()
{
     int n,sum=0,i,j;
     clrscr();
     printf("enter the range ");
     scanf("%d",&n);
     for(i=1;i<=n;i++)
     {
       sum=0;
       for(j=1;j<i;j++)
       {
          if(i%j==0)
          sum=sum+j;
       }
       if(sum==i)
       printf("\nthe perfect number is %d",i);
       if(sum<i)
       printf("\nthe deficient number is %d",i);
       if(sum>i)
       printf("\nthe abundant number is %d",i);
     }
     getch();
}
/*
INPUT:enter the range 10
OUTPUT:
the deficient number is 1
the deficient number is 2
the deficient number is 3
the deficient number is 4
the deficient number is 5
the perfect number is 6
the deficient number is 7
the deficient number is 8
the deficient number is 9
the deficient number is 10
*/

No comments:

Post a Comment