0% found this document useful (0 votes)
19 views5 pages

Oops 2

The document contains the code for 3 programs written in C: 1) A program to find the sum of the digits of a positive integer. 2) A program to generate the first n terms of the Fibonacci sequence. 3) A program to print all prime numbers between 1 and a user-supplied number n. Sample inputs and outputs are provided for each program.

Uploaded by

Aysha Noushad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Oops 2

The document contains the code for 3 programs written in C: 1) A program to find the sum of the digits of a positive integer. 2) A program to generate the first n terms of the Fibonacci sequence. 3) A program to print all prime numbers between 1 and a user-supplied number n. Sample inputs and outputs are provided for each program.

Uploaded by

Aysha Noushad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

OOPS Assessment

Name: Aysha Noushad


Registration No: 23BRS1014
Date: 10.01.2024
1. Write a C program to find the sum of individual digits of a positive integer

Code

//to find the sum of individual digits of a number


#include <stdio.h>
void main()
{
int number,sum,number2;
printf("Enter the number\n");
scanf("%d",&number);
number2=number;
while(number2!=0)
{
sum+=number2%10;
number2/=10;
}
printf("The sum of digits of %d is %d",number,sum);
}

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

1. Enter the no of terms1


0

2. Enter the no of terms2


0
1

3. Enter the no of terms7


0
1
1
2
3
5
8

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

You might also like