Asian, Balkumari ,Gupteshwor ,HDC ,IMS ,Janapriya ,Morgan ,NCC ,SDC,SPA
Answer:
- What is source code? Why is compilation needed?
ANS. Computer program written in a high-level language which is converted into object code or machine code by a compiler is called source code.Compilation is needed because to translates human-readable source code into computer-executable machine code.
- What is the use of shift operators? Explain with examples?
ANS. There are two types of shift operators i.e left and right. A left shift is a logical shift (the bits that are shifted off the end are discarded, including the sign bit) and right-shift operator causes the bit pattern in shift-expression to be shifted to the right by the number of positions specified by additive-expression.
- Discuss different types of decision making statements in C with example.
ANS. Types of decision making are
- If -If the expression returns true, then the statement-inside will be executed, otherwise statement-inside is skipped and only the statement-outside is executed.
Example
1 2 3 4 5 6 7 8 9 |
<span style="color: #000000;">#include <stdio.h> void main( ){ int x, y; x = 15; y = 13; if (x > y ) { printf("x is greater than y"); } }</span> |
- if-else- If the expressionis true, the statement-block1 is executed, else statement-block1 is skipped and statement-block2 is executed.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span style="color: #000000;">#include <stdio.h> void main( ) { int x, y; x = 15; y = 18; if (x > y ) { printf("x is greater than y"); } else { printf("y is greater than x"); } } </span> |
Nested if else statements- if expression is false then statement-block3 will be executed, otherwise the execution continues and enters inside the first if to perform the check for the next if block, where if expression 1 is true the statement-block1 is executed otherwise statement-block2 is executed.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<span style="color: #000000;">include <stdio.h> void main( ) { int a, b, c; printf("Enter 3 numbers..."); scanf("%d%d%d",&a, &b, &c); if(a > b) { if(a > c) { printf("a is the greatest"); } else { printf("c is the greatest"); } } else { if(b > c) { printf("b is the greatest"); } else { printf("c is the greatest"); } } } </span> |
4.Differentiate between global and local variables with examples?
ANS. Global variables are declared outside any function, and they can be accessed (used) on any function in the program. Local variables are declared inside a function, and can be used only inside that function.
1 2 3 4 5 6 7 8 |
<span style="color: #000000;">Example of local variable and global variable //program to add any two integers int a=2,b=4; /*global variable*/ void main(){ int a=5,b=8,sum; /*local variable*/ sum=a+b; printf("\nSum of two integers %d and %d is %d",a,b,sum);} </span> |
- How does an array form an ordinary variable?
ANS. Array is different from ordinary variable because array holds multiple values,whereas an ordinary variable hold a single value. It is true when the elements of the array are treated as individual entities, and when the variables such as an int. It is not generally right to distinguish between a variable and an array.
6.What is the relationship between an array and a pointer? Explain with suitable example.
ANS. Array and pointers in a way store addresses. Variable name of array stores the starting address of the array. Pointers also holds the address of the particular data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span style="color: #000000;">1. #include <stdio.h> 2. int main() 3. { 4. int x[4]; 5. int i; 6. 7. for(i = 0; i < 4; ++i) 8. { 9. printf("&x[%d] = %p\n", i, &x[i]); 10. } 11. 12. printf("Address of array x: %p", x); 13. 14. return 0; 15. } </span> |
- What are the advantages of dynamic memory allocation over static memory allocation?
ANS. The advantages of dynamic memory allocation over static memory allocation are Preventing the wastage of memory. This is because when we use static memory allocation, a lot of memory is wasted because all the memory allocated cannot be utilised.
8.Define enum data types with example.
ANS. Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.
1 2 3 4 5 6 7 |
<span style="color: #000000;">#include <stdio.h> enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday }; int main(){ enum week today; today = wednesday; printf("Day %d",today+1); return 0;}</span> |
9.How is a structure initialized? Explain with a example.
ANS. When we declare a structure then memory won’t be allocated for the structure. i.e only writing below declaration statement will never allocate memory
1 2 3 4 5 6 7 |
<span style="color: #000000;">struct student { int mark1; int mark2; int mark3; }; </span> |
10.What is data file? Why is it necessary in programming?
ANS. A file is a place on the disk(not in memory) where a group of related data is stored is called data file.It is necessary in programming because the data file allow us to store information permanently and to access and alter that information whenever necessary.
GROUP B
- Write a program and flowchart to read three side of triangle then find it’s area?
1 2 3 4 5 6 7 8 9 10 11 |
<span style="color: #000000;">#include <stdio.h> #include <math.h> main() int s, a, b, c, area;{ printf("Enter the values of a, b and c \n"); scanf("%d %d %d", &a, &b, &c); /* compute s */ s = (a + b + c) / 2; area = sqrt(s * (s - a) * (s - b) * (s - c)); printf("Area of a triangle = %d \n", area); }</span> |
12.WAP to write sum of first 10 odd numbers?
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="color: #000000;">#include <stdio.h> main() { intsum = 0,i; for(i = 1; i<= 10; i++) { if(i%2!=0) { sum =sum+i; } printf("Sum of the integers = %d\n", sum); }</span> |
13.WAP to determine whether number is prime or not using function?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<span style="color: #000000;">include <stdio.h> int prime(int); main() { int n,p; printf("enter number:"); scanf("%d",&n); p=prime(n); if(p==1) { printf("prime"); } else { printf("not prime"); } } int prime(int x) { int i; for(i=2;i<=x;i++) { if(x%i==0) { return 0; } } if(i==x) return 1; }</span> |
14.WAP to read m and n from a user and and calculate value of m without library function?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span style="color: #000000;">#include<stdio.h> main() { int m,n,x=1,i; printf("enter"); scanf("%d%d",&m,&n); for(i=1;i<=n;i++) { x=m*x; } m=x; printf("%d is",m); }</span> |
15.WAP to read line of text and print it in reverse order?
1 2 3 4 5 6 7 8 9 10 |
<span style="color: #000000;">#include<stdio.h> #include<string.h> main() { int line[50]; printf("enter"); gets(line); strrev(line); printf("%s",line); }</span> |
GROUP-C
WAP to read a number in array and perform following statement using switch
- Print largest number of array
- Check whether the value of x present in array or not
Count the number is even or odd number in array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
<span style="color: #000000;">#include<stdio.h> #define n 5 main() { int a[n],choice,i,x,largest,odd=0,even=0,c=0; printf("enter choice"); scanf("%d",&choice); switch(choice) { case 1: printf("\n Enter elements of the array:"); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } largest = a[0]; for (i = 1; i < n; i++) { if (largest < a[i]) { largest = a[i]; } } printf("\n largest element present in the given array is : %d", largest); break; case 2: printf("Enter integer\n"); for (i = 0; i< n; i++) { scanf("%d", &a[i]); } printf("Enter a number to search\n"); scanf("%d", &x); for (i = 0; i< n; i++) { if (a[i] == x) { c=c+1; } } if(c>=1) { printf("is present in the array.\n"); } else{ printf("isnot present in the array.\n"); } break; case 3: printf("enter numbers"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { if(a[i]%2==0) { even=even+1; } else { odd=odd+1; } } printf("even is %d\n",even); printf("odd is %d\n",odd); break; default: printf("you dont understand"); } }</span> |
17.WAP that reads three numbers from keyboard as command line augument and display their sum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span style="color: #000000;">#include <stdio.h> int main(int argc, char*argv[]) { int a,b,c,sum; a= atoi(argv[1]); b= atoi(argv[2]); c= atoi(argv[3]); sum=a+b+c; printf("num of argument are %d",argc); printf("\n the name of program is %s",argv[0]); printf("%d",sum); getch(); } </span> |
18.WAP to create a structure called employee in which data member are name, age, and salary. Read values form keyboard and save it in data file called “employee.dat”
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span style="color: #000000;">#include <stdio.h> struct employee { char name[25]; int age; int salary; }e; main() { printf("enter name, age and salary"); scanf("%s%d%d",e.name,&e.age,&e.salary); printf("%s\t%d\t%d\n",e.name,e.age,e.salary); }</span> |
Answer:-
- what is operator?
- Operator are tokens which trigger some computation when applied to variable and other objects in an expression.
- What is variable?
A variable has a value that can change. It is a memory location that can hold a value of a certain data type.
- Define structure.
- Structure is a user defined data type available in c that allows to combine data items of different kinds. To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member.
- What is global variable.
- Global variable is a global scope, meaning that it is visible throughout the program, unless shadowed. They hold their values throughout the lifetime of your program an d they can be accessed inside any of the functions defined for the program.
- Basic difference between float and double variable.
Float of 32 bits(4 byte) in which 23 bits are used to store mantissa and 8 bits are used for storing exponent and 1 bit is used for sign. It can hold 1 5 to 17 decimal digits.
Double is of 64 bits(8 byte) in which 52 bits are used to store mantissa and 7 bits are used to store exponent and 1 bits is for sign. It can hold about 15 to 17 decimal digits.
- Define type conversion.
- The conversion of predefined data type into another data types is called type conversion.
- What is the different between puts and printf?
- The different between puts and printf are as follows:
- Puts is used to display only strings whereas printf is used to display integer, float, character, hexadecimal values as well as strings.
- What is the use of union in c?
- A union is a special data type available in c that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time.
- Define double pointer.
- A pointer which points to another pointer is known as double pointer.
- Explain modular programming?
- Modular programming is the process of subdividing a computer program into separate sub- program. A module is a separate software component.
Answer:-
2.write a function that takes 2 integers as arguments and display sum of those numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span style="color: #000000;">#include<stdio.h> Void sum(int a ,int b); Void main() { Int a,b; Printf(“enter two number”); Scanf(“%d%d”,&a,&b); Sum(a,b); } void sum(int a ,int b) { Int sum=0; Sum=a+b; Printf(“the sum of two numbers is %d”,sum); } </span> |
3.wap to add number at even position of integer array of size 50.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span style="color: #000000;">#include<stdio.h> void main() { Int a[50],sum=0,i; for(i=0;i<50;i++) { Printf(“enter the value for a[%d]”,i); Scanf(“%d”,&a[i]); } for(i=0;i<50;i++) { If (i%2==0) { Sum=sum +a[i]; } } Printf(“sum=%D,sum”); }</span> |
4.WAP to display line of text “BEST OF LUCK” 5times using a function.
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="color: #000000;">#include<stdio.h> Void disp() { For (i=0;I<5;i++) { Printf(“best of luck \n”); } void main() { Disp (); } </span> |
5.Wap to convert a lower case alphabet to upper case.
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="color: #000000;">#include<stdio.h> Void disp() { For (i=0;I<5;i++) { Printf(“best of luck \n”); } void main() { Disp (); } </span> |
6.Wap to copy content of one array to another array .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span style="color: #000000;">#include<stdio.h> void main() { Int a[5]={10,20,30,50,90}; Int b [5]; for(i=1;i<=4;i++) { printf("%d”;a[i]); b[i]=a[i]; } Printf(“after copy second array is given below \n”); for(j=0;j<=4;j++) { Print(“%d”,a[j]; } getch(); } } </span> |
8.Wap to print the following pattern using loop.
2
4
8
14
22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span style="color: #000000;">#include<stdio.h> #include<stdio.h> void main() { int j=2,i,k=2; for(i=1;i<=5;i++) { printf("%d\n",j); j=j+k; k=k+2; } getch(); } </span> |
Answer:-
- Define language processor.
- A language processor is a special type software that has the capacity of translating the source code into machine code. Types are:
- Compiler
- Assembler
- Interpreter
- Write steps for executing a C program.
- Write source codes
- Preprocessor
- Compile
- Link Edit
- Load
- Execute
- Why is else statement used?
- An else statement is an alternative statement that is used to execute if the result of a previous test condition evaluates to false. The program control automatically goes to the else statement.
- What is the syntax of for loop?
- Syntax:
for([initialization] ; [condition] ; [increment/decrement])
- Is it possible to put default: anywhere in the switch condition?
- Yes, it is possible to put default anywhere in switch condition.
- Write down the output of following program:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="color: #000000;">#include<stdio.h> Int main() { int marks; printf(“Enter Your Marks(<100):”); scanf(“%d”, marks); printf(“Your Entered Marks:”, marks); return 0; } Output: Enter Your Marks(<100): 20 Your Entered Marks:</span> |
- What is array?
- An array is a collection of elements of same data type placed in contiguous (consecutively) memory location and can be accessed individually using index to a unique given name.
- Show the differences between iteration and recursion.
Iteration | Recursion |
It process of executing a statement repeatedly, until some specific condition is met. | It is the technique of defining anything in terms of itself. |
Any recursive problem can be solved iteratively . | No all problem have recursive solution. |
- When will you use structure variable?
- A structure variable is a variable of a structure type. It is used when collection of different data type need to call in a single name.
10.Define pointer.
- A pointer is a variable that represents the location (rather than value) of a data item.
Q.NO. 12.Write a menu driven program which performs following tasks:
i. Create a structure named student with member variables: roll no, grade, gender, age.
ii. Define ten structure variable type student and using loop take input for all ten structure variables.
iii. Display names of all male students.
iv. Exit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<span style="color: #000000;">#include<stdio.h> #define N 10 struct student { int roll; char grade; char gender; char name[20]; int age; }; int main() { struct student s[N]; char option; char choice= ‘y’; int i; printf(“Menu Driven Program”); printf(“\n Enter 1 for inputting student information”); printf(“\n Enter 2 to display all male students”); printf(“\n Enter 3 for exit”); while(choice != ‘n’) { printf(“\n Enter your option:\t”); scanf(“%c”, &option); { case ‘1’ : printf(“\n Enter information about %d student”, N); for (i=0 ; i<N ; i++) { printf(“\n Info. About student %d”, i+1); printf(“\n Roll: \t”); scanf(“%d”, &s[i].roll); printf(“\n Grade:\t”); scanf(“%c”, &s[i].grade); printf(“\n Gender(M/F):\t”); scanf(“%c”, &s[i].gender); printf(“\n Age:\t”); scanf(“%d”, &s[i].age); printf(“\n Name:\t”); scanf(“%s”, &s[i].name); } break; case ‘2’: printf(“\n List of Male Students ”); for (i=0 ; i<N ; i++) { if (s[i].gender= ‘M’) printf(“\n %s \n”, s[i].name); } break; case ‘3’: exit (0); default: printf(“Invalid Option!”); } printf(“\nDo you want to continue (y/n)”); scanf(“%c”, &choice); } return 0; } </span> |
13:WAP to display the leap year from 1000 to 3000.
1 2 3 4 5 6 7 8 9 10 11 |
<span style="color: #000000;">#include<stdio.h> int main() { int year; printf(“Leap years starting from 1000 to 3000. ”); for(year=1000 ; year<=3000 ; year++) if (year%400 == 0 || (year%4 == 0 && year%100 == 0) printf(“%d”, year); return 0; } </span> |
Q.No. 14: WAP to display following pattern:
*
**
***
****
*****
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="color: #000000;">#include<stdio.h> int main() { int i, j; for (i=1 ; i<=5 ; i++) { for (j=1 ; j<=i ; j++) { printf(“ * ”, i); } printf(“\n”); } return 0; } </span> |
Q.No.15:WAP to display following
1/2, 2/3, 3/4, 4/5,………………, n-1/n.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span style="color: #000000;">#include<stdio.h> int main() { int n, i; printf(“Enter the number: \n”); scanf(“%d”, &n); for (i=1 ; i<n ; i++) { printf(“\n%d/%d, ”, i, i+1); } return 0; } </span> |
Q.No.16:WAP to find transpose of a matrix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<span style="color: #000000;">#include<stdio.h> int main() { int a[2][2], i, j; for (i=0 ; i<2 ; i++) { for (j=0 ; j<2 ; j++) { printf("Enter the [%d][%d] element: ", i, j); scanf("%d", &a[i][j]); } } printf("The matrix to be transposed is:\n"); for (i=0 ; i<2 ; i++) { for (j=0 ; j<2 ; j++) { printf("%d\t", a[i][j]); } printf("\n"); } printf("The transpose of a matrix is:\n"); for (i=0 ; i<2 ; i++) { for (j=0 ; j<2 ; j++) { printf("%d\t", a[j][i]); } printf("\n"); } return 0; } </span> |
IMS
Define comments with its types.
Ans. Comments are the set of statements that are used in program for the programmer convinence and effectiveness i.e. comment doesn’t represent itself while executing a program.
There are two types of comments in c programming. They are:-
- Single line comment //
- Multiline comment /*
Differentiate between ‘=’ and ‘= =’.
Ans. ‘=’ is assignment operator i.e. it assigns value to the respected variable i.e. x=10;
‘= =’ is relational operator. It checks whether the variable value is equal to the provided value or not i.e. x = = 10;
What do you mean by function?
Ans. Function are the set of statements which are responsible to perform the specified task in the program. There are two types of function i.e. user defined function (structure) and built-in function (printf, scanf, gets, getch etc.).
Differentiate between symbolic and literal constants.
Ans. Symbolic constant, comes into a picture with preprocessor compilers.
Whereas,
A literal constant is directly typed value into a program i.e. int x = 10;
What is linker?
Ans. A linker is a computer program that takes one or more object files generated by a compiler and combines them into one executable program.
What is the use of #include preprocessor directives?
Ans. #include is responsible to insert a particular header from another file.
What do you mean by syntactic and semantic errors?
Ans. Syntactic errors are that errors occurred when we violate the rules of writing syntax.Sematic errors are those errors which occurs when the statements written in program are not meaningful to the compiler.
Why semicolon is used in c program?
Ans. Semicolon is used in C program to terminate the set of statement and forward it to next line for new statement.
What is the use of continue statement?
Ans. The continue statement works somewhat like break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between.
What is the use of size of ( ) operator?
Ans. Size of ( ) operator is used to compute the size of operand, i.e. it returns the size of a variable.
Group ”B”
2.a) Write a program to find whether entered number is prime or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span style="color: #000000;">#include<stdio.h> void main() { int n,i,flag=0; printf("Enter a number:\n"); scanf("%d",&n); for(i=2; i<=n/2; i++) { if(n%i==0) { flag=1; break; } } if(n==1) printf("1 is neither prime nor a composite number."); else { if(flag==0) printf("%d is a prime number.",n); else printf("%d is not a prime number.",n); } } </span> |
b) Print the following pattern:
9
19 29
39 49 59
69 79 89 99
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span style="color: #000000;"> #include<stdio.h> void main() { int i,j,k=9; for(i=1; i<=4; i++) { for(j=1; j<=i; j++) { printf("%d\t",k); k=k+10; } printf("\n"); } } }</span> |
c) WAP to print fabonacci series upto nth term entered by the user.
-
12345678910111213141516<span style="color: #000000;">#include<stdio.h>void main(){int n,i,a=0,b=1,nxtTerm;printf("Enter a numbers of fibonacci series you want:");scanf("%d",&n);printf("The %d fibonacci series are\t",n);for(i=1; i<=n; i++){printf("%d\t",a);nxtTerm= a+b;a= b;b= nxtTerm;}}</span>
WAP that will read two numbers and display the following menu and carry out the required actions.
MENU:
- Addition
- Subtraction
- Multiplication
- Division
- Square
- Cube
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<span style="color: #000000;">#include<stdio.h> int main() { char choice; int a,b; again: printf("\nChoose the given options below:"); printf("\n[e] for addition.\n[f] for subtraction.\n[g] for multiplication."); printf("\n[h] for division.\n[i] for square.\n[j] for cube.\n"); printf("enter choice:"); scanf("%s",&choice); switch (choice) { case 'e' : printf("Enter two numbers for operation:\n"); scanf("%d %d",&a,&b); printf("The addition of %d and %d is %d.",a,b,(a+b)); break; case 'f' : printf("Enter two numbers for operation:\n"); scanf("%d %d",&a,&b); printf("The subtraction of %d and %d is %d.",a,b,(a-b)); break; case 'g' : printf("Enter two numbers for operation:\n"); scanf("%d %d",&a,&b); printf("The multiplication of %d and %d is %d.",a,b,(a*b)); break; case 'h' : printf("Enter two numbers for operation:\n"); scanf("%d %d",&a,&b); printf("The division of %d and %d is %d.",a,b,(a/b)); break; case 'i' : printf("Enter two numbers for operation:\n"); scanf("%d %d",&a,&b); printf("The square of %d is %d and %d is %d.",a,(a*a),b,(b*b)); break; case 'j' : printf("Enter two numbers for operation:\n"); scanf("%d %d",&a,&b); printf("The cube of %d is %d and %d is %d.",a,(a*a*a),b,(b*b*b)); break; default: printf("\nInvalid option selected.Please select the provided option again:\n"); goto again; break; } return 0; } default: printf("\nInvalid option selected.Please select the provided option again:\n"); goto again; break; } return 0; } </span> |
I. Write the relationship between array and pointer.
Ans: Arrays and pointers are synonymous in terms of how they use to access memory. But, the important difference between them is that, a pointer variable can take different addresses as value whereas, in case of array it is fixed. In C , name of the array always points to the first element of an array.
II. Make a distinction between if statement and switch statement.
Ans: In if statement we use if and in switch statement we use switch as main keyword variable. To note a decision from number of choice we use switch statement and if statement tells the compiler that what to follow in a decision control structure.
III. Can a function return more than one value?
Ans: No, we cant return more than one value at a time using function because calling function holds only one return value from called function at a time.
IV. Define variable.
Ans: An entity that may change is variable.
V. Why dynamic memory allocation concept is used in programs?
Ans: Because it allocates free memory at run time. This reserves the memory required by the program.
VI. How union helps to save memory?
Ans: By it accessed only one member at any given time. Since, same memory is shared by all members one variable can reside into memory at a time.
VII. What is structure variable?
Ans: A struct in c is a composite data type declaration that defines a physically grouped list of variables under one name in a block of memory, allowing a different variables to accessed via a single pointer or by struct decleared name which returns the same address.
VIII. What is the use of “\t” in c program?
Ans: \t helps to print the value in horizontal or vertical tab.
IX. What are the different types of operator used in c programming?
- Arthematic operator
- Assignment operator
- Increment and decrement operator
- Relational operator
- Conditional operator
X. Which character terminates a string?
Ans: \0 (null) terminates a string.
3. WAP to do the following over a list of numbers
a) Create a structure named student with a member variables : roll, name, grade, gender, age.
b) Define ten structure variables of type student and using loop take input for all ten structure variables.
c) Display names of all male students.
Ans:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<span style="color: #000000;">#include<stdio.h> #include<conio.h> Main () { struct student { char name [10]; int roll; int age; char ch; int grade; }s[2]; int i,j; printf("Enter the information of the students:\t"); for (i=0;i<2;i++) { printf("\n Name:\t"); scanf("%s",s[i].name); printf("\n Grade:\t"); scanf("%d",&s[i].grade); printf("\n Roll no:\t"); scanf("%d",&s[i].roll); printf("\n Age:\t"); scanf("%d",&s[i].age); printf("\n Gender:\t"); s[i].ch= getche(); printf("\n"); } for (i=0;i<2;i++) { if (s[i].ch=='m'||'M') { printf("\n Name\tGrade\tRoll\tAge\tGender\n"); printf("%s\t%d\t%d\t%d\t%c",s[i].name,s[i].grade,s[i].roll,s[i].age,s[i].ch); } } } </span> |
6. WAP to read any two integer number n1 and n2 and develop a function that takes these two numbers as argument and print all the even integer numbers from n1 to n2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span style="color: #000000;">#include<stdio.h> #include<conio.h> void even(int n1, int n2); void main() { int n1,n2; printf("enter two numbers"); scanf("%d%d",&n1,&n2); even(n1,n2); getch(); } void even(int n1,int n2) { int i; printf("the even numbers are"); for(i=n1;i<n2;i++) { if(i%2==0) printf("%d",i); else printf("\t"); } }</span> |
7. WAP to copy string. Also write a program to concatenate two string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<span style="color: #000000;">// to copy a string #include<stdio.h> #include<string.h> int main() { char source [] = "c program"; char destination [50]; strcpy (destination,source); printf("source string: %s\n",source); printf("destination string : %s\n",destination); return 0; } // to concatenate a string #include<stdio.h> #include<string.h> int main() { char a[100],b[100]; printf("enter the first string"); gets (a); printf(" enter the second string"); gets (b); strcat(a,b); printf("string contained on concatenation is %s",a); return 0; } </span> |
- Write the step while executing program?
Ans: There are 3 step to executing program they are:
- writing a source code
- compiling and linking the code
- executing of the program.
- What is constant?
Ans: A constant is a quantity that doesn’t change during the execution of the program constant are divided into two types they are: Character onstant and Numeric
- What is function of size of operator?
Ans:The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.
- What is Escap sequence ? list out any three.
Ans: Escap sequence is a non-printing character used in C. it is the character combinations consisting of a backslash (\) followed by a letter or a combination of digits. They are : /a , /t ,/n etc.
- What is the use of break statement?
Ans: also used is switch statement which causes a transfer of control out of the entire switch statement , to the first statement following the switch statement.
- What is array? How it can be declared and initialized.
Ans: An array is a group of related data items that shares a common name it is a data structure that store number of data items as a single entity . it can be declared as int a[3], b[4]
- What is function?
Ans: A function is a block of statement that perform a particular specific task .
- What is argument? where it I used?
Ans: It is the means for communication between calling and called function It is used in a function are generally local variable for the function.
- What is pointer?
Ans: It is special type of variable that contain a memory address instead of values in pointer.
- What is structure?
Ans: structure is a collection of variables under a single name . the variable may be of different type and each has s name which is used to select it form the structure.
Short question
Program to print “welcome” 3 times.
1 2 3 4 5 6 7 8 9 10 |
<span style="color: #000000;">#include<stdio.h> Int main () { Int i:; For (i=1;i<=3;i++) { Printf(“\”welcome”\”); } Return 0; }</span> |
Wap to check given program prime or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<span style="color: #000000;">#include <stdio.h> int main() { int i, num, p = 0; printf("Please enter a number: \n"); scanf("%d", &num); for(i=1; i<=num; i++) { if(num%i==0) { p++; } } if(p==2) { printf("Entered number is %d "\ "and it is a prime number.",num); } else { printf("Entered number is %d "\ "and it is not a prime number.",num); } }</span> |
Write a program to copy of contents string to another.
1 2 3 4 5 6 7 8 9 10 11 |
<span style="color: #000000;">#include<stdio.h> Int main() { Char str1[50],str2[50]; Printf(“enter the string”); Gets(str1) Strcpy(str2,str1); Printf(“After the copiyng the string is ”,str2); Return 0; } </span> |
Write a program to print array of string.
1 2 3 4 5 6 7 8 9 |
<span style="color: #000000;">#Include<stdio.h> Int main() { Int I; Char namr[5][10]={“suman”,”manish”,”kajol”,”rabina”,”himal”}; For(i=0;i<5;i++) Printf(“\n %s”,name[i]); } </span> |
Answer:-
- How to open and close a Data file? Explain with syntax.
- The library function fopen is used to open a data file. It’s syntax is :
ptr_var=fopen (file_name,file_mode)
Where, file_name and file_mode are strings that represent the name of the data file and the manner in which the data file will be utilized.
The file is closed using library function fclose as: fclose(ptr_var).
- How union is different from structure?
- All members within union share the same storage area of computer memory. It takes less memory than structure. The memory required to store a union is same as member that occupies largest memory. Only one member union can be accessed at any given time. Union can be declared as:
1 2 3 4 5 6 |
<span style="color: #000000;">Union student { char n [20]; int roll; float marks; }st;</span> |
- Declare and initialization the pointer variable with example.
- Pointer should be declared as
1 2 3 4 |
<span style="color: #000000;">Syntax: data_type*pointer_name For e.g. int*p,num; p=#</span> |
Here, p contains address of num or points to num. This is called pointer initialization.
- Explain the different types of function calls?
- Call by Value
- Call by Reference
What is recursion and recursive function?
Recursion is method to define something in terms of itself. Recursion is one of the most programing tools. It is a technique for defining a problem in terms of one or more smaller versions of the same problem.
A recursive function is one that calls itself directly or indirectly to solve a smaller version of its task until a final which does not require a self-call.
- Write down the basic characteristics of Array?
- All the elements o an array share the same name and they are distinguished from one another with the help of an element or index number.
- Memory for array elements is allocated at array declaration time rather than program run time.
- Once the memory is allocated, its size cannot be grown or shrunk at run time.
- We have to guess expected size of memory in advance.
What do you mean by control structure? Explain.
The program statements are executed in same order in which they appear in the source program but if we want to alter the flow of normal execution of a program, control statements are used. Programming construct that contains control statements to control the flow of execution in a program is called control structure.
Explain the repetitive statements (loops)?
Repetition is the process of executing a group of statements more than one time as long as some condition remains true. Loops are used when we want to execute a part of program or block of statement several times. So a loop may be defined as a block of statements which are repeatedly executed foe a certain number of times or until a particular condition is satisfied. Its types are:
- The for loop
- The while loop
- The do while loop
- How conditional operator works?
It is also known as ternary operator. A ternary operator pair”?:” is available in C to construct conditional expressions of the form exp1?exp2:exp3; where exp1,exp2 and exp3 are expressions.
First evaluate exp1, if exp1 is true then execute exp2. If exp1 is false then execute exp3. Note that only one of the expressions (either exp2 or exp3) is executed.
- What is escape sequence?
- An escape sequence is a non-printing character used in C. It is a character combination consisting of a backlash (\) followed by a letter or a digit. For example:
- \n = Move cursor to the next or New line o the screen
- \t = Horizontal Tab
- \v = Vertical Tab
- An escape sequence is a non-printing character used in C. It is a character combination consisting of a backlash (\) followed by a letter or a digit. For example:
Q.No.2, Solution.// WAP demonstrating the use of nested if for finding largest among 3 numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span style="color: #000000;">#include <stdio.h> int main() { int a,b,c,largest; printf("Enter three integer numbers: "); scanf("%d %d %d",&a,&b,&c); if(a>b) { if(a>c) printf("%d is the largest number!”, a); else printf("%d is the largest number!",c); } else { if(b>c) printf("%d is the largest number!",b); else printf("%d is the largest number!",c); } return 0; } </span> |
Q.No.3, Solution// WAP to generate Fibonacci series up to n terms using recursive function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<span style="color: #000000;">#include <stdio.h> int fibo(int n) { if(n==0) return 0; else if(n==1) return 1; else return fibo(n-1)+ fibo(n-2); } int main() { int i,n; printf("Enter the Number of terms: "); scanf("%d",&n); for(i=0;i<=n;i++) printf("%d\t",fibo(i)); return 0; } </span> |
Q.No.5, Solution//WAP to find the sum of square of the elements of the diagonal of the square matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<span style="color: #000000;">#include <stdio.h> #include <math.h> #define MAX 100 int main() { int a[MAX][MAX],m,n,i,j,sum=0; printf("Enter the size of the matrix mxn: "); scanf("%d %d",&m,&n); if(m!=n) printf("The given matrix is not square matrix."); else printf("Enter the elements of the matrix: "); for(i=0;i<m;i++) { for(j=0;j<m;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<m;j++) { if(i==j) sum=sum+pow(a[i][j],2); } } printf("The sum is %d",sum); } </span> |
Q.No.6, Solution /*WAP to find X of the following series for the given values of a and N
X=a-a2/2+a3/3-a4/4…………………. up to N */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span style="color: #000000;">#include <stdio.h> #include <math.h> int main() { double X=0; int a,N,i; printf("Enter the value of a:"); scanf("%d",&a); printf("Enter the number of terms:"); scanf("%d",&N); for(i=0;i<=N;i++) { X=X+(((pow(a,i))/i)*(pow(-1,i+1))); } printf("The value of X is %.2f",X); return 0; } </span> |
Q.No.7, Solution
/* WAP to print the following output using for loop
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5 */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="color: #000000;">#include <stdio.h> int main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d\t",i); } printf("\n"); } return 0; } </span> |
Q.No.8, Solution //WAP to demonstrate the use of pointer to structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span style="color: #000000;">#include <stdio.h> void display(struct student *st); struct student { int roll; char name[20]; }; int main() { struct student s; printf("Enter the Roll No. of the Student: "); scanf("%d",&s.roll); printf("Enter the Name of the Student: "); gets(s.name); display(&s); return 0; } void display(struct student *st) { printf("Roll No.\t\tName"); printf("%d\t\t%s",st->roll,st->name); } </span> |
Answer:-
- What is structure programming?
- Structure programming is the specific problem solving method to solve the program by dividing the programs in small blocks known as the modular programming
- What is format for printf() and scanf() function?
- printf(“format string”,argument_list);
- scanf(“format string”,argument_list);
- What is the purpose of double data type?
- Double data type id used to store decimal numbers (numbers with floating point value) with double precision.
- What is the problem in printf(/n);
- printf(\n);
- What is array and write its advantages?
- Array Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data.
- its advantages are:-
Arrays represent multiple data items of the same type using a single name.
In arrays, the elements can be accessed randomly by using the index number.
Arrays allocate memory in contiguous memory locations for all its elements.
- List out five string handling functions?
- strcat – concatenate two strings.
- strchr – string scanning operation.
- strcmp – compare two strings.
- strcpy – copy a string.
- strlen – get string length.
- Define type conversion?
- Type conversion or typecasting refers to changing an entity of one datatype into another. There are two types of conversion: implicit and explicit.
- What is operator precedence?
- operator precedence is the hierarchy of the operators in which they are used In C,precedence of arithmetic operators( *, %, /, +, -)
- Differentiate structure and union?
- What are the modes of file handling?
r | Open for reading. | If the file does not exist, fopen()returns NULL. |
rb | Open for reading in binary mode. | If the file does not exist, fopen()returns NULL. |
w | Open for writing. | If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
wb | Open for writing in binary mode. | If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
- Write difference between gets and scanf.
- gets can take input even after space whereas scanf cannot.
- Write down the advantage of “a” mode over “w” mode in file handling.
- “a” mode can write in a file without erasing previous contents whereas “w” mode erases previous content and write in a file from beginning.
- Mention the use of “void” keyword?
- The use of void keyword is:
- to declare void pointer
- to declare a parameter less function
- Compare array and structure.
- Array is the collection of homogenous data whereas structure is the collection of heterogeneous data.
- Why p++ executes faster than p=p+1?
- p++ executes faster than p=p+1 because the value of p++ is replaced in same memory location whereas the value of p=p+1 is stored in new memory location.
- What is explicit type casting?
- Explicit type casting means data type doesn’t changes automatically, we have to define data type.
- What is the purpose of using strcmpi function?
- The purpose of using strcmpi function is t compare two string without any case.
14 program to input days and display in th format of year-month-days.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> #include<conio.h> void main() { long int days,mth,yrs,d; printf("enter days:"); scanf("%ld",&d); yrs=d/365; mth=(d-(yrs*365))/30; days=d-((yrs*365)+(mth*30)); printf("years=%ld,months=%ld,days=%ld",yrs,mth,days); getch(); } |
15.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> #include<onio.h> void main() { int i,j,k,sp=1; for(i=5;i>=1;i--) { for(k=1;k<sp;k++) { printf(" "); } sp++; for(j=i;j>=1;j--) { printf("%d",j); } printf("\n"); } getch(); } |
- Sentence and count the number of alphabets ,numbers of space ,number of digits and number of words.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char sec[100]; int c=0,i,d=0,sp=0,w=0; printf("enter a sentence:"); gets(sec); for(i=0;sec[i]!='\0';i++) { if(sec[i]>='a'&& sec[i]<='z') c++; else if(sec[i]>='0'&& sec[i]<='9') d++; else if(sec[i]==32) sp++; else w++; } printf("\n alphabets=%d",c); printf("\n numbers=%d",d); printf("\n space=%d",sp); printf("\n words=%d",w); getch(); } |