0% found this document useful (0 votes)
37 views12 pages

MSU Assignment Cover Page: Intan Nasuha

The document is an assignment for a Linux programming course. It contains 3 programming problems to solve: 1) a program to convert between days, hours, minutes and seconds, 2) a program to check properties of numbers like being special, big, weird or scary, and 3) a program to calculate the greatest common divisor and least common multiple of two integers input by the user. The cover page includes student details and submission instructions.

Uploaded by

Intan Nasuha
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)
37 views12 pages

MSU Assignment Cover Page: Intan Nasuha

The document is an assignment for a Linux programming course. It contains 3 programming problems to solve: 1) a program to convert between days, hours, minutes and seconds, 2) a program to check properties of numbers like being special, big, weird or scary, and 3) a program to calculate the greatest common divisor and least common multiple of two integers input by the user. The cover page includes student details and submission instructions.

Uploaded by

Intan Nasuha
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

Linux Programming

(CCS10903)
Assignment 3

Received Date : 3/12/2020


Submission Date : 16/12/2020
Weightage : 10 %
Semester : September 2020
Lecturer : Dr. Asif

Instruction to students:
• This is Individual lab practical.

• Complete this cover sheet and attach it to your assignment (first page).

Student declaration:
I declare that:
• This assignment is my own work
• I understand what is meant by plagiarism
• My lecturer has the right to deduct my marks in the case of:
- Late submission
- Any plagiarism found in my assignment.
Name Student ID

INTAN NASUHA BT RUSLIZAN 012018091145

TOTAL

MARKS :
Linux Programming – CCS10903
Semester: September 2020

Assignment – 3

1. Write a GNU C program to read days, hours and minutes and convert
days to hours, hours to minutes and minutes to seconds. Declare
variable separately to convert days to hours, hours to minutes and
minutes to seconds. 

2. A number is special if it is exactly divisible by 7. A number is big if it is


greater than 9999. A number is weird if it is exactly divisible by 2 and 4
but not 8. A number is scary if it is big and weird. Write a GNU C
program to check which of the following, 822, 3227, 768 and 10086 are
special but not scary. Declare four variables called special, big, weird,
and scary and make suitable assignments to these variables as
a number is tested. 

3. Write a program that asks the user to enter two integers, then calculates
and displays their Greatest Common Divisor (GCD) and Least
Common Multiple (LCM) among the two.

Your submission should bind together with your Name, Student ID, Subject
name, and Assignment Number clearly written on the cover page. The cover
should be in YELLOW color. Submit your work to Dr. Asif Iqbal. H by 16
December 2020
1. Write a GNU C program to read days, hours and minutes and
convert days to hours, hours to minutes and minutes to seconds.
Declare variable separately to convert days to hours, hours to
minutes and minutes to seconds. 

Source Code

#include <stdio.h>

