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

Fundamentals of Programming

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;

printf("Enter first integer: ");


scanf("%d", &integer1);

printf("Enter second integer: ");


scanf("%d", &integer2);

sum = integer1 + integer2;


printf("Sum is %d\n", sum);

return 0;
}

Question 2:

For each of the following statements, explain why it is not correct, and fix it

a) #Include < stdio.h >;


This statement is a directive and a directive never begin with a capital letter i.e., #Include
and does not end with a semi colon; The correct statement is;
#include <stdio.h>

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

c) printf ( "The product of %d and %d is %d"\n, x, y );


In this statement, the \n was outside of Quotation marks. Also, this statement consists of
placeholders but does not tell compiler what type of variable do we want to display. The
correct statement is:
printf("The product of %d and %d is %d\n", x, y, x *y);
d) int 9value;
In this statement, the identifier 9value is wrong as identifiers always begin with a letter or
underscore. The correct statement will be:
int value9;

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:

Write a single C statement to accomplish each of the following:

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);

c) Print the message "This is a C program." on one line.


printf(“This is a C program.\n”);

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;

//Input and storage of User's value for integer1


printf("Enter first integer: ");
scanf_s("%d", &integer1);

//Input and storage of User's value for integer2


printf("Enter second integer: ");
scanf_s("%d", &integer2);

//Input an storage of User's value for integer3


printf("Enter third integer: ");
scanf_s("%d", &integer3);

//Calculating Product of integers


product = integer1 * integer2 * integer3;
printf("Product is %d\n", 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;

//Input and storage of User's value for radius


printf("Enter radius : ");
scanf_s("%d", &radius);

//Calculating Diameter
diameter = 2.0f * radius;

//Input and storage of User's value for diameter


printf("Diameter is %d\n", diameter);

//Calculating Circumference
circumference = 2.0f * PI * radius;

//Input and storage of User's value for circumference


printf("Circumference is %d\n", circumference);

//Calculating Area
area = PI * radius * radius;

//Input and storage of User's value for area


printf("Area is %d\n", area);

return 0;
}

You might also like