0% found this document useful (0 votes)
38 views13 pages

Unit 1 Essay Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views13 pages

Unit 1 Essay Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

UNIT-1 :

1. Explain Algorithm and flowchart and write algorithm and draw flowchart to swap
two numbers.
Ans.
Algorithm:- An algorithm is a finite set of step-by-step instructions to solve a problem. It
helps programmer in breaking down the solution into no. of sequential steps.
The essential properties of an algorithm are:
Finiteness: The algorithm must always terminate after a finite number of steps.
Definiteness: Each and every instruction should be precise and unambiguous i.e. each and
every instruction should be clear and should have only one meaning.
Effectiveness: Each instruction should be performed in a finite amount of time.
Input and Output: An algorithm must take zero or more inputs, and produce one or more
outputs.
Generality: An algorithm applies to different sets of the same input type to generate
different outputs.
Flowchart:- Flowchart is a pictorial or symbolic representation of an algorithm. It indicates
the process of solution. They are constructed by using special geometrical symbols. Each
symbol represents an activity. Flowcharts are read from top to bottom unless a branch alters
the normal flow.
Algorithm to the swap of 2 numbers.
Step 1 :Start
Step 2 :Read two values into two variables a , b.
Step 3 :Declare third variable, c
Step 4 : compute
c=a
a=b
b=c
Step 5 :Print or display a, b
Step 6 :Stop

2. Explain the basic structure of C program and explain the significance of each
section. Explain Compilation and Execution of C program in detail.
Ans.
Basic Structure of C program:-
Structure of a C Program : C program is a collection of one or more functions. Every
function is a collection of statements, performing a specific task. The structure of a basic C
program is:

Documentation section : The documentation section consists of a set of comment lines


giving the name of the program, the author and other details, which the programmer would
like to use later. There are two types of comment lines.
Block comment lines /* */ and
Single line comment lines. //

Link section:
The link section provides instructions to the compiler to link functions from the system library.

#include directive
The #include directive instructs the compiler to include the contents of the file "stdio.h"
(standard input output header file) enclosed within angular brackets.

Definition section : The definition section defines all symbolic constants. #define PI 3.1413

Global declaration section :


There are some variables that are used in more than one function. Such variables are called
global variables and are declared in the global declaration section that is outside of all the
functions. This section also declares all the user-defined functions.

main() function section : Every C program must have one main function section.
This section contains two. parts:
Declaration part: It declares all the variables used in the executable part.
Executable part: There is at least one statement in the executable part.

These two parts must appear between the opening and closing braces. The program
execution begins at the opening brace and ends at the closing brace. The closing brace of
the main function is the logical end of the program. All statements in the declaration and
executable part end with a semicolon.
Subprogram section:
The subprogram section contains all the user-defined functions that are called in the main ()
function. User-defined functions are generally placed immediately after the main () function,
although they may appear in any order.
For Example a ‘C’ program that prints welcome message:
#include<stdio.h>
int main( )
{
printf(“Welcome to C Programming\n”);
return 0;
}
Output:
Welcome to C Programming

3. Explain Primitive Data Types in C.


Ans.
Primitive Data Types: The variables which are declared with the primitive data types
occupies single memory cell only. Primitive data types listed as follows:
Integer
Character
Float
Void
Integer: Integer data type is used to declare a variable that can store numbers only. The
keyword used to declare a variable of integer type is “int”.
Syntax :- int variable_name;
ex: int a; integer is again classified as given below:

Data Type Memory Size Range. Format Specifier


int. 2B. %d
short int. 1B. -(2)^7 to (2)^7-1. %d
long int. 4B. -(2)^31 to (2)^31-1 %u
Unsigned int 2B. 0 to (2)^16-1. %u
unsigned short int 1B. 0 to (2)^8-1. %d
unsigned long int. 4B. 0 to (2)^32-1. %u
Character: character data type is used to declare a variable that can store characters/single
alphabets only. The keyword used to declare a variable of character type is “char”.
Syntax :- char variable_name; ex: char gender= ‘f’;
character is classified as follows:
char
signed char
unsigned char
Data type. Memory Size. Range. FormatSpecifier
char 1B. -(2)^7 to (2)^7-1. %c
unsigned char. 1B. 0 to (2)^8-1. %c

Float: float data type is used to declare a variable that can store real constant numbers only.
The keyword used to declare a variable of float type is “float”.
Syntax :- float variable_name;
ex: float r = 1.235;
Float can be classified as follows:
float
double
long double

