Logic development

26
LOGIC DEVELOPMENT CONTROL FLOW ANALYSIS & C BASICS

Transcript of Logic development

Page 1: Logic development

LOGIC DEVELOPMENTCONTROL FLOW ANALYSIS & C BASICS

Page 2: Logic development

1.FIND THE PATTERN!

• n = input (“Enter a positive integer”) • while n > 0

• c = n • while c > 0

• print n • c = c – 1

• end• n = n - 1 • print newline

• end

Page 3: Logic development

Ans : 1

• Input : n=5;• Output:• 55555• 4444• 333• 22• 1

Page 4: Logic development

2.FIND THE OUTPUT!

• int main() { • int a[5] = {5, 1, 15, 20, 25}; • int i, j, m; • i = ++a[1]; • j = a[1]++;• m = a[i++]; • printf("%d, %d, %d", i, j, m);• return 0;

• }

Page 5: Logic development

Ans : 2

• OUTPUT : 2,2,3

• Solution:• A[1]=1;• A[1]=2;• i=2;• J=2• A[1]=3;• i=3• M=A[3]=15

Page 6: Logic development

3.FIND THE OUTPUT!

• int main() { • int i = 1; • switch(i) {

• printf("Hello\n"); • case 1: printf("Hi\n");

• break; • case 2: printf("\nBye\n");

• break;

• }• return 0;• }

Page 7: Logic development

Ans : 3

• OUTPUT: Hi• Solution: Switch uses concept of label;

Page 8: Logic development

4.FIND THE OUTPUT!

• int main() { • char str[]="C-program";• int a = 5;• printf(a >10?"Ps\n":"%s\n", str);• return 0;

• }

Page 9: Logic development

Ans : 4

• OUTPUT : C-program

Page 10: Logic development

5.FIND THE OUTPUT!

What will be the output of the program (sample.c) given below if it is executed from the command line?cmd> sample monday tuesday wednesday thursday/* sample.c */ #include<stdio.h> int main(int argc, char *argv[]) { while(--argc>0) printf("%s", *++argv); return 0;}

Page 11: Logic development

ANS : 5

• OUTPUT :

Page 12: Logic development

6. What does fp points to?

• int main() { • FILE *fp; • fp=fopen("trial", "r");• return 0;

• }

Page 13: Logic development

ANS : 6

Page 14: Logic development

7.FIND THE OUTPUT!

• int main() { • char str[] = "peace";• char *s = str; • printf("%s\n", s++ +3); • return 0;

• }

Page 15: Logic development

ANS : 7

• OUTPUT : ce

Page 16: Logic development

8.FIND THE OUTPUT!

• int main() { • int *p; • p = (int *)malloc(20); • printf("%d\n", sizeof(p)); • free(p); • return 0;

• }

Page 17: Logic development

ANS : 8

• OUTPUT : 4

Page 18: Logic development

9.FIND THE OUTPUT!

What will be the output of the program (sample.c) given below if it is executed from the command line?cmd> sample friday tuesday sunday/* sample.c */ #include<stdio.h> int main(int argc, char *argv[]) { printf("%c", *++argv[2] ); return 0; }

Page 19: Logic development

ANS: 9

• OUPUT : u• SOLUTION: argv[2]= Tuesday

• ++ argv[2] =uesday;• %c of ++argv[2] = u

Page 20: Logic development

10.FIND THE OUTPUT!

• int main() { • short int i = 0; • for(i<=5 && i>=-1; ++i; i>0)

• printf("%u,", i); • return 0;

• }

Page 21: Logic development

11.FIND THE OUTPUT!

• int main() { • int y=128; • const int x=y; • printf("%d\n", x); • return 0; • }

Page 22: Logic development

ANS : 11

• OUTPUT : 128

Page 23: Logic development

12.FIND THE OUTPUT!

• int main() { • struct emp {

• char n[20]; • int age;• };

• struct emp e1 = {"Dravid", 23};• struct emp e2 = e1;• if(e1 == e2)

• printf("The structure are equal");• return 0;

• }

Page 24: Logic development

ANS :12

• OUTPUTS NOTHING;• Error occurs at run time..! Struct data structures cannot be

compared using ‘==‘ Operator

Page 25: Logic development

13.FIND THE OUTPUT!

• int main() { • printf(5+"IndiaBIX\n"); • return 0;

• }

Page 26: Logic development

ANS : 13

• OUTPUT : BIX