+ All Categories
Home > Documents > Printf(“%d %d”,I,j); - Centurion University Answer- BTCS1206 Basic... · These operators are...

Printf(“%d %d”,I,j); - Centurion University Answer- BTCS1206 Basic... · These operators are...

Date post: 21-Mar-2018
Category:
Upload: dangbao
View: 221 times
Download: 3 times
Share this document with a friend
18
Model Answer for Basic Programming in C Part A 1. a ANS. An algorithm is a procedure or formula for solving a problem. OR It is a step by step solution of a well-defined problem. // Algorithm to the biggest no. Between 2 nos. Step 1: Start Step 2: Read two nos. A and B. Step 3: compare Aand B If (A>B ) then Display A is bigger than B if (B>A ) then Display B is bigger than A Step 4: stop 1.b .) Identifier is a user define word. Which refers the name of variable, functions,array, pointer etc. Ex :- Total. Mark, Add();, *ptr; These are the defined by programmer by its requirement. 1.c Assignment Operator : i) = is an assignment operator. Which is used to assign the value of right side operand to left side operand. ii) Left side operand must be a single variable only. Ex. A=10, b=20 A=B; it means value of B is assigned to A i.e now A =20 Relational Operator: >=,<=, ==,! = etc. are relational operator, which are used to compare value of two operand i.e Left side and right side. Ii) Left side operand may be asingle variable or an expression. Ex. A=10, b=20 A==B means value of A and B will be compared, it will check whether they are equal or not. Ans will be 0 because they are not equal. 1.d ) The output of following code is Main() { Int I=5,j; J=++I + ++I - ++ I + --I + I++ - I++; Printf(“%d %d”,I,j); } Output is 9 14 1.e) The difference between while and do while While While is an entry control loop. If the condition is false it will not be executed at least once also. Syntax is Initialization; While(condition)
Transcript

Model Answer for Basic Programming in C Part A 1. a ANS. An algorithm is a procedure or formula for solving a problem. OR It is a step by step solution of a well-defined problem. // Algorithm to the biggest no. Between 2 nos. Step 1: Start Step 2: Read two nos. A and B. Step 3: compare Aand B If (A>B ) then Display A is bigger than B if (B>A ) then Display B is bigger than A Step 4: stop 1.b .) Identifier is a user define word. Which refers the name of variable, functions,array, pointer etc. Ex :- Total. Mark, Add();, *ptr; These are the defined by programmer by its requirement. 1.c Assignment Operator : i) = is an assignment operator. Which is used to assign the value of right side operand to left side operand. ii) Left side operand must be a single variable only. Ex. A=10, b=20 A=B; it means value of B is assigned to A i.e now A =20 Relational Operator: >=,<=, ==,! = etc. are relational operator, which are used to compare value of two operand i.e Left side and right side. Ii) Left side operand may be asingle variable or an expression. Ex. A=10, b=20 A==B means value of A and B will be compared, it will check whether they are equal or not. Ans will be 0 because they are not equal. 1.d ) The output of following code is Main() { Int I=5,j; J=++I + ++I - ++ I + --I + I++ - I++; Printf(“%d %d”,I,j); } Output is 9 14 1.e) The difference between while and do while While While is an entry control loop. If the condition is false it will not be executed at least once also. Syntax is Initialization; While(condition)

{ Statements; } do while Do while is an exit control loop If the condition is false also it will be executed at least once. Syntax is Initialization; do { Statements; } while(condition); 1.f) The output of following code is Main() { Int A=5; If(A>=4); Printf(“A is more than 4”); } Output is A is more than 4 1.g) Syntax of declaration of a two dimensional array is Data type Array_name[row size][column size]; Data type specifies the type of values will be stored in array. Array can be initialized by two ways.

i. At compile time. ii. At Run time

Syntax of initializing 2D array at Compile time is Data type Array_name[row size][column size]={ elements/ values} ; i.e. Int A[3][3]={1,2,3,4,5,6,7,8,9}; 1st 3 values in 1st row, 2nd 3 nos. in 2nd row and so on will be placed. At run time It will require an input function to initialize at run time. 1.h) What is the difference between static and auto variables?

Static variable Auto variable

We have to specify the storage class to make a variable static. syntax Static int a;

It is the default storage class. syntax auto int a;

If it is not assigned any value then it will give 0 as output.

If it is not assigned any value then it will give garbage value as output.

It is visible to the block in which it is declared and also in the function where it will passed.

It is visible to the block in which the variable is declared.

It retains its value between different function calls. It holds its last value.

