Module I Answers
Module I Answers
Computing Environments:
A computer system uses many devices, arranged in different ways to solve many problems. This
constitutes a computing environment where many computers are used to process and exchange
information to handle multiple issues
FLOWCHART:
What is flowchart?
A flowchart is a type of diagram that represents an algorithm, workflow or process, showing the
steps as boxes of various kinds, and their order by connecting them with arrows. This
diagrammatic representation illustrates a solution model to a given problem. Flowcharts are used
in analyzing, designing, documenting or managing a process or program in various fields
Symbols of flowchart:
The Following Figure shows the process of creating, compiling and executing a C Program
1. Finiteness: An algorithm should have finite number of steps and it should end after a
finite time
2. Definiteness: Each step must be clear, well-defined and precise. There should be no any
ambiguity
3. Input − An algorithm should have 0 or more well-defined inputs.
4. Output − An algorithm should have 1 or more well-defined outputs, and should match
the desired output
5. Effectiveness: Each step must be simple and should take a finite amount of time
6. Feasibility − Should be feasible with the available resources
7. Independent − An algorithm should have step-by-step directions, which should be
independent of any programming code.
Read a, b, c values
Compute d = b2 4ac
if d > 0 then
o r1 = b+ sqrt (d)/(2*a)
o r2 = b sqrt(d)/(2*a)
Otherwise if d = 0 then
Stop
1. Arithmetic operators:
Arithmetic operators are used to perform mathematical calculations like addition, subtraction,
multiplication, division and modulus in C programs.
(A=10,B=20)
Example
#include<stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}
Output:
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
2. Relational operators:
Relational operators are used to find the relation between two variables i.e. to compare the
values of two variables in a C program.
Operato Description Example
r
== Checks if the values of two operands are equal or not. If yes, then (A = = B)
the condition becomes true. is not true.
!= Checks if the values of two operands are equal or not. If the values (A != B) is
are not equal, then the condition becomes true. true.
> Checks if the value of left operand is greater than the value of right (A > B) is
operand. If yes, then the condition becomes true. not true.
< Checks if the value of left operand is less than the value of right (A < B) is
operand. If yes, then the condition becomes true. true.
>= Checks if the value of left operand is greater than or equal to the (A >= B)
value of right operand. If yes, then the condition becomes true. is not true.
<= Checks if the value of left operand is less than or equal to the value (A <= B)
of right operand. If yes, then the condition becomes true. is true.
Example:
Output:
5 == 5 = 1
5 == 10 = 0
5>5=0
5 > 10 = 0
5<5=0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1
3. Logical operators
These operators are used to perform logical operations on the given
expressions.
There are 3 logical operators in C language. They are, logical AND
(&&), logical OR (||) and logical NOT (!).
&& Called Logical AND operator. If both the operands are non-zero, (A && B)
then the condition becomes true. is false.
! Called Logical NOT Operator. It is used to reverse the logical state !(A && B)
of its operand. If a condition is true, then Logical NOT operator will is true.
make it false.
Example :
4. Assignment operators
In C programs, values for the variables are assigned using assignment
operators
Example:
if the value “10” is to be assigned for the variable “sum”, it can be assigned as
“sum = 10”
Example:
#include <stdio.h>
main()
{
int a = 5, c;
c = a;
printf("c = %d \n", c);
c += a; // c = c+a
printf("c = %d \n", c);
c -= a; // c = c-a
printf("c = %d \n", c);
c *= a; // c = c*a
printf("c = %d \n", c);
c /= a; // c = c/a
printf("c = %d \n", c);
c %= a; // c = c%a
printf("c = %d \n", c);
}
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
Example:
Let a=5 and b=10
a++; //a becomes 6
a--; //a becomes 5
++a; //a becomes 6
--a; //a becomes 5
When i++ is used as prefix(like: ++var), ++var will increment the value
of var and then return it but, if ++ is used as postfix(like: var++),
operator will return the value of operand first and then only increment it.
Syntax :
Example :
(A > 100 ? 0 : 1);
In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else
conditional statements.
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
Output:
y value is 2
7. Bitwise operators
These operators are used to perform bit operations. Decimal values are
converted into binary values which are the sequence of bits and bit wise
operators work on these bits.
Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~
(bitwise OR), ^ (XOR), << (left shift) and >> (right shift).
Example:
#include>stdio.h>
main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
}
Output:
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
8. Special operators
C supports some special operators to perform some specific tasks.
Example:
#include<stdio.h>
main()
{
int a; float b;
double c; char d;
printf("Size of int=%d bytes\n",sizeof(a));
printf("Size of float=%d bytes\n",sizeof(b));
printf("Size of double=%d bytes\n",sizeof(c));
printf("Size of char=%d byte\n",sizeof(d));
}
Output:
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
The compiler converts all operands into the data type of the largest operand.
The sequence of rules that are applied while evaluating expressions are given
below:
The type conversion performed by the programmer by posing the data type of
the expression of specific type is known as explicit type conversion. The explicit
type conversion is also known as type casting.
Where, data_type is any valid c data type, and expression may be constant, variable or
expression.
Example: 1x=(int)a+b*d;
The following rules have to be followed while converting the expression from one type to
another to avoid the loss of information;
1. All integer types to be converted to float.
2. All float types to be converted to double.
3. All character types to be converted to integer
Examples:
/*implicit type conversion example*/
#include<stdio.h>
main()
{
int i = 17;
char c = 'c'; /* ascii value is 99 */
int sum;
sum = i + c;
printf("Value of sum : %d\n", sum );
}
Output:
Value of sum : 116
Output:
1.400000
Output: Output means to display data on screen or write the data to a printer or a file.
C programming language provides many built-in functions to read any given input and to display
data on screen when there is a need to output the result. C programming language has standard
libraries that allow input and output in a program. The stdio.h or standard input output library in
C that has methods for input and output.
Input output built-in functions in C falls into two categories, namely, formatted
input output (I/O) functions and unformatted input output (I/O) functions.
printf() and scanf() are examples for formatted input and output
functions and getch(), getche(), getchar(), gets(), puts(), putchar()
etc. are examples of unformatted input output functions.
The function printf() is used for formatted output to standard output based on a
format specification. The format specification string, along with the data to be
output,are the parameters to the printf() function.
The function scnaf() reads and converts characters from the standards input
depending on the format specification string and stores the input in memory
locations represented by the other arguments (num1, num2,….).
For Example:
scanf(― %c %d‖,&Name, &Roll No);
Example:
Example:
#include<stdio.h>
main()
{
int a;
float f;
double d;
char ch;
printf("Enter char,int,float and double values:\n");
scanf("%c%d%f%lf",&ch,&a,&f,&d);
printf("\n char ch=%c\n",ch);
printf("int a=%d\n",a);
printf("float f=%f\n",f);
printf("double d=%lf\n",d);
}
Output:
Enter char,int,float and double values:
r
15
63.75
123568.59
char ch=r
int a=15
float f=63.750000
double d=123568.590000