0% found this document useful (0 votes)
7 views

Lab 3

HELP IN LABS

Uploaded by

isheanesundewere
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab 3

HELP IN LABS

Uploaded by

isheanesundewere
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EUROPEAN UNIVERSITY OF LEFKE

Faculty of Engineering
Department of Computer Engineering

COMP 124
COMPUER PROGRAMING

Lab Work No.3

Prepared by Fortune Zishiri


(22144209)

Submitted to Dr. Ferhun Yorgancioglu


a) Here's a C program that prints values between user-specified lower and upper limits using a loop. It
also handles the case where the user enters the upper limit before the lower limit by swapping the
values to ensure the correct order.

#include <stdio.h>

int main() {

int lower, upper;

printf("Enter the lower limit: ");

scanf("%d", &lower);

printf("Enter the upper limit: ");

scanf("%d", &upper);

if (lower > upper) {

int temp = lower;

lower = upper;

upper = temp;

printf("Values between %d and %d are:\n", lower, upper);

for (int i = lower; i <= upper; i++) {

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

return 0;

}
b) Here's a C program that uses a do-while loop to keep asking the user for integer values and calculates
the summation of all values until a zero value is entered

#include <stdio.h>

int main() {

int value;

int sum = 0;

// Keep asking for values until zero is entered

do {

printf("Enter an integer value (or 0 to stop): ");

scanf("%d", &value);

sum += value; // Add value to the sum

} while (value != 0);

printf("Sum of all values: %d\n", sum);

return 0;

In this program, we use a do-while loop because we want to ensure that the loop runs at least once,
even if the user enters zero as the first value. The loop continues to ask the user for integer values using
scanf until a zero value is entered. The sum variable keeps track of the summation of all values entered
by the user. Once the loop is exited, the program prints the total sum of all values.

c) Here's a C program that asks the user to enter a positive number and checks if the entered number
has a good friend using a loop and mathematical calculations without using any library functions like
power or square root.

#include <stdio.h>

// Function to calculate the square of a number

int square(int num) {


return num * num;

int main() {

int num;

// Get input from user

printf("Enter a positive number: ");

scanf("%d", &num);

// Check for good friend

int sum_of_divisors = 0;

for (int i = 1; i < num; i++) {

if (num % i == 0) {

sum_of_divisors += i;

// Check if the sum of divisors is a good friend

if (square(sum_of_divisors) == num) {

printf("%d is a good friend of %d!\n", num, sum_of_divisors);

} else {

printf("%d does not have a good friend.\n", num);

return 0;

In this program, we use a loop to calculate the sum of divisors of the entered number num by iterating
from 1 to num-1 and checking if num is divisible by the current iteration value i. If it is, we add i to the
sum_of_divisors variable. Finally, we check if the square of the sum_of_divisors is equal to num, and if
so, we print that num is a good friend of sum_of_divisors. Otherwise, we print that num does not have a
good friend.

d) Here's a C program that prints the given pattern using nested do-while loops.

#include <stdio.h>

int main() {

int num = 1;

// Print the pattern using do-while loops

int i = 1;

do {

int j = 1;

do {

printf("%d ", num);

num += 2;

j++;

} while (j <= 5);

printf("\n");

i++;

} while (i <= 4);

return 0;

In this program, we use nested do-while loops to print the pattern. The outer loop runs 4 times, and the
inner loop runs 5 times. Inside the inner loop, we print the value of num which starts from 1 and is
incremented by 2 in each iteration to generate the sequence of odd numbers. After each row is printed,
a newline character is printed to move to the next row.

e) Here's a C program that prints the given pattern based on the user input n using nested while loops.
#include <stdio.h>

int main() {

int n;

// Get input from user

printf("Enter a positive integer: ");

scanf("%d", &n);

int num = n;

int i = 1;

// Print the pattern using while loops

while (i <= n) {

int j = 1;

while (j <= i) {

printf("%d ", num);

j++;

printf("\n");

num = num - 1;

i++;

return 0;

In this program, we use nested while loops to print the pattern based on the user input n. The outer
loop runs n times, and the inner loop runs i times in each iteration of the outer loop. Inside the inner
loop, we print the value of num which starts from n and is decremented by 1 in each iteration to
generate the desired pattern. After each row is printed, a newline character is printed to move to the
next row.

You might also like