Week 1 Questions Sr. No. Options Answer & Explanation
Week 1 Questions Sr. No. Options Answer & Explanation
}
}
3 Find the output: A. 1,10 Answer: C
#include<stdio.h> B. 9,11
int main() C. 9,19 Explanation: The expression
{ D. 1,19 b*c+d-a gets evaluated first,
int x=10; which then sets the value of a
int a=1,b=2,c=3,d=4; to 9. This value is used for
x+=a=b*c+d-a; incrementing x.
printf("%d,%d",a,x);
}
printf("%d",a);
}
int a=5; = 5 % ( 5 − 2 ) ∗ (5 − 3 ) +
int b = a % ( a−a /2 ) ∗ ( a 5
−3)+a; =5%3∗2+5
=2∗2+5
printf(”%d”,b);
=9
return 0 ;
}
14 #include<stdio.h> a. 1 a
b. 2 The precedence order is *, +,
int main(){ c. 4 >> and lastly +=
int i=1; d. 7 Therefore Ans = 1 + ((1
i += i*i>>2+3; *1)>>(2+3)) = 1 + ((1)>>(5)) =
printf("%d",i); 1+0 = 1
return 0;
}
Give the result of compiling and
running the above program?
15 #include<stdio.h> a. 15 c.
b. 24
int main(){ c. 33 The precedence order is *, +,
int i=1; d. 34 >> and lastly +=
i += i*i<<2+3; Therefore Ans = 1 + ((1
printf("%d",i); *1)<<(2+3)) = 1 + ((1)<<(5)) =
return 0; 1+32 = 33
}
Give the result of compiling and
running the above program?
Programming Question #1
Write a program to find the number of perfect squares between given two numbers A and B
(both inclusive). A number is called a perfect square if it can be written as x*x for some integer x.
Constraints:
Both A and B are positive. They both are less than 100,000.
Example 2:
Input: 16 70
Output: 5
Solution:
#include<stdio.h>
int main(){
int x,y,i,a;
int count=0,flag=0;
scanf("%d %d",&x,&y);
for(i=x;i<=y;i++){
for(a = 0; a <= i; a++)
{
if (i == a * a)
{
flag=1;
}
}
if(flag==1)
count++;
flag=0;
}
printf("%d",count);
return 1;
}
Programming Question #2
Write a program to find whether a given number (say x) is a “perfect number” or not.
Explanation:
Take number 6.
Proper positive divisors of 6 is 1,2,3 and their sum is 1+2+3=6.
So, 6 is a perfect number.
Constraint:
x>=1
Example 1:
Input: 6
Output: yes
Example 2:
Input: 7
Output: no
Solution:
#include<stdio.h>
int main(){
int x,i,sum=0;
scanf("%d",&x);
for(i=1;i<x;i++){
if(x%i==0){
sum+=i;
}
}
if(sum==x){
printf("yes");
}else{
printf("no");
}
return 1;
}
Programming Qn #3
Write a C program that takes a positive number N and produces an output that is the product of
its digits.
Explanation:
Take number 657.
Answer expected : 6 * 5 * 7 = 210
Constraint:
1<=N<=999999
Input: A single number
Output:
The value
Example 1:
Input: 657
Output: 210
Example 2:
Input: 7
Output: 7
Programming Qn #4
Given three points (x1, y1), (x2, y2) and (x3, y3) , write a program to check if all the
three points fall on one straight line.
INPUT:
Six integers x1, y1, x2, y2, x3, y3 separated by whitespace.
OUTPUT:
Print “Yes” if all the points fall on straight line, “No” otherwise.
CONSTRAINTS:
-1000 <= x1, y1, x2, y2, x3, y3 <= 1000
2. -2 0 -2 1 -2 2 Yes
3. -62 14 -18 -23 -6 23 No