Data type. Memory. Range Format Specifier


Float. 4B. 3.4E-38 to 3.4E+38. %f
Double. 8B. 1.7E-308 to 1.7E+308. %lf
long double 10B. 3.4E-4932 to 1.1E+4932. %lf

void type: it holds no value and thus valueless. It is primarily used in three cases:
1.To specify the return type of a function (when the function returns no value)
2.To specify the parameters of the function (when the function accepts no arguments from
the caller.)
3.To create generic pointers.

4. Write the importance of precedence and associativity? Write the table for operator
precedence?.
Ans.
Operator Precedence : It defines the order in which operators in an expression are
evaluated depending on their relative precedence.
Example: Let us see x=2+2*2
1st pass- 2+2*2
2nd pass- 2+4
3rd pass- 6 that is x=6.
Associativity : it defines the order in which operators with the same order of precedence
are evaluated.
Ex: Let us see x = 2/2*2
1st pass-- 2/2*2
2nd pass-- 1*2
3rd pass-- 2 that is x=2
The below table lists C operators in order of precedence (highest to lowest) and their
associativity indicates in what order operators of equal precedence in an expression are
applied.

S.No. Operators. Associativity


1 ( ) [ ] → . ++ (postfix) − −(postfix). L to
R
2. ++ (prefix) -- (prefix)! ~ sizeof(type) + (unary) − (unary). R
to L
&(address) * (indirection)
3. * / %. L to R
4. + −. L to
R
5. <<>>. L to R
6. <<= >>=. L to R
7. = = !=. L to R
8. &. L to R
9. ^. L to R
10. |. L to R
11. &&. L to R
12. | |. L to R
13. ?: R to L
14. = += −= *= /= %= >>= <<= &= ^= |=.
R to L
15. , L to R

Program: /* operator associativity */


#include<stdio. h> int main()
{
int a, b=4,c=8,d=2,e=4,f=2;
a=b+c/d+e*f; // expression1
printf(" The value of a is%d", a);
a=(b+c)/d+e*f; // expression2

printf("The value of a is%d", a);


a=b+c/((d+e)*f); // expression3 printf("The value of a is%d", a);
return 0;
}
Output:
The value of a is 16 The value of a is 14 The value of a is 6
5. How does a control string in a printf() function differ from the control string in
scanf() function? Write commonly used scanf() format codes.
Ans.
Formatted input and output functions:
When input and output is required in a specified format the standard library functions
scanf() and printf() are used
Output function printf( ) : printf() is a function that is used to print any data on the video
monitor screen.
Syntax:
printf(“control string”,list_of_variables);
The function accepts two arguments - control string, which controls what gets printed,
and list of variables.
The control string is all-important because it specifies
The type of each variable in the list and how the user wants it printed. It is also called a
format specifier.
Characters that are simply printed as they are.
Control string that begins with a % sign.
Escape sequences that begin with a \ sign

List of variables must be separate by comma.

The number of format specifiers must exactly match the number of variables.
Format specifiers
Format-specifier. Data Type
%c. Char (Single character)
%c. unsigned char
%s. String
%d. Int
%u. unsigned int
%hd. short int
%ld. long int
%lu. unsigned long Int
%o. Int Octal values
%x. Int Hexa decimal values
%f. Float Floating-point value
%lf. Long float/double

Flag characters used in printf


Flag Meaning
−. Left justify the display
+. Display the positive or negative sign of value
0. Pad with leading zeros
Space. Display space if there is no sign

Examples:
1. The simplest printf statement is
printf(“Welcome to the world of C language”); //When executed, it prints the string
enclosed in double quotes on screen.
Input function scanf() : C provides scanf( ) function for entering input data. This function
accepts all types of data as input (numeric, character, string).
Syntax: scanf(“controlstring”,variable1,variable2,…,variable n);
This function should have at least two parameters.
-First parameter is control string which is a conversion specification character. It should be
within double quotes. This may be one or more, it depends on the number of variables.
-The other parameter is variable names.
Each variable must be preceded by an ampersand (&) sign. This gives the starting address
of the variable name in memory. scanf ( ) uses all the format specifiers used in printf( )
function.
Note: comments are not allowed in scanf()

