Professional Documents
Culture Documents
Fundamentals of Programming Assignment 1
Fundamentals of Programming Assignment 1
Assignment 1
Question 1:
Arrange the following lines of code to display the addition of two integers:
#include <stdio.h>
int main(void)
{
int integer1, integer2, sum;
return 0;
}
Question 2:
For each of the following statements, explain why it is not correct, and fix it
b) #define PI = 3.14159
This statement is a directive containing a macro-definition, and a macro-definition does not
contain an = sign. The correct statement is:
#define PI 3.14159
e) float enum=5.89;
In this statement, there is an invalid combination of two type specifiers i.e. float and enum.
Also, there is no f after 5.89. in such case, compiler will give a warning. The correct
statement will be:
float x= 5.89f;
Question 3:
a) Prompt the user to enter an integer. End your prompting message with a colon (:) followed
by a space and leave the cursor positioned after the space.
printf(“Enter the integer: ”);
b) Read an integer from the keyboard and store the value entered in integer variable a.
scanf(“%d”, &a);
d) Print the message "This is a C program." on two lines so that the first line ends with C.
printf(“This is a C ”);
printf(“program.\n”);
e) Print the message "This is a C program." with each word on a separate line.
printf(“This” );
printf(“is” );
printf(“a” );
printf(“C” );
printf(“program.\n”);
Question 4:
Write a program that calculates the product of three integers. Use scanf to read the values of
integers.
Add comments in your program. (To get full marks your code should be legible, not only just correct)
#include <stdio.h>
#include <math.h>
int main(void)
{
//Declaring variables
int integer1, integer2, integer3, product;
return 0;
}
Question 5:
Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference
and area. Use the constant value 3.14159 for π. Perform each of these calculations inside the printf
statement(s). Add comments in your program. (To get full marks your code should be legible, not
only just correct)
#include <stdio.h>
#include <math.h>
#define PI 3.14159f
int main(void)
{
//Declaring variables
int radius, diameter, circumference, area;
//Calculating Diameter
diameter = 2.0f * radius;
//Calculating Circumference
circumference = 2.0f * PI * radius;
//Calculating Area
area = PI * radius * radius;
return 0;
}