You are on page 1of 5

HOLY NAME UNIVERSITY

College of Engineering and Computer Studies


Tagbilaran City, Bohol

Activity 3:

Individual Programming Assignment


CCS-104 Script

Good day, Ma’am! I’m Karylle Delantes, and today I’m going to show you a program
that I have learned in my past classes.

It is just a simple program that determines the largest prime number and the
smallest composite number.

Before I start, Let’s define and differentiate prime and composite numbers.

• A Prime Number has exactly two factors. “1” and the number itself.
Example: 2, 5, 7, 19, and many more…
• A Composite Number has more than two factors.
Example: 4, 10, 15, 36, and many more…

Page 1 of 5
So, let’s go back to the program.

Program Description:

The program will ask for a set of positive integers. The program will only stop asking
for a number if the user enters a “0” or a negative number. From these positive
numbers, it will determine and print the largest prime and the smallest composite
numbers.

Here is the code I created:

I started with a function which is the check function.

• It checks the number, whether if it’s a Prime or Composite number.

But I will tackle that later.

Inside the main function:

• The Declaration Statements are num, largestPrime, SmallestComp, and


ans. I assigned largestPrime and SmallestComp to 0 since it is where the
inputted number will be stored later.
• Then the do-while loop, where printf and scanf will be performed first.
• When the user enters an integer number, it will be stored inside num, and go
through or calls the check function.

Check Function:

• The Declaration Statements in this function are i, which means initial, and c,
which means count.
• Inside the for-loop, the num will be checked whether if it has only two
factors or has more than two factors.
• Next, it will go through the if-else statements to see whether the variable c
is equals to two or not.
• If it equals 2, then it’s a Prime number which it returns to num as 0. On the
other hand, if c does not equals 2 then it’s a Composite number which it
returns to num as 1.

Page 2 of 5
Let's go back to the main function:

• So ans will depend on the entered number, whether if it’s 0 or a 1.


• Next, it will again go through if-else statements to see if the num is the
largest prime or the smallest composite.
• Then it will store inside the variables largestPrime or smallestComp.
• The while keyword here is part of the do-while loop, where the loop will stop
when the user enters a 0 or a negative number.

Lastly, the output:

• It contains if-else statements, showing the Largest Prime number and the
Smallest Composite number on the screen.

So let’s check if the program will run correctly.

To check:

1. Determines the Largest Prime number and the Smallest Composite number

Enter Number: 20
Enter Number: 2
Enter Number: 13
Enter Number: 9
Highest Prime Number: 19
Enter Number: 10 Smallest Composite Number: 9
Enter Number: 3
Enter Number: 19
Enter Number: 0

2. Determines the Largest Prime number and the Smallest Composite number

Enter Number: 17
Enter Number: 43
Highest Prime Number: 61
Enter Number: 61
Smallest Composite Number: 4
Enter Number: 12
Enter Number: 4
Enter Number: -2

Page 3 of 5
The program runs smoothly, and the expected outputs are correct.

That’s all, thank you for listening and have a wonderful day. Bye!

Code:

//DELANTES, Karylle - Determines the largest prime number and the smallest
composite number
//Activity 3 - Individual Programming Assignment (CCS104)

#include <stdio.h>

int check(int num){


int i, c = 0;
for(i = 1; i <= num; i++){
if(num % i == 0){
c++;
}
}
if(c == 2){
//Prime = 0
return num = 0;
}else{
//Composite = 1
return num = 1;
}
}

int main(){
int num, largestPrime = 0, smallestComp = 0, ans;

printf("Determines the Largest Prime number and the Smallest Composite


number\n");
printf("Direction: Enter only positive integers unless you want to stop, ente 0 or a
negative number\n\n");

Page 4 of 5
do{
//Input
printf("Enter number: ");
scanf("%d", &num);
//Process
ans = check(num);

if(ans == 0 && num > largestPrime)


{
largestPrime = num;
}else if(ans == 1 && num > 1 && smallestComp == 0){
smallestComp = num;
}else if(ans == 1 && num > 1 && smallestComp > num){
smallestComp = num;
}
}while(num > 0);

//Output
printf("\n--------------------------------\n");

if(largestPrime > 0){


printf("\nLargest Prime: %d\n", largestPrime);
}else{
printf("\nLargest Prime: None\n");
}

if(smallestComp > 0){


printf("Smallest Composite: %d\n", smallestComp);
}else{
printf("Smallest Composite: None\n");
}

//Name
printf("\nSumbitted by: Karylle Delantes BSIT-2\n");
("\nSumbitted to: Ma'am Isabel Retutal'\n");
printf("\nDate: August 17, 2021 (Tuesday)\n");

Page 5 of 5

You might also like