Powered By Blogger

Friday, April 20, 2012

Program : 6



PROGRAM - 6(a)

Write a program to find the sum of individual digits of a positive integer.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
     unsigned long num,temp;
     short int sum=0;
     clrscr();
     printf("\n Enter a positive integer : ");
     scanf("%ld",&num);
     temp=num;
     while (temp!=0)
     {
           sum=sum+temp%10;
           temp=temp/10;
     }
     printf("\n sum of individual digits 
                       of %ld = %d",num,sum);
     getch();
}

/*
INPUT: Enter a Positive integer: 1234
OUTPUT: sum of the individual digits of 1234 is 10
*/


PROGRAM - 6(b)

A Fibonacci sequence is defined as follows: 
   The first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a program to generate the first N terms of the sequence.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
unsigned long int a=0,b=1,i=2;
unsigned short int n;
clrscr();
printf("enter positive value for n (0 to 47): ");
scanf("%d",&n);
printf("\n <_ _ _ _ FIBONACCI SERIES _ _ _ _>\n\n");
while (i<n)
{
     printf("\t%ld\t%ld",a,b);
     a=a+b;
     b=a+b;
     i+=2;
}
if (n%i==0)
     printf("\t%ld\t%ld",a,b);
else
     printf("\t%ld",a);
getch();
}

/*
<_ _ _ _ FIBONACCI SERIES _ _ _ _>
INPUT: enter positive value for n (0 to 47):10
OUTPUT:
FIBONACCI SERIES
0  1  1  2  3  5  8  13  21  34
*/


PROGRAM - 6(c)

Write a program to generate all the prime numbers between 1 and N, where N is a value supplied by the user.

Program:

#include<stdio.h>
#include<conio.h>
main()
{
    int n,i,j,m=0;
    clrscr();
    printf("enter n value");
    scanf("%d",&n);
    printf("the prime numbers are");
    for(i=2;i<=n;i++)
    {
      for(j=2;j<=i;j++)
       {
       if(i%j==0)
        m++;
       }
   
    if(m==0)
    printf("%4d",i);
    m=0;
    }
    getch();
}

/*
INPUT: enter n value: 10
OUTPUT: the prime numbers are
   2   3   5   7    
*/

No comments:

Post a Comment