int main() {

int sec, d, h, m, s;

printf("Input seconds: ");

scanf("%d", &sec);

d = (sec / (24 * 3600));

h = (sec/3600);

m = (sec -(3600*h))/60;

s = (sec -(3600*h)-(m*60));

printf(" Days : %d\n Hours : %d\n Minutes : %d\n Seconds:


%d\n ",d,h-24,m,s);

return 0;

}
Output
2. A number is special if it is exactly divisible by 7. A number is big if
it is greater than 9999. A number is weird if it is exactly divisible by
2 and 4 but not 8. A number is scary if it is big and weird. Write
a GNU C program to check which of the following, 822, 3227, 768
and 10086 are special but not scary. Declare four variables called
special, big, weird, and scary and make suitable assignments to
these variables as a number is tested. 

Source Code:

#include<stdio.h>

#include<conio.h>

int main()

int n,special,big,weird,scary;

printf("Enter a number:");

scanf("%d",&n);

if(n%7==0)

special=n;

printf("Number is special.\n");

else

{
printf("Number is not special.\n");

if (n>999)

big=n;

printf("Number is big.\n");

else

printf("Number is not big.\n");

if(n%2==0 && n%4==0 && n%8!=0)

weird=n;

printf("Number is weird.\n");

else

{
printf("Number is not weird.\n");

if(n==big && n==weird)

scary=n;

printf("Number is scary.");

else

printf("Number is not scary.");

getch();

return 0;

}
Output

Input number: 822

Input number: 768

Input number: 3227


Input number: 10086
3. Write a program that asks the user to enter two integers, then
calculates and displays their Greatest Common Divisor (GCD) and
Least Common Multiple (LCM) among the two.

Source Code

#include <stdio.h>
int main()
{
int n1, n2, i, gcd, max;

printf("Enter two integers: ");


scanf("%d %d", &n1, &n2);

for(i=1; i <= n1 && i <= n2; ++i)


{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}

printf("The G.C.D of %d and %d is %d.\n", n1, n2, gcd);

// maximum number between n1 and n2 is stored in min


max = (n1 > n2) ? n1 : n2;

while (1) {
if (max % n1 == 0 && max % n2 == 0)
{
printf("The L.C.M of %d and %d is %d.", n1, n2, max);
break;
}
++max;
}

return 0;
}
OUTPUT

Common questions

Powered by AI

A logical error in designing such a program could occur if conditions are not properly prioritized, as the condition for 'scary' requires the number to be both 'big' and 'weird.' If the logic does not correctly combine these conditions or fails to consider edge cases where conditions overlap and conflict, unexpected classifications might occur. Ensuring these conditions are checked sequentially with careful attention to logical operators is crucial .

The program prompts user input through standard input functions, and this input determines the flow of operations by acting as conditions and constants for calculations and logic checks. Through variable assignment and conditional logic branching based on user data, the program executes conversions and checks accurately, directly reflecting user-provided values .

This condition assumes 'big' and 'weird' checks are sufficient, but it might not address variation in number properties where a number could independently satisfy 'big' and 'weird' without being deliberately 'scary'. Handling properties where values oscillate between categories needs more nuanced conditional checking to avoid misclassification due to overlapping characteristics .

In the GNU C program, a number is considered 'special' if it is divisible by 7, and 'scary' if it is both 'big' (greater than 9999) and 'weird' (divisible by 2 and 4, but not 8). The program uses conditions with modulus operations to check these properties and assigns appropriate values to the variables 'special', 'big', 'weird', and 'scary' based on the number tested .

To calculate the GCD, the program increments a variable 'i' from 1 and finds the largest integer that divides both numbers without a remainder using a for-loop. For the LCM, it starts with the maximum of the two numbers and continuously increments until it finds a number that both inputs divide evenly. This ensures the smallest common multiple is found .

Declaring variables separately for different conversion tasks in the GNU C program (days to hours, hours to minutes, and minutes to seconds) enhances readability and modularizes the code, making it easier to manage and debug each conversion process independently. This approach avoids errors that could arise from variable reuse and aids in maintaining distinct values for each conversion stage .

Attaching an assignment cover page ensures all relevant information (Name, Student ID, Subject, Assignment Number) is clearly presented, aiding in organization and identification. Specifying a format, such as color, helps standardize submissions for easy sorting and handling by teachers, reducing administrative burden and enhancing consistency in documentation .

The GNU C program reads the number of days, hours, and minutes as input and uses separate variables for conversion. Days are converted to hours by multiplying with 24, hours to minutes by multiplying with 60, and minutes to seconds by multiplying with 60. The program then prints the converted values .

Implementing functions for each conversion or condition check can enhance modularity and reusability. Using advanced C features, like structures to encapsulate related variables (e.g., time components), can improve readability. Efficient algorithms like the Euclidean method for GCD can replace simple loops, saving computational resources. Condition checks can benefit from literal handling and switch-case structures for clear logic flow .

Challenges could include ensuring the accurate breakdown of seconds into constituent parts, such as correctly accommodating overflow from higher units (e.g., ensuring hours reset to zero after 24). Additionally, edge cases should be handled where input exactly equals a day or hour boundary, requiring precise modulus and division operations .

You might also like