You are on page 1of 10

NAME – Aditya Mishra ROLL NO.

– 180102114 SAP ID - 1000011196

Assignment 1–Programming for Problem Solving, Odd Semester 2021-22

Instructions
 There are 8questions in this assignment.
 This evaluation instrument, if submitted after due date, will not be evaluated,
and a score of zero will be awarded if it is a graded instrument.
 Email/paper/other modes of submissions will not be accepted.
 Upload a pdfversion of this document.

Due Date:10 pm, October16, 2021.

Submitting this Assignment

You will submit the hard copy ofthis assignment.

Grading: 2 points

Questions:

1. WAP in C to calculate the area of a circle.


2. Write an algorithm and flow chart for finding factorial of a number.
3. Find the error/ output:
i. #include<stdio.h>
int main ()
{
void num=10;
printf(“%v”, num);
return 0;
}

ii. #include<stdio.h>
void main ()
{
int a, b;
a=10, b=20;
printf(“%d”, a, b);
}
4. Suppose a, b and c are integer variables that have assigned the values a=8, b=3 and c=-5. De-
termine the value of each of the following arithmetic expressions:
i. 2*b+3*(a-c)
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

Assignment 1–Programming for Problem Solving, Odd Semester 2021-22


ii. a*(c%b)
iii. a/c
iv. a*b/c
v. a-b*a+a*15/3

5. Find the error/ output:


i. #include<stdio.h>
void main ()
{
int x=1, y=2;
y= -- x--;
printf (“%d%d”, x, y);
}

ii. #include<stdio.h>
void main ()
{
int x=20, y=20, z=20;
x==y=z;
printf (“%d”, x);
}

iii. #include<stdio.h>
void main ()
{
int x=5, y=6;
x=x==0;
y==y+1
printf (“%d%d”, x, y);
}

6. WAP in C to convert the temperature from Fahrenheit to Celsius.


7. Write the programming example for each of the following:
i. Syntax Error
ii. Run Time Error
iii. Logical Error
iv. Semantic Error
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

Assignment 1–Programming for Problem Solving, Odd Semester 2021-22

8. Find the error/ output:


i. #include<stdio.h>
void main ()
{
int x=2;
x=x+x*++x;
printf (“%d”, x);
}

ii. #include<stdio.h>
void main ()
{
int x=5.3%2;
printf (“%d”, x);
}

Solutions –
Q1) The required C program is given below –

#include<stdio.h>

int main() {
float radius, area;

printf("\nEnter the radius of Circle : ");


scanf("%f", &radius);

area = 3.14 * radius * radius;


printf("\nArea of Circle : %f", area);

return (0);
}
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

Assignment 1–Programming for Problem Solving, Odd Semester 2021-22

OUTPUT –

Q2) The algorithm for finding the factorial of a number is given below –

Step 1: Start
Step 2: Read a number n
Step 2: Initialize variables:
i = 1, fact = 1
Step 3: if i <= n go to step 4 otherwise go to step 7
Step 4: Calculate
fact = fact * i
Step 5: Increment the i by 1 (i=i+1) and go to step 3
Step 6: Print fact
Step 7: Stop

Below is a flowchart for the same algorithm –


NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

Assignment 1–Programming for Problem Solving, Odd Semester 2021-22

Q3 ) i) It has a syntax error, more appropriately a semantic error. “Void” is NOT a datatype AT
ALL, and specifically for variables in C language. Its just a keyword specifically created to
declare/identify procedures or functions which do not return a value, speaking in the context of
C language.

ii) The print statement will fetch the subsequent arguments for the ‘%d’ format specifier, since
‘a’ is the first argument thus the value corresponding to ‘a’ will get printed, rest of the arguments
will be ignored.

Therfore, OUTPUT - 10

Q4) Given, a = 8, b = 3, c = -5

i) 2*b+3*(a-c)

= 2*b+3*(a-(-5))

= 2*b+3*(8+5)
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

Assignment 1–Programming for Problem Solving, Odd Semester 2021-22

= 2*b+3*13

= 2*3+3*13

= 6+39 = 45

ii) a*(c%b)

= 8*((-5)%3)

= 8*(-2) = 16

iii) a/c

= 8/(-5) = -1

iv) a*b/c

= 8*3/(-5) = 24/(-5) = -4

v) a-b*a+a*15/3

= 8-3*8+8*15/3

= 8-3*8+8*5

= 8-24+40

= -16+40 = 24

Q5) i) In the main function one of the unary decrement operators is syntactically deficient of
recognizing its operand in this statement.
If the compiler treats the operators as postfix because of greater precedence then since their
associativity is left to right thus, the operation couldn’t proceed since no leftmost
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

Assignment 1–Programming for Problem Solving, Odd Semester 2021-22


operand is available. And if the compiler treats them as prefix then since their associativity is
right to left thus still the operation won’t proceed since no rightmost valid operand available.

ii) In main function, in the expression statement “x==y=z”, the assignment operator doesn’t has
a left operand of assignment. Since the relational operator “==” has higher precedence thus the
operands x and y are assigned to it and is evaluated first which has a constant value, therefore the
binary assignment operator “=” doesn’t has the valid left operand i.e. a variable.

iii) The only error is that the print statement isn’t preceeded by a semicolon.

Q6) The required C program is given below –

#include<stdio.h>
int main()
{ float Fahrenheit, Celsius;
printf("Enter the scale in fahrenheit = ");
scanf("%f", &Fahrenheit);
Celsius = ((Fahrenheit-32)*5)/9;
printf("\n\n Temperature in Celsius is : %f",Celsius);
return (0);
}

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

Assignment 1–Programming for Problem Solving, Odd Semester 2021-22

Q7) i) Syntax error

#include<stdio.h>
void main()
{
int x = 10;
int y = 15;

printf("%d", (x, y)) // semicolon missed


}

error: expected ';' before '}' token

ii) Run-time error

#include<stdio.h>
void main()
{
int n = 9, div = 0;

// wrong logic
// number is divided by 0,
// so this program abnormally terminates
div = n/0;

printf("result = %d", div);


}

warning: division by zero [-Wdiv-by-zero]


div = n/0;

iii) Logical Error


NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

Assignment 1–Programming for Problem Solving, Odd Semester 2021-22


int main()
{
int i = 0;

// logical error : a semicolon after loop


for(i = 0; i < 3; i++);
{
printf("loop ");
continue;
}
getchar();
return 0;
}

NO Output.

iv) Semantic Error

void main()
{
int a, b, c;
a + b = c;
}

error: lvalue required as left operand of assignment


a + b = c; //semantic error

Q8) i) In the arithmetic expression statement “x=x+x*++x”, the increment operator has the
highest precedence thus it will be evaluated first. Since the postfix has greater precedence thus it
needs a valid left operand which it doesn’t has, so the compiler will treat the operator as prefix.

Now since in prefix first the variable is incremented and then the expression is evaluated thus
The output will be –

x=x+x*++x ( given, x=2)


NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

Assignment 1–Programming for Problem Solving, Odd Semester 2021-22


x=x+x*x ( x is incremented and thus x = 3 .

x=3+3*3

x=3+9

x=12

ii) Syntax error, since the binary modulo operator can only be applied to interger datatypes and
not floating point or double.

You might also like