QA UNIT I Basics of C Programming
QA UNIT I Basics of C Programming
com/programming-in-c-
for-msbte-k-scheme/
312303 - Programming In ‘C’ (Sem II)
As per MSBTE’s K Scheme
CO / CM / IF / AI / AN / DS
S-22,
1 List any four key words used in ‘C’ W-23 S- 2M
19
Ans. auto Double int struct
W-23, S-
6 Drawandlabeldifferentsymbolsusedin flowchart. 2M
22, W-19
Ans.
Draw flow chart for finding largest number among three W-23, S-
7 4M
numbers. 22, S-19
Ans.
W-23, S-
8 State the use of printf() and scanf( ) with suitable example. 4M
22
Ans. The printf( ) function is used to display output and the scanf() function is used to take
input from users.
The printf( ) and scanf( ) functions are commonly used functions in C Language. These
functions are inbuilt library functions in header files of C programming.
printf( ) Function
In C Programming language, the printf( ) function is used for output.
printf( ) function can take any number of arguments. First argument must be
enclosed within the double quotes “hello” and every other argument should be
separated by comma ( , ) within the double quotes.
printf( ) function is defined in stdio.h header file. By using this function, we can
print the data or user-defined message on monitor (also called the console).
printf( ) can print a different kind of data format on the output string.
To print on a new line on the screen, we use “\n” in printf() statement.
C language is case sensitive programming language. For example, printf() and scanf()
in lowercase letters treated are different from Printf() and Scanf(). All characters in
printf() and scanf() builtin functions must be in lower case.
Syntax
printf("format specifier",argument_list);
#include<stdio.h>
Int main( )
return 0;
}
W-23, S-
6 Write a program to print Fibonacci series starting from 0 and 1. 6M
22, W-19
Ans. #include <stdio.h>
int main( )
{
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Write algorithm and draw flow-chart to print even numbers W-23, W-
7 4M
From 1 to 100. OR 1 to 50 18
Ans Algorithm
1. Start
2. Initialize the variable i to 1.
3. while i<=100
4. if i%2==0
5. print the number
6. increment value of i
7. stop
Flowchart
Ans.
Write a c program among three numbers find largest number
11 S-23 4M
Among three
Ans.
#include <stdio.h>
Int main( ) {
intA, B, C;
if(A >= B) {
if(A >= C)
else
else{
if(B >= C)
else
return 0;
}
12 Explain use comment in clanguage S-23 4M
Ans. The comments in C are human-readable explanations or notes in the source code of
a C program. A comment makes the program easier to read and understand. These
are the statements that are not executed by the compiler or an interpreter.
Types of comments in C
In C there are two types of comments in C language:
Single-line comment
Multi-line comment
Single-line Comment in C
A single-line comment in C starts with ( // ) double forward slash. It extends till the
end of the line and we don’t need to specify its end.
Multi-line Comment in C
The Multi-line comment in C starts with a forward slash and asterisk ( /* ) and ends
with an asterisk and forward slash ( */ ). Any text between /* and */ is treated as a
comment and is ignored by the compiler.
Ans.
C Header files offer the features like library functions, data types, macros, etc by
importing them into the program with the help of a preprocessor directive
“#include”.
19 Explain any four guide lines for preparation of flow chart. W-22 4M
Ans.
Types Data Types
Ans.
:
pow()- compute the power of a input value Syntax: double pow (double x, double y);
Ans.
Draw any two symbols used to construct flow chart. Also state
18 W-22 2M
Their use.
Ans.
Ans.
Ans. Algorithm:
Step 1:Start
Step 2:Declare variables no1,no2,no3
Step 3: Accept / Initialize values for variables no1,no2,no3
Step 4: If no1 >no2 and no1>no3 then display "no1 is largest" otherwise check if
no2>no1 and no2>no3 then display "no2 is largest" otherwise display "no3 is largest"
Step 5: Stop
S- 22,
Describethefollowingterms:(i)Keyword(ii)Identifier(iii) Variable (iv)
27 W-19, 4M
Constant
W-23
Ans Keyword: Keywords are special words in C programming which have their own
predefined meaning. The functions and meanings of these words cannot be altered.
Some keywords in C Programming are if, while, for, do, etc..
(ii) Identifier: Identifiers are user-defined names of variables, functions and arrays. It
comprises of combination of letters and digits. Example int age1; float height_in_feet;
Here, age1 is an identifier of integer data type. Similarly height_feet is also an identifier
but of floating integer data type,
(iii) Variable: A variable is nothing but a name given to a storage area that our programs
can manipulate. Each variable in C has a specific type, which determines the size and
layout of the variable's memory; the range of values that can be stored within that
memory; and the set of operations that can be applied to the variable. Example: add, a,
name
(iv) Constant:
Constants refer to fixed values that the program may not change during its execution.
These fixed values are also called literals. Constants can be of any of the basic data types
like an integer constant, a floating constant, a character constant, or a string literal.
There are enumeration constants as well.
28 Writeaprogramtodisplaytableofgivennumber(Accept S-22 4M
numberfromuser).
Ans.
1. #include <stdio.h>
2. int main( ) {
3. int num, i; // declare a variable
4. printf (" Enter a number to generate the table in C: ");
5. scanf (" %d", &num); // take a positive number from the user
6.
7. printf ("\n Table of %d", num);
8. // use for loop to iterate the number from 1 to 10
9. for ( i = 1; i <= 10; i++)
10. {
11. printf ("\n %d * %d = %d", num, i, (num*i));
12. }
13. return 0;
14. }
29 Write a program to sum all the even numbers between 1 to 100. S-22 4M
Ans. /**
* C program to print sum of all even numbers between 1 to n
*/
#include <stdio.h>
int main()
{
int i, n, sum=0;
return 0;
}
Ans
40 Explain any four bit-wise operator used in ‘C’ with example. S-19 4M
Ans. Bitwise OR |
It takes 2 bit patterns and performs OR operations on each pair of
corresponding bits. The following example will explain it.
1010
1100
--------
OR 1110
Bitwise NOT
One s complement operator (Bitwise NOT) is used to convert each
-bit to 0- -bit to1-
unary operator i.e. it takes only one operand.
1001
NOT 0110
Bitwise XOR ^
Bitwise XOR ^, takes 2 bit patterns and perform XOR operation with
it.
(ii)
S-18
(iii) 2M
(iv)`
Ans.
53 State the use of printf( ) & scanf( ) with suitable example. S-18 4M
Ans. The printf() function is used to display output and the scanf() function is used
to take input from users.
The printf() and scanf() functions are commonly used functions in C
Language. These functions are inbuilt library functions in header files of C
programming.
________________________________________
printf() Function
In C Programming language, the printf() function is used for output.
printf() function can take any number of arguments. First argument must be
enclosed within the double quotes “hello” and every other argument should
be separated by comma ( , ) within the double quotes.
Important points about printf():
• printf() function is defined in stdio.h header file. By using this function,
we can print the data or user-defined message on monitor (also called the
console).
• printf() can print a different kind of data format on the output string.
• To print on a new line on the screen, we use “\n” in printf() statement.
C language is case sensitive programming language. For example, printf() and
scanf() in lowercase letters treated are different from Printf() and Scanf(). All
characters in printf() and scanf() builtin functions must be in lower case.
Syntax
printf("format specifier",argument_list);
The format string for output can be %d (integer), %c (character), %s (string),
%f (float) %lf (double) and %x (hexadecimal) variable.
Simple Example of printf() Function
#include<stdio.h>
int main(){int num = 450;// print numberprintf("Number is %d \n", num);
54 Develop a simple ‘C’ program for addition and multiplication S-18 4M
of
Two integer numbers.
Ans. #include<stdio.h>
#include<conio.h>
void main()
int a,b,add,mul;
clrscr();
scanf("%d%d",&a,&b);
add=a+b;
mul=a*b;
getch();
55 Explain any four library functions under conio.h header file. S-18 4M
Ans. clrscr() -This function is used to clear the output screen.
getch() -It reads character from keyboard
getche()-It reads character from keyboard and echoes to o/p screen
putch - Writes a character directly to the console.
textcolor()-This function is used to change the text color
textbackground()-This function is used to change text background
56 Explain how formatted input can be obtain , give suitable S-18 4M
example.
Ans. Formatted input:
When the input data is arranged in a specific format, it is called
formatted input. scanf function is used for this purpose in C.
General syntax:
scanf(“control string”, arg1, arg2..);
Control string here refers to the format of the input data. It includes
the conversion character %, a data type character and an optional
number that specifies the field width. It also may contain new line
character or tab. arg1, arg2 refers to the address of memory locations
where the data should be stored.
Example:
scanf(“%d”,&num1);
Eg:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
printf("Enter a number");
scanf("%d",&i);
printf("Entered number is: %d",i);
getch();
}
57 Write a program to swap the values of variables a = 10 , b = 5 S-18 4M
using function.
Ans.
#include<stdio.h
void swapvalues(int *i, int *j)
{
int temp;
temp=*i;
*i=*j;
*j=temp;
}
void main() {
int a=10;
int b=5;
clrscr();
printf("The values before swaping:\na=%d, b=%d",a,b);
swapvalues(&a,&b);
printf("\nThe values after swaping:\na=%d, b=%d",a,b);
getch();
}
58 Design a program to print a message 10 times. S-18 4M
Ans. #include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=0;i<10;i++)
{
printf("Welcome to C programming\n");
}
getch();
}
59 Implement a program to demonstrate logical AND operator. S-18 4M
Ans. #include<stdio.h>
#include<conio.h>
void main()
{
int i;
int j;
clrscr();
printf("Enter the values of i and j");
scanf("%d%d",&i,&j);
if(i==5 && j==5) {
printf("Both i and j are equal to 5");
} else {
printf("Both the values are different and either or both are not
equal to 5");
}
getch();
}
60 Design a program in C to read the n numbers of values in an S-18 6M
Array and display it in reverse order.
#include<stdio.h>
#include<conio.h>
#define max 50
void main()
{
int a[max],i,n;
clrscr();
printf("\n Enter number of elements:");
scanf("%d",&n);
printf("\n Enter array element:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Array elements in reverse order:");
for(i=n-1;i>=0;i--)
printf("\t%d",a[i]);
getch();
}
sThank You
[Link]
scheme/
Visit
[Link]