39
PROGRAMING IN “C” DEPT OF COMPUTER SCIENCE & ENGINEERING NALANDA INSTITUTE OF TECHNOLOGY, BHUBANESWAR Prepared by: Narottam Sahu

PROGRAMING IN “C” - Nalanda Institute of Technology

Embed Size (px)

Citation preview

PROGRAMING IN “C”

DEPT OF COMPUTER SCIENCE & ENGINEERING

NALANDA INSTITUTE OF TECHNOLOGY, BHUBANESWAR

Prepared by: Narottam Sahu

1. What is pointer on pointer?

Ans:- It’s a pointer variable which can hold the address of another pointer variable. It de-refers twice to point to

the data held by the designated pointer variable.

Eg: int x = 5, *p=&x, **q=&p;

2.What do you mean by Dangling pointer variable in C programing ?

Ans:- A pointer in C programing is used to point the memory location of an existing variable. In case if

that particular variable is deleted and the pointer is still pointing to the same memory location then that

particular pointer variable is called as a Dangling pointer variable.

3.What is NULL pointer?

Ans :- NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should

initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a

pointer NULL when memory pointed by it is deallocated in the middle of a program.

4.What is difference between including the header file with-in angular < > and

double quotes “ “.

Ans:- If a header file is included with in < > then the compiler searches for the particular header file only

with in the built in include path. If a header file is included with in “ “, then the compiler searches for the

particular header file first in the current working directory, if not found then in the built in include path.

5.How a -ve integer is stored?

Ans:- Get the two’s compliment of the same positive integer. Eg: 1011 (-5)

Step-1 − One’s compliment of 5 : 1010

Step-2 − Add 1 to above, giving 1011, which is -5

6.What is the difference between near, far and huge pointers?

Ans:- These are some old concepts used in 16 bit Intel architectures in the days of MS DOS, not much

useful anymore.

Near pointer is used to store 16 bit addresses means within current segment on a 16 bit machine. The limitation is that we can only access 64kb of data at a time.

A far pointer is typically 32 bit that can access memory outside current segment. To use this, compiler allocates a segment register to store segment address, then another register to store offset within current segment.

Like far pointer, huge pointer is also typically 32 bit and can access outside segment. In case of far pointers, a segment is fixed. In far pointer, the segment part cannot be modified, but in Huge it can be

NEAR POINTER FAR POINTER

It’s size is 2 bytes Its size is 4 bytes

They have the address in between 0-65535(i.e in user area)

They have the address more than 65535(i.e out of user area)

For Example: simple pointers, which we normally studied in C and C++

Pointers which are used in devices, running program, i.e to attack on other computers via this far pointers.

7. What are stack and heap areas?

Ans:- Heap Area: It is used for the objects allocated dynamically (Using malloc() and calloc()). Stack Area: It is used to store local variables and arguments of a method. This stays in memory only till the termination of that particular method.

8.What is bit field?

Ans:- We can create integer structure members of differing size apart from non-standard size using bit

fields. Such structure size is automatically adjusted with the multiple of integer size of the machine.

9.What are command line arguments?

Ans:- The arguments which we pass to the main() function while executing the program are called as command line arguments. The parameters are always strings held in the second argument (below in args) of the function which is array of character pointers. First argument represents the count of arguments (below in count) and updated automatically by operating system.

main( int count,char *arg[]){ }

10. What are the difference ways of passing parameters to the function?

Ans:- Call by value − We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used.

Call by reference − We send address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.

11. Wher the address operator (&) cannot be used?

Ans:- It cannot be used on constants.

It cannot be used on variable which are declared using register storage class.

12. Is FILE is a built-in data type?

Ans:- No, it is a structure defined in stdio.h.

13. Which key word is used to perform unconditional branching?

Ans:- goto

14. What are enumeration?

Ans:- Enumerations are list of integer constants with name. Enumerators are defined with the

keyword enum.

15. What is the full form of ANSI?

Ans:- American National Standards Institute.

16. Can we assign a float variable to a long integer variable?

Ans:- Yes, with loss of fractional part.

17. What is recursion?

Ans:- Function calling itself is called as recursion.

18. Which function can be used to release the dynamic memory allocation?

Ans:- free()

19. What is the first string in the argument vector w.r.t command line argument?

Ans:- Programe name

20. What is the output file generated by the linker?

Ans:- Linker generates the executable file.

21. Describe wild pointer in C?

Ans:- Uninitialized pointers in the C code are known as Wild Pointers. They point to some arbitrary

memory location and can cause bad program behaviour or program crash.

22. Describe the difference between = and == symbol in C programing?

