Q1. Define Algorithm with example? Answer:- A sequential solution of any program that written in human language, called algorithm. Algorithm is first step of the solution process, after the analysis of problem, programmers write the algorithm of that problem. The number of instructions must be finite. To write the algorithm you first must know how to solve the problem! An algorithm produces the same output information given the same input information, and several short algorithms can be combined to perform complex tasks such as writing a computer program. Qualities of a Good Algorithm Inputs and outputs should be defined precisely. Each step in algorithm should be clear and unambiguous. Algorithm should be most effective among many different ways to solve a problem. An algorithm shouldn't have computer code. Instead, the algorithm should be written in such a way that, it can be used in similar programming languages. Example Q2. What is the use of flow chart? Answer: - A flow chart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. 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. Flowcharts are important for planning & working of a program. Flowcharts decreases our efforts i.e. they are easy to understand and check logics and algorithms. Flowcharts have become necessity as it is important to do paper work i.e. making logic and algorithm before creating a program on a Machine. It emphasizes individual steps & their interconnections. Once the flowchart is drawn, it becomes easy to write the program in any high level language. Must for the better documentation of a complex program. Symbols & Description Symbol Purpose Description Flow line Used to indicate the flow of logic by connecting symbols. Terminal(Stop/Start) Used to represent start and end of flowchart. Input/output Used for input and output operation. Processing Used for arithmetic operations and data-manipulations.
Decision On-page Connector Used to represent the operation in which there are two alternatives, true and false. Used to join different flow line Off-page Connector Predefined Process/Function Used to connect flowchart portion on different page. Used to represent a group of statements performing one processing task. Advantages of Flowchart:- 1. Communication: - Flowcharts are better way of communicating the logic of a system to all concerned. 2. Effective analysis: - With the help of flowchart, problem can be analyzed in more effective way. 3. Proper documentation: - Program flowcharts serve as a good program documentation, which is needed for various purposes. 4. Efficient Coding: - The flowcharts act as a guide or blueprint during the systems analysis and program development phase. 5. Proper Debugging: - The flowchart helps in debugging process. 6. Efficient Program Maintenance: - The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part. Q3.What is the different steps followed in the program development? Answer: - A computer is used to solve a problem. Steps are:- 1. Analyze the problem 2. Identify the variables involved 3. Design the solution 4. Write the program 5. Enter it into a computer 6. Compile the program and correct errors 7. Correct the logical errors if any 8. Test the program with data
9. Document the program Q4. Write about space requirements for variables of different data type? Answer:- Whenever we declare variable in Computer s memory, Computer must know the type of the data to be stored inside the memory. A data type specifies the type of data that a variable can store such as integer, floating, character etc. There are 4 types of data types in C language. Types Basic Data Type Derived Data Type Enumeration Data Type Void Data Type Data Types int, char, float, double array, pointer, structure, union Enum Void Basic Data Types The basic data types are integer-based and floating-point based. C language supports both signed and unsigned literals. The memory size of basic data types may change according to 32 or 64 bit operating system. Let's see the basic data types. It size is given according to 32 bit OS. Data Types Memory Size Range Char 1 byte 128 to 127 signed char 1 byte 128 to 127 unsigned char 1 byte 0 to 127 Short 2 byte 32,768 to 32,767 signed short 2 byte 32,768 to 32,767 unsigned short 2 byte 0 to 32,767 Int 2 byte 32,768 to 32,767 signed int 2 byte 32,768 to 32,767 unsigned int 2 byte 0 to 32,767
short int 2 byte 32,768 to 32,767 signed short int 2 byte 32,768 to 32,767 unsigned short int 2 byte 0 to 32,767 long int signed long int unsigned long int Float Double long double 4 byte 4 byte 4 byte 4 byte 8 byte 10 yte Q5. What is an operator? Describe the different types of operator that are included with in the c language with the example for each? Answer: - Operators:- "Operator is a special symbol whose meaning is fixed and used to perform operation on values or operands." Operators are used in programs to manipulate data & variable. Types Of Operators :- 1. Based On Operands 2. Based on Operation Types Of Operators On The Basis Of Operands : 1. Unary Operators 2. Binary Operators 3. Ternary Operators 1. Unary Operators: - This type of operator requires only one operand or variable to perform an operation. This operator is called unary Operator. (++, --,!) 2. Binary Operators: - This type of operator requires or takes only two operands, means at least two operands are compulsory required. (<, >,< =, +)
3. Ternary Operators: - This type of operator requires or takes three operands to perform operation is called Ternary Operator. (? :) Types Of Operators On The Basis Of Operation:- 1. Arithmetic Operators 2. Relational Operators 3. Logical Operators 4. Assignment Operators 5. Increment & Decrement Operators 6. Bitwise Operators 7. Misc Operators 1) Arithmetic Operators: The arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc. There are following arithmetic operators supported by C language: Assume variable A holds 10 and variable B holds 20 then: Operator Description Example + Adds two operands A + B will give 30 - Subtracts second operand from the first A - B will give -10 * Multiply both operands A * B will give 200 / Divide numerator by denumerator B / A will give 2 % Modulus Operator and remainder of after an integer division ++ Increment operator, increases integer value by one -- Decrement operator, decreases integer value by one B % A will give 0 A++ will give 11 A-- will give 9 2) Relational Operator:- These operators are used to compare two operands and Return true if relation is True, Return false if relation is False. These are Binary Operators
Operator Description Example == Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B) is not true.!= Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A!= B) is true. > Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. (A <= B) is true. 3) Logical Operator:- These are Binary Operators. These operators are used within if when there is more than one condition. There are three operators: 1) Logical And && 2) Logical OR 3) Logical Not! Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false. Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A B) is true.! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.!(a && B) is true. 4) Assignment Operators:- There are following assignment operators supported by C language: Operator Description Example = Simple assignment operator, Assigns values from right side operands to left side operand += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C = A + B will assign value of A + B into C C += A is equivalent to C = C + A C -= A is equivalent to C = C A C *= A is equivalent to C = C * A C /= A is equivalent to C = C / A C %= A is equivalent to C = C % A <<= Left shift AND assignment operator C <<= 2 is same as C = C << 2 >>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2 &= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator = bitwise inclusive OR and assignment operator C ^= 2 is same as C = C ^ 2 C = 2 is same as C = C 2 5) Bitwise Operators:- C language has a special facility that allows direct manipulation of individual bits of a data. This facility is provided by bitwise operator. Bitwise operators allow us to provide the facility to manipulate individual bits of a data where general operators directly manipulate the data. Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) = 12, i.e., 0000 1100 Binary OR Operator copies a bit if it exists in either operand. (A B) = 61, i.e., 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, i.e., 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) = -61, i.e. 1100 0011 in 2's complement form. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 = 240 i.e., 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 = 15 i.e., 0000 1111
6) Misc Operators sizeof & ternary Besides the operators discussed above, there are a few other important operators including sizeof and? : Supported by the C Language. Operator Description Example sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4. & Returns the address of a variable. &a; returns the actual address of the variable. * Pointer to a variable. *a;? : Conditional Expression. If Condition is true? then value X : otherwise value Y 7) Increment & Decrement Operator:- These are Unary operators. These are one of the powerful operators. Their symbol is: ++, -- Increment Operator (++):- This operator is used to increase value of a variable by one and always one. Decrement Operator (--):- This operator is used to decrease or reduce the value of a variable by one. For example int i = 10; i ++; then i--; then There are two forms for these two operators. 1. Prefix form 2.Postfix form i = 11 i = 9
1.Prefix form:- In this form the operator is fix before the operand. Example:- ++j --j => int t1, i = 10; t1 = ++i; evalution steps i=i+1-> 11 t1=i->11 Now t1=11 i=11 2. Postfix form:- In this form the operator will fix after the operand. Just like this i++, j++ In postfix form, the value of a variable use in an expression and after it will manipulate. int t1, i = 10; t1 = i ++; Evaluation steps t1 = i 10 i = i +111 Now t1 = 10 i =11 And int t1, i = 10; t1 = i--; t1 = i
i = i -1; Now t1 =10 i = 9