Assignment I
Assignment I
CS3401 – ALGORITHMS
ASSIGNMENT – 1.1
Submission Date: 18.03.2024 (Before 01.00PM)
Answer All the Questions
1. Trace the following program and find the output of the following snippet.
#include<stdio.h> int main()
int f1(int x) {
{ int n;
int b; n=f1(4);
if(x<1) printf("%d",n);
return 1; return 0;
else }
b=x*f1(x-2);
return b;
}
2. Find the output.
printf("%d", n%2);
fun(n/2);
}
5. Find the Output.
#include<stdio.h>
void print(int n)
{
if (n > 4000)
return;
printf("%d ", n);
print(2*n);
printf("%d ", n);
}
int main()
{
print(1000);
return 0;
}
6. Which keyword can be used for coming out of recursion?
7. Which loop is guaranteed to execute at least one time?
8. How long the following loop runs and find the output?
for(x = 10; x = 20; x++) ;
printf(“%d”,x);
9. What is the purpose of switch statement?
10. Find the output of the following snippet.
void main()
{
int num = 0;
do
{
- - num;
printf(“%d”, num);
num ++;
}
while(num >= 0);
}
11. What is the value of the grade after execution of the switching statement?
marks = 80;
switch(marks)
{
default:
grade = „E‟;
case 60:
grade = „C‟;
break;
case 70:
grade = „B‟;
break;
case 80:
grade = „A‟;
}
12. Find the output of the following statement.
#include <stdio.h>
int main()
{
int value = 0;
if(value)
printf("wellcome");
printf("All");
return 0;
}
13. Find the output of the following snippet.
#include <stdio.h>
int main()
{
for(i = 0;i < 10; i++);
printf("%d", i);
return 0;
}
14. What is the output of the following snippet?
#include <stdio.h>
int main()
{
int digit = 0;
for(; digit <= 9; )
digit++;
digit *= 2;
--digit;
return 0;
}
15. Find the output of the following statement.
#include <stdio.h>
int main()
{
int i=32767;
for (i = 1; i >= 0; i++)
printf("%d\n", i);
}
16. What is the output of the following statement?
#include <stdio.h>
void main()
{
double k = 0;
for (k = 0.0; k < 3.0; k++)
printf("Hello");
}
17. What is an infinite loop? Give an example.
18. Find the output of the following statement.
void main()
{ int i;
for(i = 0; i < 10; i++)
{
if(i % 2 == 0)
{
continue;
}
printf(“%d”,i);
}
}
19. Find the output.
void main()
{ int i=0;
while( i < 10)
{
if(i % 2 == 0)
{
continue;
}
printf(“%d”,i);
i++;
}
20. What is the output?
void main()
{
int a[] = {0, 0x4, 4, 9};
int i = 2;
printf(“%d %d”, a[i], i[a]);
}
21. What is the output of the following program?
int cap(int n)
{
if (n <=1)
return 1;
else
return(cap(n -3) + cap(n-1));
}
main()
{
int n;
n = cap(6);
printf(“%d”, n);
}
22. What is the correct way to access the array element a[3] and a[2][3] using pointer.
23. What is the output of the following statement?
#include <stdio.h>
int main()
{
static int a[] = {08, 010, 0x20, 30, -42};
static int *p[] = {a+2, a, a+4, a+3, a+1};
int **ptr;
ptr = p;
**++ptr;
printf(“%d %d”, **ptr, ptr - p);
return 0;
}
24. What is the output?
#include <stdio.h>
int main()
{
char a[] = “Computer Science and Engineering”;
char *b =”Computer Science”;
printf(“%d %d”, sizeof(a), sizeof(b));
return 0;
}
25. Find the output.
#include <stdio.h>
main()
{
char s1[] = “Ramko”;
char s2[] = “System”;
s1 = s2;
printf(“%s”, s1);
}