You are on page 1of 6

TECHNICAL UNIVERSITY OF MOLDOVA

FACULTY OF COMPUTERS, INFORMATICS AND MICROELECTRONICS


SPECIALITY SOFTWARE ENGINEERING

Report
on
Computer programming

Laboratory Work nr.3

Performed: st. gr. FAF-212


Maia Zaica

Verified: Associate professor, dr.


Mihail Kulev

Chişinău 2021
LABORATORY WORK Nr.3

Laboratory works goal: Gain practical skills

Topic: Conditional statements. If ternary. Return type.

Problem condition:

A. Flea travel
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A flea is sitting at one of the n hassocks, arranged in a circle, at the moment. After minute number k the flea jumps
through k - 1 hassoсks (clockwise). For example, after the first minute the flea jumps to the neighboring hassock. You
should answer: will the flea visit all the hassocks or not. We assume that flea has infinitely much time for this jumping.

Input
The only line contains single integer: 1 ≤ n ≤ 1000 — number of hassocks.

Output
Output "YES" if all the hassocks will be visited and "NO" otherwise.

Examples

Short theory on laboratory work:


C is a procedural programming language. The main features of C language include low-level access to memory, simple
set of keywords, and clean style, these features make C language suitable for system programming like operating system
or compiler development.
The components of structure are:
Header Files Inclusion: The first and foremost component is the inclusion of the Header files in a C program.
A header file is a file with extension .h which contains C function declarations and macro definitions to be shared
between several source files.
Some of C Header files:
 stddef.h – Defines several useful types and macros.
 stdint.h – Defines exact width integer types.
 stdio.h – Defines core input and output functions
 stdlib.h – Defines numeric conversion functions, pseudo-random network generator, memory allocation
 string.h – Defines string handling functions
 math.h – Defines common mathematical functions
Main Method Declaration: The next part of a C program is to declare the main() function. The syntax to declare the
main function is:
Syntax to Declare the main method: int main()
{}

Variable Declaration: The next part of any C program is the variable declaration. It refers to the variables that are to
be used in the function. Please note that in the C program, no variable can be used without being declared. Also in
a C program, the variables are to be declared before any operation in the function.
Example: int main()
{
int a;
.
.

Body: The body of a function in the C program, refers to the operations that are performed in the functions. It can
be anything like manipulations, searching, sorting, printing, etc.
Example: int main()
{
int a;

printf("%d", a);
.
.
Return Statement: The last part of any C program is the return statement. The return statement refers to the
returning of the values from a function. This return statement and return value depend upon the return type of the
function. For example, if the return type is void, then there will be no return statement. In any other case, there will
be a return statement and the return value will be of the type of the specified return type.

Laboratory work processing:

#include <stdio.h> In a C program, all lines that start with # are processed by a preprocessor which is a program
invoked by the compiler. In a very basic term, the preprocessor takes a C program and produces another C
program. The produced program has no lines starting with #, all such lines are processed by the preprocessor. In
the above example, the preprocessor copies the preprocessed code of stdio.h to our file. The .h files are called
header files in C. These header files generally contain declarations of functions. We need stdio.h for the function
printf() used in the program.

int main() There must be a starting point from where execution of compiled C program begins. In C, the execution
typically begins with the first line of main().The empty brackets indicates that the main doesn’t take any parameter.
In C, if a function signature doesn’t specify any argument, it means that the function can be called with any number
of parameters or without any parameters. main() can be written to take parameters also.
The int was written before main indicates return type of main(). The value returned by main indicates the status of
program termination. See this post for more details on the return type.
In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the
symbolic representation of a memory location.
We have variable n of type int.

In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function
reads formatted input from the standard input such as keyboards. We input the number A (3 ≤ A ≤ 1000).
Here, have used %d format specifier inside the scanf() function to take int input from the user. When the user
enters an integer, it is sorted in the A variable.

Conditional Operator in C
The conditional operator is also known as a ternary operator. The conditional statements are the decision-making
statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.
As conditional operator works on three operands, so it is also known as the ternary operator.
The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also a decision-
making statement.

Meaning of the above syntax.

o In the above syntax, the expression1 is a Boolean condition that can be either true or false value.
o If the expression1 results into a true value, then the expression2 will execute.
o The expression2 is said to be true only when it returns a non-zero value.
o If the expression1 returns false value then the expression3 will execute.
o The expression3 is said to be false only when it returns zero value.

A flea is sitting at one of the n hassocks, arranged in a circle, at the moment. After minute number k the flea jumps
through k - 1 hassoсks (clockwise). If n is a power of two, the flea will visit all the hassocks, if it is not it won’t.

n&n-1 is figuring out if n is either 0 or an exact power of two.

It works because a binary power of two is of the form 1000...000 and subtracting one will give you 111...111. Then,
when we AND those together, we get zero

Any non-power-of-two input value (other than zero) will not give us zero when we perform that operation.

In the above code, we are taking input as the 'n' (number of hassocks) of the user. After taking input, we have applied
the condition by using a conditional operator. In this condition, we are checking if n is either 0 or an exact power of two.
If n is power of two, then the statement1 will execute, i.e., (printf("Yes")) otherwise, statement2 will execute, i.e.,
(printf("No")).
The printf() function is the standard C way of displaying output on the screen. The quotes tell the compiler that you
want to output the literal string.

Finally, at the end of the program, we return a value from main to the operating system by using the return
statement. This return value is important as it can be used to tell the operating system whether our program
succeeded or not. A return value of 0 means success. The final brace closes off the function.

Program code in C language

Results of running and testing the program (screenshots) :

Teting on codeforces
Conclusions:

 Skills were developed to compile, run and test a program in the C programming language.
 The verification of the results confirms that the elaborated program works correctly.
 Practiced analitical skills.
 Got more familiar with Ternary Operator.

Bibliography:
1. The overview of Computer Programming course lessons for students gr. (lecturer: associate professor dr. M. Kulev).
Chisinau, UTM, FCIM, 2021.
2. Tutorial in C language. https://www.javatpoint.com/c-programming-language-tutorial
3. https://www.geeksforgeeks.org/c-language-set-1-introduction/

You might also like