1. 2. Write a program that calculates series
𝑆 = ∑
1
2 𝑛
𝑁
𝑛=1
from lab 3
sum=0;
for(i=1;i<=N,i++)
{
term=1.0;
for(j=1;j<=I,j++)
term=term/j;
|
sum=sum+term;
printf(“Sum s is %f”,sum);
sum=0;
for(i=1;i<=N,i++)
{
term=1.0;
for(j=1;j<=I,j++)
term=term/2*j;// only changes
|
sum=sum+term;
printf(“Sum s is %f”,sum);
3. Find errors in following code
C version
Questions Answer
#include<studio.h> #include <stdio.h>
#include <math.h> //since the code has
power function
int main(); int main()
{ {
int x; int x;
int z=3.0; int z=3;
int y=6;//float or double or fix it into int, and
also define y since it is involved in the later
part of the code
float nums float nums // rest of the program is num, you
fix that or fix this
printf(“Enter a number.”) Printf(“Enter a number.”);
printf(“Enter a number.”) Printf(“Enter a number.”);
2. scanf(“%d”,num) scanf(“%d”,&num);
x=(y%z)+pow(num,2); x=(y%z)+pow(num,2); //(y%z is modulus
function and only takes integer that’s why z
has to be integer)
printf(“x=%f”,”x); printf(“x=%f”,(float) x); //extra quotation
mark, even x became float, it should be
declared as float here just for confirmation
}; return 0;
}
Multiple choice
Q1)What command we use to stop Visual Studio from closing terminal when running a program
d) system(“pause”)
Q2)Which of the following loops do not have proper syntax
(a) semi colon missing
(b) for(control variable start; conditional criteria end; variable increment/decrement)
{
command
} //semicolon missing
(c) whale(conditional criteria) //it is while
{
commands
}
(d) for(control variable start; conditional criteria end; variable increment/decrement) command
Q3) the value of (A<D) &&(!(C==0))||((C==3)||(!B)) with A=-1.0, B=2.3, C=0, D=-5.0 is
F&&T||(T||F) = F||T=True
Q4)(F4B)16 is
Either A or C
Q5) printing C(everything) cause there is no break statement.
Q6) which of the following are equivalent
A,b,d,f
X++; ++x; x=x+1;x+=1