It retains its value till the control remains in the block in which the variable is declared.

Static variable should be compile by compiler first.

Auto variable will compile by the compiler after the static variable.

1.i)What is Keyword ? Mention any two keywords used by two c compiler. Ans:- 1.Keywords are those words whose meaning is already defined by Compiler 2. Cannot be used as Variable Name 3. There are 32 Keywords in C 4. C Keywords are also called as reservedwords. Auto, break, etc. keywords used by two c compiler. 1. J) Differentiate between break and continue Break; -It is used to break the flow of loopstatement or switch statement either conditionally or unconditionally. -break statement is used in loop statement or in switch statement. Ex. For (i=0;i<=10;i++) { If(i==3) {break; } Else Printf(“%d”,i); } Output is 0 1 2 Continue -Continue statement is used skip some of statements in loop and move to next iteration. -continue statement is used in loop statement only. Ex. For(i=0;I<=10;i++) { If(i%3==0) Continue; Else Printf(“%d”,i); } Output is 0 1 2 4 5 7 8 10

PART 2 2. a) Structure of C program

Documantation

Linking section

Definition

Global declaration

Main() { Body of main function }

Sub programs/ functions.

Documentation Here need to explain what to be done and how to be done . In other word it is simly the question of th e program.

- It should be written with comment line. By using single comment line or multiple comment line i.e // or /* */ respectably

_ it is optional. Linking section In this section, your program can be linked with some other part/ or program. -it can be achieved by using preprocessor directive. i.e. #include. Example. #include<stdio.h> Defination section: Here define symbolic constant or macro( small functions) can be defined. By using #define Example. #define pi 3.141 #define max 1000 #define inti(int i){ if(i>0)printf(“it is a +ve no.”); Global declaration Cprogram is collection of functions. There are two types of variables. 1.Local 2.Global The local variables are accessible in those functions, where they are defined /declared. However sometimes data should be accessed in some of functions. So it is required data Must be declared outside of all function i.e should be declared as global.

Main() It is the main Part of c program. The execution of any C program starts from main function. Without main () program will not be executed. It may be called starting point of C program. Whatever you want to execute that definition must be kept in main() only. SUB Programs: A C program can be divide into several part. Each sub part of program is called sub program or function. Every function must be defined outside of main().and to execute that must be called in main() or in some other function. 2.B> Write an algorithm and flow chart for generating the prime nos. between 1 to 100. Algorithm Step-1: start Step -2: initiate i=1 Step-3 repeat step4 to step 7 while I !=100 Step -4 set c=0,j=1; Step-5 repeat while j != I Step 6 if(I % j==0) then c=c+1; end of if j=j+1; End of step 5 loop Step 7 if (c==2)then Print I //I is prime no End of if I =I +1; End of step 3 loop Step 8: stop.

Flow Chart:

Start

i =1

if i ≤100

C =0

if j ≤i

if i ÷ j ==0

C++

J++

If C++

Print i

i++

Stop

3. Draw a flow chart to describe the execution method of a C program Ans:

System Ready

Source Code

Compile Compiler

If Syntax Error

Object Code

Execute Source code

Input Data

Logical error ?

Out Put

. exe file

Wrong data

editor edit

Yes

Yes

No

No

Start

Stop

3.B> What do you mean by operators in C? Describe all the operators in C Ans:- Operator is nothing but a symbol, which performs some specific operation/task. C programming language is very rich with its operators. There are several operators in C. These e are mentioned bellow.

1. Arithmetic operators. 2. Assignment operators. 3. Logical operators. 4. Relational operators 5. Conditional operators 6. Increment / decrement operators 7. Bitwise operators 8. Some Special operators. 1. Arithmetic operator

These operators are used to perform different arithmetic operation like addition, subtraction, multiplication, division etc. These operators are shown bellow + , -, *, / , % etc. A=B+C; // Addition A=B-C; //subtraction A=B*c;// multiplication A=B/C // division

2. Assignment operator This operator is used to assign the value of operand to another. = is assignment operator. The value of right side operand will be assigned to left hand side operand. The left side operand must be a single variable. A=19, B=32 A=B i.e. value of B will be assigned to A. Now Ais 32.

3. Relational operators: These operators used to compare / find relationship between two operand. The following are relational operators. >= , <= ,== ,!= ,< ,> A>=B, A==b etc. If these condition/ relationship is true it will produce 1 otherwise 0.

4. Logical operators These operators are used to combine two or more relational expression. These followings are logical operators && , ||and !

These operators also give 0 or 1 as output.