Program:
#include <stdio.h>
int main()
{
int x;
printf(“Enter number:”); scanf(“%d”, &x);
printf(“The value of x is %d\n”, x); return 0;
}
Output
Enter a number: 45 The value of x is 45
Program: /* For accessing multiple values from keyboard using scanf() function */ #include
<stdio.h>
int main()
{
int x,y;
printf(“Enter two numbers:”); scanf(“%d%d”, &x,&y);
printf(“The value of x is %d\n”, x); printf(“The value of y is %d\n”, y);
return 0;
}
Output :
Enter 2 numbers: 29 47
The value of x is 29
The value of x is 47.

6. What is meant by type conversion? Why is it necessary? Explain about implicit and
explicit type conversion with examples.
Ans.
Type Conversion :
Type casting: Type casting is a way to convert a variable from one data type to another
data type.
It can be of two types:
Implicit type casting
Explicit type casting
Implicit type casting : When the type conversion is performed automatically by the
compiler without programmer’s intervention, such type of conversion is known as implicit
type conversion or type promotion. In this, all the lower data types are converted to it’s next
higher data type.
If the both operands are of the same type, promotion is not needed. If they are not,
promotion follows these rules:
-float operands are converted to double.
-char or short are converted to int.
-If anyone operand is double, the other -operand is also converted to double, and that is the
type of result.(or)If anyone operand is long, the other operand is also converted to long, and
that is the type of result.
-If anyone operand is unsigned, the other operand is also converted to unsigned, andthat is
the type of result.
-The smallest to the largest data types with respect to size are given as follows: figure

Program:
//To perform implicit type casting//
#include<stdio.h>
int main()
{
int sum, num=17;
char ch='A';
sum=num+ch;
printf("The value of sum=%d\n", sum); return 0;
}
Output:
The value of sum= 82 //sum=num+ch=>17+65 //

Explicit typecasting: Which is intentionally performed by the programmer for his


requirement in a C program?The explicit type conversion is also known as type casting.We
can convert the values from one type to another explicitly using the cast operator as follows:
Syntax: (data_type) expression;
The following rules have to be followed while converting the expression from one type to
another to avoid the loss of information:
All integer types to be converted to float.
All float types to be converted to double.
All character types to be converted to integer.
Ex: Let us look at some examples of type casting.
res = ( int ) 9.5;
9.5 is converted to 9 by truncation then assigned to res.
res = ( int ) 12.3 / ( int ) 4.2 ;
It evaluated as 12/4 and the value 3 is assigned to res.
res = ( double ) total / n;
total is converted to double and then division is done in float point mode.

res = ( int )(a + b);


The value of (a + b) is converted to integer and then assigned to res.
res = ( int )a + b;
a is converted to int and then added with b.

7. Explain formatted IO in C in detail with examples.


Ans.
Formatted I/O Functions :Formatted I/O functions are used to take various inputs from the
user and display multiple outputs to the user. These types of I/O functions can help to
display the output to the user in different formats using the format specifiers. These I/O
supports all data types like int, float, char, and many more.
Why are they called formatted I/O?
These functions are called formatted I/O functions because we can use format specifiers in
these functions and hence, we can format these functions according to our needs.
List of format specifiers-
Format Specifier. Type
%c. Character
%d. Signed integer
%f. Float values
%hi. Signed integer (short)
%hu. Unsigned Integer (short)
%i. Unsigned integer
%l or %ld. Long
%lf. Double
%Lf. Long double
%lu. Unsigned int or unsigned
%llu. Unsigned long long
%o. Octal representation
%p. Pointer
%s. String
%u. Unsigned int

There are two formatted input/output functions.


(a) scanf(): Used for formatted input and
(b) printf(): Used to obtain formatted output

scanf(): scanf() function is used in the C program for reading or taking any value from the
keyboard by the user, these values can be of any data type like integer, float, character,
string, and many more. This function is declared in stdio.h(header file), that’s why it is also a
pre-defined function. In scanf() function we use &(address-of operator) which is used to store
the variable value on the memory location of that variable.
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);

Control String (or)Format Specifier

Control string is enclosed within double quotes.It specifies the type of values that have to be
read from keyboard. Control string consists of field specification written in the form.% field
specification.

(ii) Address_list (or) list of variables


It contains addresses of input/output variables preceded. The addresses are specified by
preceding a variable by ‘&’ operator.

Example:
#include <stdio.h>
int main()
{
int num1;
printf("Enter a integer number: ");
scanf("%d", &num1);
printf("You have entered %d", num1);
return 0;
}

printf(): printf() function is used in a C program to display any value like float, integer,
character, string, etc on the console screen. It is a pre-defined function that is already
declared in the stdio.h(header file).
Syntax 1:
To display any variable value.
printf(“Format Specifier”, var1, var2, …., varn);
Example:
#include <stdio.h>
int main()
{
int a;
a = 20;
printf("%d", a);
return 0;
}

