Unit 1 Essay Questions
Unit 1 Essay Questions
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:
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
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
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
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.
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
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 //
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 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.
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;
}
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;
}
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;
}
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;
}