Operator Name of the Operator

&& And Operator

|| Or Operator

! Not Operator

5. Conditional operator This operator is combination of two ternary operators i.e.? And: This works as if else statement. Syntax exp1? exp2:exp3; It means if exp1 is true then theexp2 will be executed otherwise exp3. Ex. A>B? printf(“A is big ”) : printf(“B is big”);

6. Increment/decrement operator This is a unary operator. it deals with only one operand. ++ / -- are inc/dec operator. Let a++ means a=a+1 and a—means a=a-1. A++ value of A will be incremented by one A-- value of a will be decremented by 1.

7. Bit wise operator These operators are deals with bit pattern of operands. & bit wise AND operator. | OR >>Right sift operator <<Left sift operator

^ Exclusive OR operator. 8. Some Special operator

Except all these above operators some special operators are there in C. These are . Dot operator to access structure variable. Arrow operator to access structure variable(pointer kind)

& address of operandoperator Address of a variable *Indirection operator value at pointer Sizeof () operator to measure memory occupied by a variable. Etc. 4. a> write a program to print this following structure. A A B A B C A B C D A B C D E Ans. #include <stdio.h> Void main() { Int I,j,K; For (i=1;i<=5;i++) { K=65; For(j=1; j<=I ; j++)

{ Ptintf(“%c”,k++); } Printf(“\n”);

} } 4.b>what do you mean by recursion? Write a program to use recursion. Ans. A recursive function is a function that calls itself during its execution. This enables the function to repeat itself several times, outputting the result and the end of each iteration. Below is an example of a recursive function. Ex. Inc() { Inc(); } Program. Int fact(int); Void main() { Int no, f;

Printf(“enter a no.”); Scanf(“%d”,&no); F=fact(no); Printf (“factorial of %d is %d”,no,f); } Int fact(int x) { If(x==0) Return 1; Else Return (x*fact(x-1)); } 5.a>write a program to find the no. of times agiven element occur n an array. #include<stdio.h> Void main() { Int A [100], I,n,no,c=0; Printf(“enter the no. of elements u want to store in array”); Scanf(“%d”,&n); Printf(“enter the elements in to array”); For(i=0;i<n;i++) { Scanf(“%d”,&A[i]); } Printf(“enter the element to be searched”); Scanf(“%d”,&no); For(i=0;i<n;i++) { If(A[i]==no) { C++; } } Printf(“the given NO. %d is present %d times”,no, c); } 5.B> Write a menu driven program by using switching case to perform all the arithmetic operations. Program. #include <stdio.h> Void main() { Int A,B, ch,R,c; Do { Printf(“******* MENU**********”); Printf(“ \n 1.Addition \n 2.subtraction \n 3.multiplication \n 4.division); Printf(“enter your choice [1 to 4]”);

Scanf(“%d”,&ch); Switch(ch) { Case 1: printf(“enter two numbers.”); Scanf(“%d%d”,&A,&B); R=A+B; Printf(“Result is %d”,R); Break; Case 2: printf(“enter two numbers.”); Scanf(“%d%d”,&A,&B); R=A-B; Printf(“Result is %d”,R); Break; Case 3: printf(“enter two numbers.”); Scanf(“%d%d”,&A,&B); R=A*B; Printf(“Result is %d”,R); Break; Case 4: printf(“enter two numbers.”); scanf(“%d%d”,&A,&B); R=A/B; Printf(“Result is %d”,R); Break; Default: Printf(“your choice is wrong”); } Printf(“do u want to cont. enter 1/0 for y/n”) Scanf(“%d”,&c); }while(c==1); } 6.A> Write a program to process student records by using structures #include<stdio.h> Struct student { Char name[30];branch[20],regd_n0[20]; Int mark; }; Void main() { Struct student s; Printf(“enter student records”); Scanf(“%s%s%s%d”,s.name,s.branch,s.regd_no,&s.mark); Printf(“student details are”);

Printf(“%s%s%s%d”,s.name,s.branch,s.regd_no,s.mark); } +6.b>write a c program to convert binary to decimal and decimal to binary based as on the user option. Program #include<stdio.h> Main() { Long int no; Int no1,A[10],I=0,ch,j,R; Printf(“*************menu**************”); Printf(“1.decimal to binary \n 2.binary to decimal”); Printf(“enter your choice”); Scanf(“%d”,&ch); Switch(ch) { Case 1: Printf(“enter a decimal no.”); Scanf(“%d”,&no1); While(no1!=0) { A[i]=no1%2; No1=no1/2; I++; } Printf(“the equivalent binary form is ”); For(j=i-1;j>=0 ; j--) { Printf(“%d”,A[j]) }; Break; Case 2: J=1; printf("Enter any number any binary number: "); scanf(“%ld”,&no); while (no!=0) { R=no%10; No1=no1+r*j; J=j*2; No=no/10; } Printf(“the equivalent decimal no is %d”,no1); Break; Default: Printf(“ur choice is wrong”); }

} 7. Write short notes on A. switch case: It is another control instruction in C.

