Download The while loop

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Elementary mathematics wikipedia , lookup

Transcript
CUSTOMER_CODE
SMUDE
DIVISION_CODE
SMUDE
EVENT_CODE
APR2016
ASSESSMENT_CODE BCA1020_APR2016
QUESTION_TYPE
DESCRIPTIVE_QUESTION
QUESTION_ID
15137
QUESTION_TEXT
What is structure, structure definition? Explain with example.
1.structure: (3 marks)
a.Constructed data type in c.
b.Packing different data types.
c.Convenient to handle logically related data items.
2.definition:(7 marks)
a.Creates a format used to declare structure variable.
b.Keyword struct:
c.Each member can be of different types.
d.Template terminated with semicolon.
e.General format: structtag_name
{
SCHEME OF EVALUATION Data_type member 1;
Data_type member 2;
};
f.Example: structbook_bank
{
Char title[20];
Char author[15];
Int pages;
Float price;
}book1,book2;
QUESTION_TYPE DESCRIPTIVE_QUESTION
QUESTION_ID
15138
Explain the Following loops in C
QUESTION_TEXT 1. The While Loop
2. The Do..While Loop
SCHEME OF
EVALUATION
The while loop
Loops generally consist of two parts: one or more control expressions
which control the execution of the loop, and the body, which is the
statement or set of statements which is executed over and over.
The most basic loop in C is the while loop. A while loop has one control
expression, and executes as long as that expression is true. Here before
executing the body of the loop, the condition is tested. Therefore it is
called an entry-controlled loop
int x = 2;
while(x < 1000)
{
printf("%d\n", x);
x = x * 2;
}
The general syntax of a while loop is
while( expression )
statement(s)
A while loop starts out like an if statement: if the
condition expressed by the expression is true, the
statement is executed. However, after executing the
statement, the condition is tested again, and if it's still
true, the statement is executed again. (Presumably, the
condition depends on some value which is changed in the
body of the loop.) As long as the condition remains true,
the body of the loop is executed over and over again
The do…while loop
The do…while loop is used in a situation where we need to
execute the body of the loop before the test is performed.
Therefore, the body of the loop may not be executed at all
if the condition is not satisfied at the very first
attempt. Where as while loop makes a test of condition
before the body of the loop is executed.
For above reasons while loop is called an entry-controlled
loop and do..while loop is called an exit-controlled
loop.
do while loop takes the following form:
do
{
Body of the loop
}
while ( expression);
On reaching the do statement , the program proceeds to
evaluate the body of the loop first. At the end of the
loop, the conditional expression in the while statement is
evaluated. If the expression is true, the program continues
to evaluate the body of the loop once again. This process
continues as long as the expression is true. When the
expression becomes false, the loop will be terminated and
the control goes to the statement that appears immediately
after the while statement.
On using the do loop, the body of the loop is always
executed at least once irrespective of the expression.
QUESTION_TYPE
DESCRIPTIVE_QUESTION
QUESTION_ID
72829
QUESTION_TEXT
Write a program to generate Fibonacci numbers using functions.
SCHEME OF
EVALUATION
Program to generate Fibonacci numbers .
#include< stdio.h >
main()
{
int count, n;
long int fib(int);
printf(“\n How many Fibonacci numbers?”);
scanf(“%d\n”, &n);
for(count=1;count< =n;count++)
{
printf(“\ni=%d
F=%ld”, count, fib(count));
}
long int fib(int count)
{
/* calculate a Fibonacci number using the formula
if i=1, F=0; if i=2, F=1, and F=F1+F2 for i >=3 */
static long int f1=0, f2=1; /* declaration of static variables */
long int f;
if (count==1)
f=0;
else if (count==2)
f=1;
else
f=f1+f2;
f2=f1;
f1=f;
/* f1 and f2 retain their values between different calls
of the function*/
return f;
}
QUESTION_TYPE
DESCRIPTIVE_QUESTION
QUESTION_ID
72830
QUESTION_TEXT
Write a program to find factorial of a given positive integer.
Program to find factorial of given a positive integer.
#include< stdio.h >
main()
{
int n;
long int fact(int);
/* Read in the integer quantity*/
scanf(“%d”, &n);
/*calaculate and display the factorial*/
SCHEME OF EVALUATION
printf(“n!=%ld\n”, fact(n));
}
long int fact(int n)
{
if(n==0)
return(1);
else
return (n*fact(n-1));
}
QUESTION_TYPE
DESCRIPTIVE_QUESTION
QUESTION_ID
125350
QUESTION_TEXT
Explain if-else statement and nesting of if else statements with an
Example.
SCHEME OF
EVALUATION
1.
If – else statement (05marks)
a.
If statement have optional second statement … else clause.
b.
Else executed when condition not met.
c. Syntax:
If(expression)
Statement(s)
Else
Statement(s)
If more than one statement enclose within braces.
d)Example: program to find whether number is negative or + ve
#include<stdio.h>
Void main()
{
intnum;
printf(“enter the number\n”);
scanf(“%d”,&num);
if(num<0)
printf(“the number is negative”);
else
printf(“the number is positive”);
}
2.
nesting of if else statements (05marks)
a.
Nest one if statement inside another.
b.
Indent various levels.
c.
Multiple alternative
d.
Example:if(x<=y)
If(x<y)
Printf(“%d is< %d”,x,y);
Else
Printf(“%d ==%d”,x,y);
Else
Printf(“%d >%d”,x,y);
Syntax: if(expression1)
If(expression 2)
Statement 1
Else
Statement 2
Else
Statement 3
QUESTION_T
DESCRIPTIVE_QUESTION
YPE
QUESTION_ID 125352
QUESTION_T
What is formatted input and output? Explain with example
EXT
1 scanf():
a.
Formatted input is scanf() function.(5marks)
b.
Input can be entered into computer from standard input device.
c. Syntax: scanf(control string,arg1,arg2,…..argn) where control string
containing certain required formatting information, arg1,arg2 ,argn ….
Individual data items.
d.
Control characters
e.
Ex: scanf(“%d%f%c%s”,&I,&f,&c,str);
SCHEME OF
EVALUATION
2 Printf (05marks)
a.
Formatted output is printf() function.
b.
Output data can be written from computer onto standard output devices.
c.
Syntax: printf(control string,arg1,arg2,…..,argn);
d.
Example: 1)printf(“I is %d\n”,i); 2)printf(“hello\n”);