Tweet
Share
COMPUTER PROGRAMMING
HOME
Quadratic equation
Floids Triangle
Fibonacci series & Prime numbers
Merge Sorting
Stack in an array
Two Stacks in an array
Tweet
Share
C PROGRAMMING QUESTIONS AND ANSWERS WITH ALGORITHM
4.Write a C Program to generate,first n number of elements in the Fibonacci series?
ALGORITHM
Step 1:Start
Step 2:Declare variables n,i=0,j=1,temp,k.
Step 3:Input and read the limit of the Fibonacci series,n.
Step 4:If n==1 then
4.1)Print i and j.
Step 5:Else
5.1)Print i and j.
5.2)Initialize k as 2.
5.3)while(k<n) repeat steps a to e.
a)Find temp= i+j.
b)Print temp.
c)Assign i=j.
d)Assign j=temp.
e)Increment k by 1.
5.4)End while.
Step 6:End if.
Step 7:Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0,j=1,temp,k;
clrscr();
printf("Enter the limit: ");
scanf("%d",&n);
printf("fibonacci series\n");
if(n==1)
{
printf("%d\n%d\n",i,j);
}
else
{
printf("%d\n%d\n",i,j);
for(k=2;k<n;k++)
{
temp=i+j;
printf("%d\n",temp);
i=j;
j=temp;
}
}
getch();
}
OUTPUT
Enter the limit:13
fibonacci series
0
1
1
2
3
5
8
13
21
34
55
89
144
------------------------------------------------------------------------------
5.Write a C program to print the prime numbers up to a limit?
ALGORITHM
Step 1:Start.
Step 2:Declare variables num,flag,i,j.
Step 3:Input and read the limit,num.
Step 4:If num=1 then
4.1)Print there are no prime numbers.
Step 5:Else
5.1)Initialize i=2.
5.2)while(i<=num) repeat steps a to g.
a)Initialize flag=0 and j=1.
b)while(j<=i) repeat steps b.1 to b.3
b.1)if(i%j==0) then increment flag by 1.
b.2)End if.
b.3)Increment j by 1.
c)End while.
d)If flag=2 then print the number,i.
e)End if.
g)Increment i by 1.
5.3)End while.
Step 6:End if.
Step 7:Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int num,flag,i,j;
clrscr();
printf("Enter the limit:\n");
scanf("%d",&num);
if(num==1)
{
printf(“No Prime numbers”);
}
else
{
printf("Prime Numbers are \n");
for(i=2;i<=num;i++)
{
flag=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
flag++;
}
if(flag==2)
printf("%d\n",i);
}
}
getch();
}
OUTPUT
Enter the limit:
20
Prime Numbers are
2
3
5
7
11
13
17
19
First
<<Previous
Next>>