It is a multiple selection state ment. A switch statement allows a variable to be tested for equality

against a list of values. Each value is called a case, and the variable being switched on is checked for

eachswitch case.

Syntax

switch(exp/constant)

{

Case canstant1:

Statements;

Case constant2:

Statements;

>>>>>>>>>>>

>>>>>>>>

Deafault:

Statement;

}

B.multidimensional Array. Array is collection of similar data elements under a unique name in a successive memory location. An Array which has a single subscript is called one dimensional array, which has two ,is called two dimensional and which array has more than two subscript is called multi dimensional array. Syntax to declare is datatype ARR_Name[size][size][size]; eg. Int A[10][10][10], there will be at most 1000 elements.

C.Library function C program is a collection of function.functions are categorized intwo types.

1. User defined function. 2. Predefined function/ Library function.

User de fined functionare defined by the programmer as per their requirement . Howeve library functions are defined already to compiler and stored in respective header file. i.e Clibrary is collection of hrader files. These function stored in library in C program. C is very rich with its functions. These can be used with out defining in your program. Which reduce redundant code , decrease the size of code and save time. Some eg of library functions. Printf(),scanf(),gets(),puts(),power()etc. D. Actual parameter Vs formal parameter Parra meter means the value passed to a function to perform something .or

Values Supplied to Function so that Function can Utilize These Values.

Actual parameter means the value variable or identifier used in method ,where function is called or Actual parameters are parameters as they appear in function calls.

Formal parameter are parameters as they appear in function definition Chages in formal parameter will not be affected to actual parameter. formal parameters are bound to an actual value only as long as their method is active. When a

method returns to its caller, the formal parameters no longer contain any values formal parameter is photo copy of actual parameter.

eg.

Main()

{

Int i=5;

Incr(i);// I is actual parameter

}

Void incr(int x) // x is formal parameter

{

X++;

Printf(“%d”,X);

}

E. if else ladder.

It is one form of if statement/decision makin control Statement.

If we are having different - different test conditions with different - different statements, then for these kind of programming we need else if leader. Else if the leader is not interdependent to any other statements or anyother test conditions.

Syntax:

if(test_condition1) { statement 1; } else if(test_condition2) { statement 2; } else if(test_condition3) { statement 3; } else if(test_condition4) { statement 4; } -------------------------------- --- ------------------------------------------------------ // Test Condition And Codes As Per Requirement.

-------------------------------- else // at last, we use only else. { statement x; }

8. a> Write a program to calculate sum of 5 subjects and find percentage. Program #include<stdio.h> Void main() { Int m1 , m2 , m3, m4, m5, sum, perc,total=500; Printf(“enter mark in 5 subject”); Scanf(“%d%d%d%d%d”,&m1,&m2,&m3,&m4,&m5); Sum =m1+m2+m3+m4+m5; Perc=sum*100/total; Printf(“the sum of 5 sub is %d”,sum); Printf(“the percentage of 5 sub is %d”,perc); } B.write a C program to copy one file to another. #include<stdio.h> #include<process.h> #include<conio.h> void main() { FILE *fp1, *fp2; char a; clrscr(); fp1 = fopen("test.txt", "r"); if (fp1 == NULL) { puts("cannot open this file"); exit(1); } fp2 = fopen("test1.txt", "w"); if (fp2 == NULL) { puts("Not able to open this file");

fclose(fp1); exit(1); } do { a = fgetc(fp1); fputc(a, fp2); } while (a != EOF); fclose(fp1); fclose(fp2); getch(); } C..write a program to store records of 100 books Program. #include<stdio.h> Struct book { Char B_name[20],author[20]; Int page; }; Void main() { Struct book B[100]; Int I; Printf(“enter the record of100 books”); For(i=0; i<=99; i++ ) { Scanf(“%s%s”,B[i].B_name,B[i].author); Scanf(“%d”,&B[i].page); } Printf(“deatail of book”); For (i=0; i<=99;i++) { Printf(“%s%s%d”,B[i].B_name,B[i].author,B[i].page); } }


Recommended