Syntax 2:
To display any string or a message
printf(“Enter the text which you want to display”);
Example:
#include <stdio.h>
int main()
{
printf("This is a string");
return 0;
}

Flag characters used in printf


Flag Meaning
−. Left justify the display
+. Display the positive or negative sign of value
0. Pad with leading zeros
Space. Display space if there is no sign
Examples:
1. The simplest printf statement is
printf(“Welcome to the world of C language”); //When executed, it prints the string

8. Write a C program to calculate simple interest & compound interest.


Ans.
//to calculate simple interest/
#include<stdio.h>
int main()
{
float principle,time,rate,si;
printf("Enter principle amount:");
scanf("%f",&principle);
printf("Enter time:");
scanf("%f",&time);
printf("Enter rate:");
scanf("%f",&rate);
si=(principle*time*rate)/100;
printf("Simple Interest=%f",si);
return 0;
}

//To calculate the compound interest//


#include <stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
double principle, rate, time, amount, CI;
printf("Enter the principle amount:\n ");
scanf("%lf",&principle);
printf("Enter the rate of interest:\n ");
scanf("%lf",&rate);
printf("Enter the time duration: \n");
scanf("%lf",&time);
amount = principle*(pow((1+rate/100),time));
CI = amount - principle;
printf(" The compound interest is: %lf",CI);
getch();
return 0;
}

9. Write a C program to convert Fahrenheit to Celsius degrees.


Ans.

Algorithm:
Step 1: Start
Step 2: Declare a variable F and C as float;
Step 2: Read the temperature in Fahrenheit F
Step 3: Convert the temperature to Celsius using the formula C = (F – 32)*5/9
Step 4: Print the temperature in Celsius C
Step 5: Stop
Flowchart: figure
PROGRAM:
// To convert fahrenheit to celsius//
#include<stdio.h>
#include<conio.h>
int main()
{
float fahrenit, celsius;
clrscr();
printf("\n enter fahrenit");
scanf("%f",& fahrenit);
celsius=(fahrenit-32)*5/9;
printf("\n celsius;%f", celsius);
getch();
return 0;
}

10. Write a C program to illustrate bitwise operators in C.


Ans.
//to perform bit wise operators//
#include<stdio.h>
#include<conio.h>
int main()
{
int a=4,b=2,c,d,e,f,g,h;
clrscr();
c=a&b;
printf("result=%d",c);
d=a|b;
printf("result=%d",d);
e=~a;
printf("result=%d",e);
f=a^b;
printf("result=%d",f);
g=a<<1;
printf("result=%d",g);
h=a>>1;
printf("result=%d",h);
getch();
return 0;
}

11. Write a C program to calculate the largest of 3 numbers with conditional operator.
Ans.
//to find larger of three numbers//
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,big;
printf("Enter a value:\n");
scanf("%d",&a);
printf("Enter b value:\n");
scanf("%d",&b);
printf("Enter c value:\n");
scanf("%d",&c);
big=(a>b&&a>c)?a:(b>c)?b:c;
printf("Big=%d",big);
getch();
return 0;
}

12. Evaluate the following expression.


X=A/B*C-B+A*D/3
Y=A+B*C+(D*E)+F*G
Where A=1, B=2, C=3, D=4, E=5, F=6, G=7
Ans.
Problem Statement Analysis
A = 1,B = 2 ,C = 3 ,D = 4 ,E-5, F = 6 ,G = 7

Y= A+B*C+(D*E)+F*G
=1+2*3+(4*5)+6*7
=1+2^ * 3 + 20 +6^ * 7
= 1 + 6 + 20 +6^ *7
= 1 + 6 + 20 + 42
=7+20+42
=27+42
= 69
X=A/B*C-B+A*D/3
=1/2*3-2+1*4/3
=0*3-2+1*4/3
=0-2+1*4/3
= 0 - 2 + 4/3
=0-2+1
=-2+1
=-1
PROGRAM :
//to perform the operators precedence //
#include<stdio.h>
#include<conio.h>
int main()
{
int A=1,B=2,C=3,D=4,E=5,F=6,G=7,x,y;
clrscr();
y=A+B*C+(D*E)+F*G;
printf("y=%d\n",y);
x=A/B*C-B+A*D/3;
printf("x=%d\n",x);
getch();
return 0;
}

You might also like