Ans:- ‘==' is the comparison operator which is used to compare the value or expression on the left-hand

side with the value or expression on the right-hand side.

‘=' is the assignment operator which is used to assign the value of the right-hand side to the variable on the left-hand side.

23. What are the different storage classes in C?

Ans:- There are four types of storage classes in C. They are extern, register, auto and static.

24. What does static variable mean?

Ans:- Static is an access qualifier. If a variable is declared as static inside a function, the scope is limited

to the function,but it will exists for the life time of the program. Values will be persisted between successive calls to a function.

25. What are macros? What are its advantages and disadvantages?

Ans:- Macros are processor directive which will be replaced at compile time.

The disadvantage with macros is that they just replace the code they are not function calls. similarly the advantage is they can reduce time for replacing the same values.

26. What are the differences between structures and arrays?

Ans:- Arrays is a group of similar data types but Structures can be group of different data types.

27. Differentiate between null and void pointer.

Ans:- A null pointer is a pointer which points to nowhere. It has the value zero. Void pointer is a generic

pointer which is introduced by ANSI (American National Standards Institute).

28. What is the main difference between the Compiler and the Interpreter?

Ans:- Compiler is used in C Language and it translates the complete code into the Machine Code in one

shot. On the other hand, Interpreter is used in Java Programming Langauge and other high-end programming languages. It is designed to compile code in line by line fashion.

29. What is /0 character?

Ans:- The Symbol mentioned is called a Null Character. It is considered as the terminating character used

in strings to notify the end of the string to the compiler.

30. Why is C called the Mother of all Languages?

Ans:- C introduced many core concepts and data structures like arrays, lists, functions, strings, etc.

Many languages designed after C are designed on the basis of C Language. Hence, it is considered as the mother of all languages.

31. Differentiate between getch() and getche()?

Ans:- Both the functions are designed to read characters from the keyboard and the only difference is

that

getch(): reads characters from the keyboard but it does not use any buffers. Hence, data is not displayed on the screen.

getche(): reads characters from the keyboard and it uses a buffer. Hence, data is displayed on the screen.

32. Differentiate between Actual Parameters and Formal Parameters.

Ans:- The Parameters which are sent from main function to the subdivided function are called as Actual

Parameters and the parameters which are declared a the Subdivided function end are called as Formal Parameters.

33. What is bit field?

Ans:- We can create integer structure members of differing size apart from non-standard size using bit

fields. Such structure size is automatically adjusted with the multiple of integer size of the machine.

34. Predict the output of below programs.

#include<stdio.h>

int main() { int n; for(n = 7; n!=0; n--) printf("n = %d", n--); getchar(); return 0; }

Output:Above program goes in infinite loop because n is never zero when loop condition (n != 0) is checked.

35. Predict the output of below programs. #include <stdlib.h> #include <stdio.h> enum {false, true}; int main() { int i = 1; do { printf("%d\n", i); i++; if (i < 15) continue; } while (false); getchar(); return 0; } Output: 1 Explanation: The do wile loop checks condition after each iteration. So after continue statement, control transfers to the statement while(false). Since the condition is false ‘i’ is printed only once.

36. Predict the output of below programs. char *getString() { char *str = "Nice test for strings"; return str; } int main() { printf("%s", getString()); getchar(); return 0; } Output: “Nice test for strings” The above program works because string constants are stored in Data Section (not in Stack Section). So, when getString returns *str is not lost.

37. Predict the output of below programs. #include<stdio.h> int main() { static int i=5; if(--i){ main(); printf("%d ",i); } }

Output: 0 0 0 0 Explanation: Since i is a static variable and is stored in Data Section, all calls to main share same i.

38. Predict the output of below programs. #include<stdio.h> int main() { static int var = 5; printf("%d ",var--); if(var) main(); } Output: 5 4 3 2 1

39. Predict the output of below programs. # include <stdio.h> int main() { int i=0; for(i=0; i<20; i++) { switch(i) { case 0: i+=5; case 1: i+=2; case 5: i+=5; default: i+=4; break; } printf("%d ", i); } getchar(); return 0; }

Output: 16 21 Explanation: Initially i = 0. Since case 0 is true i becomes 5, and since there is no break statement till last statement of switch block, i becomes 16. Before starting the next iteration, i becomes 17 due to i++. Now in next iteration no case is true, so execution goes to default and i becomes 21.

In C, if one case is true switch block is executed until it finds break statement. If no break statement is present all cases are executed after the true case.

40. Predict the output of below programs.

#include <stdio.h> int main()

{ printf("%p", main); getchar(); return 0; }

