Exercise 3

Write a C program to find the sum of individual digits of a positive integer and find the reverse of the given number.

SOURCE CODE:

/*Program to find sum of individual digits and reverse of a given number.*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int n,m,rev=0,sum=0;
 clrscr();
 printf("\nEnter number:\n");
 scanf("%d",&n);
 m=n;
 while(n>0)
 {
  sum=sum+n%10;
  n=n/10;
 }
 while(m>0)
 {
  rev=rev*10+m%10;
  m=m/10;
 }
 printf("\nSum of individual digits is %d",sum);
 printf("\nAfter reverse digits number is %d",rev);
}

OUTPUT:








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 C program to generate the first n terms of the sequence.

SOURCE CODE:

/*Program that displays Fibonacci Sequence*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int a=0,b=1,c,n;
 clrscr();
 printf("Enter the range:");
 scanf("%d",&n);
 printf("\n::Fibonacci Sequence is::\n");
 printf("%d %d ",a,b);
 c=a+b;
 while(c<=n)
 {
  printf("%d ",c);
  a=b;
  b=c;
  c=a+b;
 }
}

OUTPUT:










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

SOURCE CODE:

/*Program to print all primes in the given range.*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int n,i,j,c;
 clrscr();
 printf("\nEnter the range:");
 scanf("%d",&n);
 printf("\nPrime numbers in the given range:\n");
 for(i=1;i<=n;i++)
 {
  c=0;
  for(j=1;j<=i;j++)
  {
   if(i%j==0)
    c++;
  }
  if(c==2)
   printf("%d ",i);
 }
}

OUTPUT: