Oops 2
Oops 2
Code
Sample Output
Enter the number
123
The sum of digits of 123 is 6
2. 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
Code:
// fibonacci series
#include <stdio.h>
Void main()
{
Int no_of_terms,sum,term1=0,term2=1;
Printf(“Enter the no of terms”);
Scanf(“%d”,&no_of_terms);
If(no_of_terms>=1)
{
Printf(“%d\n”,term1);
}
If (no_of_terms>=2)
{
Printf(“%d\n”,term2);
}
If (no_of_terms>3)
{
Int sum,i=2;
While (i<no_of_terms)
{
Sum=term1+term2;
Term1=term2;
Term2=sum;
Printf(“%d\n”,sum);
I++;
}
}
}
Sample Outputs
3.Write a C program to generate all the prime numbers between I and n, where n is
a value supplied by the user.
Code
// to print prime numbers from 1 to a given number
#include <stdio.h>
void main()
{
int i=2,n;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("The prime nos between 1 and %d are\n",n);
while(i<=n)
{
int j=2,flag=0;
while(j<=i/2)
{
if(i%j==0)
{
flag=1;
break;
}
j++;
}
if(flag==0)
{
printf("%d\n",i);
}
i++;
}
}
Sample Output
1.Enter the value of n
14
The prime nos between 1 and 14 are
2
3
5
7
11
13