Output: Address of function main. Explanation: Name of the function is actually a pointer variable to the function and prints the address of the function.

41. Predict the output of below programs. #include <stdio.h> int main() { int i; i = 1, 2, 3; printf("i = %d\n", i); getchar(); return 0; }

Output: 1 The above program prints 1. Associativity of comma operator is from left to right, but = operator has higher precedence than comma operator. Therefore the statement i = 1, 2, 3 is treated as (i = 1), 2, 3 by the compiler.

42. Predict the output of below programs. #include <stdio.h> int main() { int i; i = (1, 2, 3); printf("i = %d\n", i); getchar(); return 0; }

Output: 3

43. Predict the output of below programs. #include‹stdio.h› int main() { struct site { char name[] = "GeeksforGeeks"; int no_of_pages = 200; }; struct site *ptr; printf("%d",ptr->no_of_pages); printf("%s",ptr->name); getchar(); return 0; }

Output: Compiler error

Explanation: Note the difference between structure/union declaration and variable declaration. When you declare a structure, you actually declare a new data type suitable for your purpose. So you cannot initialize values as it is not a variable declaration but a data type declaration.

44. Predict the output of below programs. int main() { char a[2][3][3] = {'g','e','e','k','s','f','o', 'r','g','e','e','k','s'}; printf("%s ", **a); getchar(); return 0; }

Output: geeksforgeeks

Explanation: We have created a 3D array that should have 2*3*3 (= 18) elements, but we are initializing only 13 of them. In C when we initialize less no of elements in an array all uninitialized elements become ‘\0’ in case of char and 0 in case of integers.

45. Predict the output of below programs. #include <stdio.h> int fun(int n) { int i, j, sum = 0; for(i = 1;i<=n;i++) for(j=i;j<=i;j++) sum=sum+j; return(sum); } int main() { printf("%d", fun(15)); getchar(); return 0; }

Output: 120

46. Predict the output of below programs. int main() { unsigned int i=10; while(i-- >= 0) printf("%u ",i); return 0; }

Output: 9 8 7 6 5 4 3 2 1 0 4294967295 4294967294 …… (on a machine where int is 4 bytes long) 9 8 7 6 5 4 3 2 1 0 65535 65534 …. (on a machine where int is 2 bytes long) Explanation: Let us examine the condition of while loop. It is obvious that as far as the condition of while loop is met, printf will be executed. There are two operators in the condition of while loop: post-decrement operator and comparison operator. From operator precedence, we know that unary operator post-decrement has higher priority than comparison operator. But due to post-decrement property, the value of i will be decremented only after it had been used for comparison. So at the first iteration, the condition is true because 10>=0 and then i is decremented. Therefore 9 will be printed. Similarly the loop continues and the value of i keeps on decrementing. Let us see what what happen when condition of while loop becomes 0 >= 0. At this time, condition is met and i is decremented. Since i is unsigned integer, the roll-over happens and i takes the value of the highest +ve value an unsigned int can take. So i is never negative. Therefore, it becomes infinite while loop.

47. Predict the output of below programs. int main() { int a[10]; printf("%d",*a+1-*a+3); return 0; } Output: 4

48. Predict the output of below programs. int main() { unsigned int i=65000; while ( i++ != 0 ); printf("%d",i); return 0; } Output: 1

Explanation: It should be noticed that there’s a semi-colon in the body of while loop. So even though, nothing is done as part of while body, the control will come out of while only if while condition isn’t met. In other words, as soon as i inside the condition becomes 0, the condition will become false and while loop would be over. But also notice the post-increment operator in the condition of while. So first i will be compared with 0 and i will be incremented no matter whether condition is met or not. Since i is initialized to 65000, it will keep on incrementing till it reaches highest positive value. After that roll over happens, and the value of i becomes zero. The condition is not met, but i would be incremented i.e. to 1. Then printf will print 1.

49. Predict the output of below programs. int main() { char str[] = "NALANDA"; int i; for(i=0; str[i]; i++) printf("\n%c%c%c%c", str[i], *(str+i), *(i+str), i[str]);

getchar(); return 0; }

Output:- NNNN AAAA LLLL AAAA NNNN DDDD AAAA Explanaition: Following are different ways of indexing both array and string.

arr[i] *(arr + i) *(i + arr) i[arr]

So all of them print same character.

50. Predict the output of below programs. int main() { char *p; printf("%d %d ", sizeof(*p), sizeof(p)); getchar(); return 0; } Output: Compiler dependent. I got output as “1 4”

Explanation: Output of the above program depends on compiler. sizeof(*p) gives size of character. If characters are stored as 1 byte then sizeof(*p) gives 1. sizeof(p) gives the size of pointer variable. If pointer variables are stored as 4 bytes then it gives 4.

51. Predict the output of below programs. int main() { char arr[] = {1, 2, 3}; char *p = arr; if(&p == &arr) printf("Same"); else printf("Not same"); getchar(); } Output: Not Same &arr is an alias for &arr[0] and returns the address of the first element in array, but &p returns the address of pointer p.

52. Predict the output of below programs. int x = 0; int f() { return x; } int g() { int x = 1; return f(); } int main() { printf("%d", g()); printf("\n"); getchar(); } Output: 0 In C, variables are always statically (or lexically) scoped. Binding of x inside f() to global variable x is defined at compile time and not dependent on who is calling it. Hence, output for the above program will be 0.

53. Predict the output of below programs. #include<stdio.h> int main() { typedef int i; i a = 0; printf("%d", a); getchar(); return 0; } Output: 0 There is no problem with the program. It simply creates a user defined type i and creates a variable a of type i.

54. Predict the output of below programs. #include<stdio.h> int main() { typedef int *i; int j = 10; i a = &j; printf("%d", *a); getchar(); return 0; } Output: 10

55. Predict the output of below programs. #include<stdio.h> int fun(int n, int *fg) {

int t, f; if(n <= 1) { *fg = 1; return 1; } t = fun(n-1, fg); f = t + *fg; *fg = t; return f; } int main( ) { int x = 15; printf ( "%d\n", fun (5, &x)); getchar(); return 0; } Out put: 8

56. Predict the output of below programs. void fun(int *p) { static int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); printf("%d", *p); getchar(); return 0; } Output: 20 Inside fun(), q is a copy of the pointer p. So if we change q to point something else then p remains unaffected.

57. Predict the output of below programs. void fun(int **p) { static int q = 10; *p = &q; } int main() { int r = 20; int *p = &r; fun(&p); printf("%d", *p); getchar();

return 0; } Output 10

Note that we are passing address of p to fun(). p in fun() is actually a pointer to p in main() and we are changing value at p in fun(). So p of main is changed to point q of fun().

58. Predict the output of below programs. int main() { int x, y = 5, z = 5; x = y==z; printf("%d", x); getchar(); return 0; } Out put=1 The crux of the question lies in the statement x = y==z. The operator == is executed before = because precedence of comparison operators (<=, >= and ==) is higher than assignment operator =. The result of a comparison operator is either 0 or 1 based on the comparison result. Since y is equal to z, value of the expression y == z becomes 1 and the value is assigned to x via the assignment operator.

59. Predict the output of below programs. #include<stdio.h> int main() { int a[] = {1, 2, 3, 4, 5, 6}; int *ptr = (int*)(&a+1); printf("%d ", *(ptr-1) ); getchar(); return 0; } Output: 6 &a is address of the whole array a[]. If we add 1 to &a, we get “base address of a[] + sizeof(a)”. And this value is typecasted to int *. So ptr – 1 points to last element of a[].

60. Predict the output of below programs. #include <stdio.h> int main() { int a = 10, b = 20, c = 30; if (c > b > a) { printf("TRUE"); } else { printf("FALSE"); } getchar();

return 0; } Output: FALSE Let us consider the condition inside the if statement. Since there are two greater than (>) operators in expression “c > b > a”, associativity of > is considered. Associativity of > is left to right. So, expression c > b > a is evaluated as ( (c > b) > a ) which is false.

61. Predict the output of below programs. #include<stdio.h> int main() { enum channel {star, sony, zee}; enum symbol {hash, star}; int i = 0; for(i = star; i <= zee; i++) { printf("%d ", i); } return 0; } Output:

compiler error: redeclaration of enumerator 'star'

In the above program, enumartion constant ‘star’ appears two times in main() which causes the error. An enumaration constant must be unique within the scope in which it is defined. The following program works fine and prints 0 1 2 as the enumaration constants automatically get the values starting from 0.

62. Predict the output of below programs. #include<stdio.h> int main() { int i, j; int p = 0, q = 2; for(i = 0, j = 0; i < p, j < q; i++, j++) { printf("NALANDA\n"); } return 0; } Out put:- NALANDA NALANDA When two expressions are separated by comma operator, the first expression (i < p) is executed first. Result of the first expression is ignored. Then the second expression (j < q) is executed and the result of this second expression is the final result of the complete expression (i < p, j < q). The value of expression 'j < q' is true for two iterations, so we get "NALANDA" two times on the screen.

63. Predict the output of below programs. #include<stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int *ptr1 = arr; int *ptr2 = arr + 5; printf ("ptr2 - ptr1 = %d\n", ptr2 - ptr1); printf ("(char*)ptr2 - (char*) ptr1 = %d", (char*)ptr2 - (char*)ptr1); getchar(); return 0; } OUT PUT:- ptr2 - ptr1 = 5

(char*)ptr2 - (char*) ptr1 = 20

In C, array name gives address of the first element in the array. So when we do ptr1 = arr, ptr1 starts pointing to address of first element of arr. Since array elements are accessed using pointer arithmetic, arr + 5 is a valid expression and gives the address of 6th element. Predicting value ptr2 – ptr1 is easy, it gives 5 as there are 5 inetegers between these two addresses. When we do (char *)ptr2, ptr2 is typecasted to char pointer. In expression “(int*)ptr2 – (int*)ptr1”, pointer arithmetic happens considering character poitners. Since size of a character is one byte, we get 5*sizeof(int) (which is 20) as difference of two pointers.

64. Predict the output of below programs. #include <stdio.h> int main() { int arr[] = {}; printf("%d", sizeof(arr)); return 0; } Out put:- 0 C (or C++) allows arrays of size 0. When an array is declared with empty initialization list, size of the array becomes 0.

65. Predict the output of below programs. main() { printf("\n main Called Again"); main(); } Answer : Infinite loop Description : There is no condition in the main() to stop the recursive calling of the main() hence it will be called infinite no of times.

66. Predict the output of below programs. #include<stdio.h> int main() { int x = 10; float y = 10.0;

if (x == y) printf("x and y are equal"); else printf("x and y are not equal"); } Answer : x and y are equal Description : if (x == y) here we are comparing if (10 == 10.0) hence this condition is satisfied. Because we cannot compare int and float so the int is converted to float and then compared. Hence it prints “x and y are equal”.

67. Predict the output of below programs. #include<string.h> main() { char *str1 = "abcd"; char str2[] = "abcd"; printf("%d %d %d", sizeof(str1), sizeof(str2), sizeof("abcd")); } Answer : 8 5 5 Description : First size of character pointer is printed which is 8 on the machine this program is run. Then the size of string str2 whose size is 5 (including the ‘/0’ termination character) is printed. The third is similar to second one.

68. Predict the output of below programs. #include "stdio.h" int main() { int _ = 18; int __ = 38; int ___; ___ = _ + __; printf ("%i", ___); return 0; } Answer : 56 Description :Variable name can have only underscore.

69. Predict the output of below programs. main() { int x = 41, y = 43; x = y++ + x++; y = ++y + ++x; printf ("%d %d", x , y); } Out put:- 86 130 Description : Its actually compiler dependent. After x = y++ + x++, the value of x becomes 85 and y becomes 44, And y = ++y + ++x will be computed as y = (44) + (86). After computation y becomes 130.

70. Predict the output of below programs. main()

{ int x = 7; printf ("%d, %d, %d", x, x<<5, x>>5); } Answer : 7, 224, 0 Description : As x = 7 so first %d gives 7, second %d will take value of x after left shifting it five times, and shifting is done after converting the values to binary, binary value of 7 (000111) will be left shifted twice to make it binary 224(11100000), so x<<5 is 224 and as left shifting does not effect the original value of x its still 5 so third %d will also show 0.

71. Fibonacci Series in C without recursion.

#include<stdio.h>

int main()

{

int n1=0,n2=1,n3,i,number;

printf("Enter the number of elements:");

scanf("%d",&number);

printf("\n%d %d",n1,n2);//printing 0 and 1

for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed

{

n3=n1+n2;

printf(" %d",n3);

n1=n2;

n2=n3;

}

return 0;

}

Out put:- Enter the number of elements:15 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

72. Fibonacci Series in C using recursion function.

#include<stdio.h>

void printFibonacci(int n){

static int n1=0,n2=1,n3;

if(n>0){

n3 = n1 + n2;

n1 = n2;

n2 = n3;

printf("%d ",n3);

printFibonacci(n-1);

}

}

int main(){

int n;

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("Fibonacci Series: ");

printf("%d %d ",0,1);

printFibonacci(n-2);//n-2 because 2 numbers are already printed

return 0;

}

Out put:- Enter the number of elements:15 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

72. W.A.P to check whether a number is prime or not.

#include<stdio.h>

int main(){

int n,i,m=0,flag=0;

printf("Enter the number to check prime:");

scanf("%d",&n);

m=n/2;

for(i=2;i<=m;i++)

{

if(n%i==0)

{

printf("Number is not prime");

flag=1;

break;

}

}

if(flag==0)

printf("Number is prime");

return 0;

}

Out put:- Enter the number to check prime:56 Number is not prime Enter the number to check prime:23 Number is prime

73. W.A.P to check whether a number is palindrome or not.

#include<stdio.h>

int main()

{

int n,r,sum=0,temp;

printf("enter the number=");

scanf("%d",&n);

temp=n;

while(n>0)

{

r=n%10;

sum=(sum*10)+r;

n=n/10;

}

if(temp==sum)

printf("palindrome number ");

else

printf("not palindrome");

return 0;

}

Out put:- enter the number=151 palindrome number enter the number=5621 not palindrome number

74. W.A.P to find out factorial of a given number.

#include<stdio.h>

int main()

{

int i,fact=1,number;

printf("Enter a number: ");

scanf("%d",&number);

for(i=1;i<=number;i++){

fact=fact*i;

}

printf("Factorial of %d is: %d",number,fact);

return 0;

} Enter a number: 5 Factorial of 5 is: 120

75. W.A.P to find out factorial of a given number.(using recursion fuction).

#include<stdio.h>

long factorial(int n)

{

if (n == 0)

return 1;

else

return(n * factorial(n-1));

}

void main()

{

int number;

long fact;

printf("Enter a number: ");

scanf("%d", &number);

fact = factorial(number);

printf("Factorial of %d is %ld\n", number, fact);

return 0;

} Enter a number: 6 Factorial of 5 is: 720

76. W.A.P to check whether a number is Armstrong or not.

#include<stdio.h>

int main()

{

int n,r,sum=0,temp;

printf("enter the number=");

scanf("%d",&n);

temp=n;

while(n>0)

{

r=n%10;

sum=sum+(r*r*r);

n=n/10;

}

if(temp==sum)

printf("armstrong number ");

else

printf("not armstrong number");

return 0;

} enter the number=153 armstrong number enter the number=5 not armstrong number

77. W.A.P to count Vowels, Consonants, Digits and White spaces in a given string.

#include <stdio.h>

int main() { char line[150];

int vowels, consonant, digit, space; vowels = consonant = digit = space = 0; printf("Enter a line of string: "); fgets(line, sizeof(line), stdin); for (int i = 0; line[i] != '\0'; ++i) { if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U') {

++vowels; } else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) { ++consonant; } else if (line[i] >= '0' && line[i] <= '9') {

++digit; } else if (line[i] == ' ') { ++space; } } printf("Vowels: %d", vowels); printf("\nConsonants: %d", consonant);

printf("\nDigits: %d", digit); printf("\nWhite spaces: %d", space); return 0; }

Out put:-

Enter a line of string: I am a student of computer science Vowels : 12 Consonants: 16 Digits : 0 White space: 6

78. W.A.P to check a string is palindrome or not. #include <stdio.h> #include <string.h> int main() { char text[100]; int begin, middle, end, length = 0; gets(text); while ( text[length] != '\0' ) length++; end = length - 1; middle = length/2; for( begin = 0 ; begin < middle ; begin++ )

{ if ( text[begin] != text[end] ) { printf("Not a palindrome.\n"); break; } end--; } if( begin == middle ) printf("Palindrome.\n"); return 0; }

Output:- madam palindrome

78. W.A.P to find LCM of two numbers using recursion. // C program to find LCM of two numbers #include <stdio.h> // Recursive function to return gcd of a and b int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // Function to return LCM of two numbers int lcm(int a, int b) { return (a*b)/gcd(a, b); } // Driver program to test above function int main() { int a = 15, b = 20; printf("LCM of %d and %d is %d ", a, b, lcm(a, b)); return 0; } Out put:- LCM of 15 and 20 is 60

79. W.A.P to find GCD of two numbers using recursion. #include <stdio.h> int hcf(int n1, int n2); int main() { int n1, n2; printf("Enter two positive integers: ");

scanf("%d %d", &n1, &n2); printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));

return 0; } int hcf(int n1, int n2) { if (n2 != 0) return hcf(n2, n1 % n2); else return n1; } Out put:-

Enter two positive integers: 366

60 G.C.D of 366 and 60 is 6.

80. W.A.P to convert binary to decimal number. #include <math.h> #include <stdio.h> int convert(long long n); int main() { long long n; printf("Enter a binary number: "); scanf("%lld", &n);

printf("%lld in binary = %d in decimal", n, convert(n)); return 0; } int convert(long long n) { int dec = 0, i = 0, rem; while (n != 0) { rem = n % 10; n /= 10; dec += rem * pow(2, i); ++i; }

return dec; } Out put:-

Enter a binary number: 110110111 110110111 in binary = 439

81. W.A.P reverse of a sentence using recursion. #include <stdio.h> void reverseSentence();

int main() { printf("Enter a sentence: ");

reverseSentence(); return 0; } void reverseSentence() { char c; scanf("%c", &c); if (c != '\n') { reverseSentence(); printf("%c", c); } }

Enter a sentence: margorp emosewa awesome program

82. W.A.P Addition of two matrixes. #include <stdio.h> int main() { int r, c, a[100][100], b[100][100], sum[100][100], i, j; printf("Enter the number of rows (between 1 and 100): "); scanf("%d", &r); printf("Enter the number of columns (between 1 and 100): "); scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n"); for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { printf("Enter element a%d%d: ", i + 1, j + 1); scanf("%d", &a[i][j]); } printf("Enter elements of 2nd matrix:\n"); for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { printf("Enter element a%d%d: ", i + 1, j + 1);

scanf("%d", &b[i][j]); } // adding two matrices for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { sum[i][j] = a[i][j] + b[i][j]; } // printing the result printf("\nSum of two matrices: \n");

for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) {

printf("%d ", sum[i][j]); if (j == c - 1) { printf("\n\n"); } } return 0; }

Enter the number of rows (between 1 and 100): 2 Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix: Enter element a11: 2 Enter element a12: 3 Enter element a13: 4 Enter element a21: 5 Enter element a22: 2 Enter element a23: 3 Enter elements of 2nd matrix: Enter element a11: -4 Enter element a12: 5 Enter element a13: 3 Enter element a21: 5

Enter element a22: 6 Enter element a23: 3 Sum of two matrices: -2 8 7

10 8 6

83. W.A.P to find the largest element in an array. #include <stdio.h> int main() { int i, n;

float arr[100]; printf("Enter the number of elements (1 to 100): "); scanf("%d", &n); for (i = 0; i < n; ++i) { printf("Enter number%d: ", i + 1); scanf("%f", &arr[i]); } // storing the largest number to arr[0] for (i = 1; i < n; ++i) {

if (arr[0] < arr[i]) arr[0] = arr[i];

} printf("Largest element = %.2f", arr[0]); return 0; }

Enter the number of elements (1 to 100): 5 Enter number1: 34.5 Enter number2: 2.4 Enter number3: -35.5 Enter number4: 38.7

Enter number5: 24.5 Largest element = 38.70

84. W.A.P to calculate power using recursion. #include <stdio.h> int power(int n1, int n2); int main() { int base, a, result; printf("Enter base number: "); scanf("%d", &base); printf("Enter power number(positive integer): "); scanf("%d", &a);

result = power(base, a); printf("%d^%d = %d", base, a, result); return 0; } int power(int base, int a) { if (a != 0) return (base * power(base, a - 1)); else return 1; }

Enter base number: 3 Enter power number(positive integer): 4 3^4 = 81

85. W.A.P to store information of a student and display it. #include <stdio.h> struct student { char firstName[50]; int roll; float marks; } s[10];

int main() {

int i; printf("Enter information of students:\n"); // storing information for (i = 0; i < 5; ++i) { s[i].roll = i + 1; printf("\nFor roll number%d,\n", s[i].roll); printf("Enter first name: "); scanf("%s", s[i].firstName); printf("Enter marks: "); scanf("%f", &s[i].marks); }

printf("Displaying Information:\n\n"); // displaying information for (i = 0; i < 5; ++i) {

printf("\nRoll number: %d\n", i + 1); printf("First name: "); puts(s[i].firstName); printf("Marks: %.1f", s[i].marks); printf("\n"); } return 0; }

Enter information of students: For roll number1, Enter name: Tom Enter marks: 98 For roll number2, Enter name: Jerry Enter marks: 89

.

.

. Displaying Information: Roll number: 1 Name: Tom Marks: 98 . . .

86. Which structure is used to link the program and the operating system? Ans: The answer can be explained through the following points,

• The structure used to link the operating system to a program is file. • The file is defined in the header file “stdio.h”(standard input/output header file). • It contains the information about the file being used, its current size and its location in memory. • It contains a character pointer that points to the character that is being opened. • Opening a file establishes a link between the program and the operating system about which file is

to be accessed.

87. What are the limitations of scanf() and how can it be avoided? Ans: The Limitations of scanf() are as follows:

