Assignment 1

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

Course Name: Structured Programming Language

Course Code: 0613-1107

Submitted to:
Abul Hasnat Md. Saiful Islam
Professor
Department of CSE,NDUB

Submitted by:
Md. Safayet Hossain
ID: 0692320005101034
Department of CSE, NDUB.
Date of Submission: September 6 , 2023

SPL @ CSE 22 ASSIGNMENT 1


1. [CLO1] [3] a:-
Here are the numbers in normal decimal notation:
103e-4 = 0.0103
1.2345e+6 = 1,234,500
123.45e+3 = 123,450

1. [CLO1] [3] b:-


In C scientific notation, numbers are represented in the form of "mantissa x 10^exponent".
Let's convert the given numbers:
1. 1300:
Scientific notation: 1.3e3
2. 123.45:
Scientific notation: 1.2345e2
3. 0.00426:
Scientific notation: 4.26e-3

2. [CLO3] [5]:-
Let's go through each constant and determine if it is a valid type in C and identify its data
type if applicable:
1. 'PQR' - This is not a valid constant in C. It consists of multiple characters enclosed in single
quotes, which suggests a character literal. However, character literals in C can only contain
a single character.
2. 15E-2 - This is a valid constant in C. It is a floating-point number written in scientific
notation. The data type for this constant is `double`.
3. 35 - This is a valid constant in C. It is an integer literal, and the data type for this constant is
`int`.
4. 'h' - This is a valid constant in C. It is a character literal, and the data type for this constant
is `char`.
5. -37.491 - This is a valid constant in C. It is a floating-point number, and the data type for
this constant is `double`.
6. .912 - This is a valid constant in C. It is a floating-point number, and the data type for this
constant is `double`.

7. 4,719 - This is not a valid constant in C. Commas are not allowed in numeric literals.
8. 'true' - This is not a valid constant in C. In C, the keyword for a Boolean true value is `true`
(without quotes) when using the `<stdbool.h>` library. Without including the library, `true`
is not recognized as a keyword.
9. "T" - This is a valid constant in C. It is a string literal containing a single character, and the
data type for this constant is `char`.
10. 4.5e3 - This is a valid constant in C. It is a floating-point number written in scientific
notation, and the data type for this constant is `double`.
11. '$' - This is a valid constant in C. It is a character literal, and the data type for this constant
is `char`.
3. [CLO3] [2]:-
In C, the most appropriate variable types for the area of a circle in square inches and the
number of cars passing through an intersection in an hour would be:
1. Area of a circle in square inches: `double`
Reason: The area of a circle can have fractional values, and `double` provides higher
precision compared to other floating-point types like `float`. It is suitable for storing real
numbers with decimal points.
2. Number of cars passing through an intersection in an hour: `int`
Reason: The number of cars passing through an intersection is typically a whole number
(non-negative), and it is unlikely to be a fractional value. Thus, `int` (integer) is a suitable
variable type to represent this quantity.
Here's a C program to compute the area of a circle and the number of cars passing through
an intersection in an hour:

#include <stdio.h> int main() { //


Area of a circle double radius,
area; const double pi =
3.14159265359;
printf("Enter the radius of the circle in inches: "); scanf("%lf", &radius); area = pi
* radius * radius; printf("The area of the circle is: %lf square inches\n", area); //
Number of cars passing through an intersection int carsPerHour; printf("Enter
the number of cars passing through the intersection in an hour: "); scanf("%d",
&carsPerHour);
printf("The number of cars passing through the intersection in an hour is: %d\n",
carsPerHour); return 0;
}
In this program, the area of the circle is calculated using the formula: `area = pi * radius *
radius`, and the number of cars passing through the intersection is taken as user input using
`scanf` function with the `%d` format specifier. The output is given below:-

4. [CLO2 and CLO3] [1]:-


Let's walk through the program step by step with the given inputs (5 and 7).
#include <stdio.h> int main() {
int m, n; printf("Enter two
integers> ");
scanf("%d%d", &m, &n); m
= m + 5;
n = 3 * n;
printf("m = %d\nn = %d\n", m, n); return
0;
}
The output is as follows:-

Explanation:
1. The program prompts the user to enter two integers.
2. The user enters "5" and "7".
3. The program reads these values into variables `m` and `n`.
4. `m` is then updated with the value of `m + 5`, which becomes 5 + 5 = 10. 5. `n` is updated
with the value of `3 *

5. [CLO2 and CLO3] [2]:-


The solve of this problem is given below:-
#include <stdio.h> int
main() { int exp = 11;
printf("My name is ");
printf("Abrar Hossain.");
printf("\n"); printf("I live
in ");
printf("Dhaka, BD\n"); printf("and I have
%d years ", exp); printf("of
programming experience.\n"); return 0;
}
Compile and run this program. It will display the following output :-
6. [CLO2 and CLO3] [4]:-
Here’s the C program created using the given informations:-
#include <stdio.h> #define
PI 3.14159
int main() { double radius, area;
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
// Calculate the area of the circle area = PI *
radius * radius; printf("The area of the circle
is: %lf\n", area); return 0;
}
Compile and run this program, and it will ask the user to enter the radius of the circle. After
the user provides the input, it will display the computed area of the circle. The output is
shown below:-
7. [CLO2] [3]:-
The C of the problem solved is given below:-
#include <stdio.h> int main() { printf("%d\t", sizeof(6.5)); // Size of a floating-point
literal (6.5) - likely 4 or 8 bytes printf("%d\t", sizeof(90000)); // Size of an integer
literal (90000) - likely 4 bytes printf("%d", sizeof('A')); // Size of a character literal
('A') - likely 1 byte return 0;
}
The output is given below:-

You might also like