Looping Constructs Mendiola - Assessment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Name: Adriel D.

Mendiola COMP 002 – Computer Programming 1


Year&Section: BSIT 1-4 Using Looping Constructs

Activity 1: Counting Even Numbers

Situation: You are tasked with writing a program that counts and prints all even numbers between 1 and
20 using a loop. Use a for loop to iterate through the numbers and print only the even ones.

#include <stdio.h>

int main() {

for (int even = 2; even <= 20; even = even + 2){

printf("%d\n", even);

return 0;

Activity 2: Factorial Calculation

Situation: Your task is to create a program that calculates the factorial of a given number. Use a while
loop to compute the factorial of a user-input integer.

#include <stdio.h>

int main() {

int number,i =1,f=1;


Name: Adriel D. Mendiola COMP 002 – Computer Programming 1
Year&Section: BSIT 1-4 Using Looping Constructs

printf("Enter a number:");

scanf("%d",&number);

while(i <= number) {

f=f*i;

i++;

printf("fatorial of %d = %d", number, f);

return 0;

Activity 3: Displaying a Pattern

Situation: Write a program to display the following pattern using nested for loops.

1
22
333
4444
55555
#include <stdio.h>
int main() {
Name: Adriel D. Mendiola COMP 002 – Computer Programming 1
Year&Section: BSIT 1-4 Using Looping Constructs

int rows = 5;
for (int i=1; i <= rows; i++ ){
for(int j = 1; j<=i;j++){
printf("%d", i);
}
printf("\n");
}
return 0 ;
}

Activity 4: Sum of Numbers


Situation: Write a program to find the sum of all numbers between 1 and 100 that are divisible by 3. Use
a do while loop to iterate through the numbers.
#include <stdio.h>
int main() {
int num=1,sum = 0;
do{
if (num %3 == 0) {
sum = num + sum;
}
num++;

}while (num <= 100);


printf("The sum of numbers from 1 to 100 that is divisible by 3: %d",
sum);
Name: Adriel D. Mendiola COMP 002 – Computer Programming 1
Year&Section: BSIT 1-4 Using Looping Constructs

return 0;
}

Activity 5: Temperature Conversion Tool

Situation: You are developing a program that converts temperatures from Celsius to Fahrenheit. Allow
the user to convert multiple temperatures until they choose to exit.

#include <stdio.h>

int main() {

float Fahrenheit,Celsius;

char choice;

do{

printf("Enter a temperature in celcius = ");

scanf("%f", &Celsius);

Fahrenheit = (1.8 * Celsius) + 32;

printf("%.2f Celsius is equivalent to %.2f Fahrenheit\n",


Celsius, Fahrenheit);
Name: Adriel D. Mendiola COMP 002 – Computer Programming 1
Year&Section: BSIT 1-4 Using Looping Constructs

printf("Do you want to continue with other value? (y/n) = " );

scanf(" %c", &choice);

} while (choice == 'y' || choice == 'Y');

return 0;

You might also like