• scanf() cannot work with the string of characters. • It is not possible to enter a multiword string into a single variable using scanf(). • To avoid this the gets( ) function is used. • It gets a string from the keyboard and is terminated when enter key is pressed. • Here the spaces and tabs are acceptable as part of the input string.

88. Differentiate between the macros and the functions. Ans: The differences between macros and functions can be explained as follows:

• Macro call replaces the templates with the expansion in a literal way. • The Macro call makes the program run faster but also increases the program size. • Macro is simple and avoids errors related to the function calls. • In a function, call control is transferred to the function along with arguments. • It makes the functions small and compact. • Passing arguments and getting back the returned value takes time and makes the program run at

a slower rate.

89. What is syntax error? Syntax errors are associated with mistakes in the use of a programming language. It maybe a command that was misspelled or a command that must was entered in lowercase mode but was instead entered with an upper case character. A misplaced symbol, or lack of symbol, somewhere within a line of code can also lead to syntax error.

90. W.A.P for half pyramid.

*

* *

* * *

* * * *

* * * * *

#include <stdio.h> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) {

for (j = 1; j <= i; ++j) { printf("* ");

} printf("\n"); } return 0; }

91. W.A.P for half pyramid of numbers.

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

#include <stdio.h> int main() { int i, j, rows;

printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) { for (j = 1; j <= i; ++j) { printf("%d ", j); } printf("\n"); }

return 0; }

92. W.A.P for half pyramid of Alphabets.

A

B B

C C C

D D D D

E E E E E

#include <stdio.h> int main() {

int i, j; char input, alphabet = 'A'; printf("Enter an uppercase character you want to print in the last row: "); scanf("%c", &input); for (i = 1; i <= (input - 'A' + 1); ++i) { for (j = 1; j <= i; ++j) { printf("%c ", alphabet); } ++alphabet; printf("\n"); }

return 0; }

93. W.A.P for Inverted half pyramid of *.

* * * * *

* * * *

* * *

* *

*

#include <stdio.h> int main() { int i, j, rows; printf("Enter the number of rows: ");

scanf("%d", &rows); for (i = rows; i >= 1; --i) { for (j = 1; j <= i; ++j) { printf("* ");

} printf("\n"); } return 0; }

94. W.A.P for Inverted half pyramid of numbers.

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

#include <stdio.h> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; --i) { for (j = 1; j <= i; ++j) {

printf("%d ", j); } printf("\n"); } return 0; }

95. W.A.P for full pyramid of *.

*

* * *

* * * * *

* * * * * * *

* * * * * * * * *

#include <stdio.h> int main() { int i, space, rows, k = 0; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i, k = 0) { for (space = 1; space <= rows - i; ++space) { printf(" "); } while (k != 2 * i - 1) {

printf("* "); ++k; } printf("\n"); } return 0; }

96. W.A.P for full pyramid of numbers.

1

2 3 2

3 4 5 4 3

4 5 6 7 6 5 4

5 6 7 8 9 8 7 6 5

#include <stdio.h> int main() { int i, space, rows, k = 0, count = 0, count1 = 0; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) { for (space = 1; space <= rows - i; ++space) { printf(" ");

++count; } while (k != 2 * i - 1) { if (count <= rows - 1) { printf("%d ", i + k); ++count; } else { ++count1; printf("%d ", (i + k - 2 * count1)); } ++k; }

count1 = count = k = 0; printf("\n");

} return 0; }

97. W.A.P for Inverted full pyramid of *.

* * * * * * * * *

* * * * * * *

* * * * *

* * *

*

#include <stdio.h> int main() { int rows, i, j, space; printf("Enter the number of rows: ");

scanf("%d", &rows); for (i = rows; i >= 1; --i) { for (space = 0; space < rows - i; ++space) printf(" "); for (j = i; j <= 2 * i - 1; ++j) printf("* "); for (j = 0; j < i - 1; ++j)

printf("* "); printf("\n"); } return 0; }

98. W.A.P for Pascal’s Triangle.

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

#include <stdio.h> int main() { int rows, coef = 1, space, i, j; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 0; i < rows; i++) { for (space = 1; space <= rows - i; space++) printf(" "); for (j = 0; j <= i; j++) { if (j == 0 || i == 0)

coef = 1; else

coef = coef * (i - j + 1) / j; printf("%4d", coef); } printf("\n"); } return 0; }

99. W.A.P for Floyd’s Triangle.

1

2 3

4 5 6

7 8 9 10

#include <stdio.h> int main() {

int rows, i, j, number = 1; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; ++j) { printf("%d ", number); ++number; }

printf("\n"); } return 0; }

100. Who invented ‘C’? Ans:- Dennis Ritchie