Thursday 7 July 2011

"SOLVED PROGRAMS (AT WEEKLY )

WEEK 1:
FIBONACCI:
/* 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.
*/

#include <stdio.h>

void main()
{
int num1=0, num2=1,no,counter,fab;
clrscr();

printf("<===========PROGRAM TO FIND THE FIBONACCI SERIES UP TO N NO. IN SERIES=========>");
printf("\n\n\n\t\tENTER LENGTH OF SERIES (N) : ");
scanf("%d",&no);

printf("\n\n\t\t\t<----FIBONACCI SERIES---->");
printf("\n\n\t\t%d  %d",num1,num2);

//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN ADVANCE
for(counter = 1; counter <= no-2; counter++)
{
fab=num1 + num2;
printf("  %d",fab);
num1=num2;
num2=fab;
}
getch();
}
PRIME NO:

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

#include <stdio.h>

void main()
{
int no,counter,counter1,check;
clrscr();
printf("<-----------------------PRIME NO. SERIES------------------------>");
printf("\n\n\n\t\t\tINPUT THE VALUE OF N: ");
scanf("%d",&no);
printf("\n\nTHE PRIME NO. SERIES B/W 1 TO %d : \n\n",no);

for(counter = 1; counter <= no; counter++)
{
 check = 0;
 //THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.

 for(counter1 = counter-1; counter1 > 1 ; counter1--)
   if(counter%counter1 == 0)
   {
    check++;        // INCREMENT CHECK IF NO. IS NOT A PRIME NO.
    break;
   }
   if(check == 0)
   printf("%d\t",counter);
}
getch();
}
SUM OF DIGITS:
/* Write a C program to find the sum of individual digits of a positive integer.*/

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

void main()
{
  int num, k=1, sum=0;
  clrscr();
  printf("Enter the number whose digits are to be added:");
  scanf("%d",&num);
while(num!=0)
{
  k=num%10;
  sum=sum+k;
  k=num/10;
  num=k;
}
printf("Sum of the digits:%d",sum);
getch();
}
 

No comments:

Post a Comment