ICT Role in 21st Century Education & its Challenges.pptx
C q 1
1. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 1
QuestionQuestionQuestionQuestion 1111 Draw the functional block diagram of a Digital Computer and discuss its components in brief.
AnswerAnswerAnswerAnswer
Introduction to digital ComputerIntroduction to digital ComputerIntroduction to digital ComputerIntroduction to digital Computer
ComputerComputerComputerComputer
A computer is an electronic device that accepts data through input devices processes those data and gives
some meaning full information in the form of knowledge through output devices.
Digital ComputerDigital ComputerDigital ComputerDigital Computer
Those Computers, which accepts digits (like name, age, special characters etc.) as input called Digital
Computers.
Digital Computer has following units:Digital Computer has following units:Digital Computer has following units:Digital Computer has following units:
1.1.1.1. Input Unit
2.2.2.2. Central Processing Unit
3.3.3.3. Output Unit
Block diagram of Digital ComputersBlock diagram of Digital ComputersBlock diagram of Digital ComputersBlock diagram of Digital Computers
Digital computer is an electronic device. It comprises of many units. These units work in co ordination with
each other to perform the give task.
CPU
Output UnitProcessing UnitInput Unit
CU
ALU Output UnitInput Unit
Memory
2. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 2
Input UnitInput UnitInput UnitInput Unit
Computers need to receive data and instruction in order to solve any problem. Therefore we need to input the
data and instructions into the computers. The input unit consists of one or more input devices. Keyboard is the
one of the most commonly used input device. Other commonly used input devices are the mouse, floppy disk
drive, magnetic tape, etc. All the input devices perform the following functions.
Accept the data and instructions from the outside world.
Convert it to a form that the computer can understand.
Supply the converted data to the computer system for further processing.
Output Unit:Output Unit:Output Unit:Output Unit:
The output unit of a computer provides the information and results of a computation to outside world.
Printers, Visual Display Unit (VDU) are the commonly used output devices. Other commonly used output
devices are floppy disk drive, hard disk drive, and magnetic tape drive.
Central Processing UnitCentral Processing UnitCentral Processing UnitCentral Processing Unit
The CPU is like brain performs the following functions:
It performs all calculations.
It takes all decisions.
It controls all units of the computer.
CPU consists of following parts.
Central Processing UnitCentral Processing UnitCentral Processing UnitCentral Processing Unit
Memory UnitMemory UnitMemory UnitMemory Unit Arithmetic UnitArithmetic UnitArithmetic UnitArithmetic Unit Control UnitControl UnitControl UnitControl Unit
Memory UnitMemory UnitMemory UnitMemory Unit
The memory unit of the computer holds data and instructions that are entered through the input unit, before
they are processed. It preserves the intermediate and final results before these are sent to the output devices. It
also saves the data for the later use. The various storage devices of a computer system are divided into two
categories.
1. Primary memory:1. Primary memory:1. Primary memory:1. Primary memory: This memory is generally used to hold the program being currently executed in the
3. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 3
computer, the data being received from the input unit, the intermediate and final results of the program. The
primary memory is temporary in nature. The data is lost, when the computer is switched off. In order to store
the data permanently, the data has to be transferred to the secondary memory.
The cost of the primary storage is more compared to the secondary storage. Therefore most computers have
limited primary storage capacity.
Primary MemoryPrimary MemoryPrimary MemoryPrimary Memory It is further categorized into two categories:
(i)(i)(i)(i) ROM (Read Only Memory)ROM (Read Only Memory)ROM (Read Only Memory)ROM (Read Only Memory)
(ii)(ii)(ii)(ii) RAM (Random Access Memory)RAM (Random Access Memory)RAM (Random Access Memory)RAM (Random Access Memory)
(i)(i)(i)(i) ROMROMROMROM Computer cannot write data in it using special devices, instructions are written in it use
instructions are written in it. It is put inside the computer. Since data can only be read ROM it hence
it is called Read Only Memory. ROM physically exists in the form of blank electronic chip.
ROM is further divided into three categories.
(a)(a)(a)(a) PROMPROMPROMPROM
(b)(b)(b)(b) EPROMEPROMEPROMEPROM
(c)(c)(c)(c) EEPROMEEPROMEEPROMEEPROM
(a)(a)(a)(a) PROMPROMPROMPROM Those ROMs in which data once written can be programmable is called
Programmable Read Only Memory.
(b)(b)(b)(b) EPROMEPROMEPROMEPROM In this type of ROM data is written or erase, using ultraviolet rays is called Erasable
Programmable Read Only Memory.
(c)(c)(c)(c) EEPROMEEPROMEEPROMEEPROM This is Electrically Erasable Programmable Read Only Memory. In this data is
erased by electric pulses.
(ii)(ii)(ii)(ii) RAM (Random Access Memory)RAM (Random Access Memory)RAM (Random Access Memory)RAM (Random Access Memory) it provides both reading and writing facilities. As and when
required, Computer writes data or instructions in it and reads them whenever necessary. This is the
reason this is called Read/Write memory.
The data written in Random Access Memory remains there till the time computer power is on.
As soon as the power goes off data written in RAM gets erased. This is the reason why are also called
Temporary Memory.
2. Secondary memory:2. Secondary memory:2. Secondary memory:2. Secondary memory: It stores several programs, documents, data bases etc. The programs that you run on
the computer are first transferred to the primary memory before it is actually run. Whenever the results are
4. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 4
saved, again they get stored in the secondary memory. The secondary memory is slower and cheaper than the
primary memory. Some of the commonly used secondary memory devices are Hard disk, CD, etc.
ALU (Arithmetic and Logic Unit)ALU (Arithmetic and Logic Unit)ALU (Arithmetic and Logic Unit)ALU (Arithmetic and Logic Unit)
All calculations are performed in the Arithmetic Logic Unit (ALU) of the computer. It also does comparison
and takes decision. The ALU can perform basic operations such as addition, subtraction, multiplication,
division, etc and does logic operations viz, >, <, =, 'etc. Whenever calculations are required, the control unit
transfers the data from memory unit to ALU once the computations are done, the results are transferred to the
memory unit by the control unit and then it is send to the output unit for displaying results.
C U (ControlC U (ControlC U (ControlC U (Control Unit)Unit)Unit)Unit)
It controls all other units in the computer. The control unit instructs the input unit, where to store the data
after receiving it from the user. It controls the flow of data and instructions from the storage unit to ALU. It
also controls the flow of results from the ALU to the storage unit. The control unit is generally referred as the
central system of the computer that control and synchronizes its working.
QuestionQuestionQuestionQuestion 2222 Define Algorithm with its characteristics. Draw a flow chart of finding the greatest number
among three numbers.
AnswerAnswerAnswerAnswer
AlgorithmsAlgorithmsAlgorithmsAlgorithms
Logical sequence of steps describe complete solution to solve problem in finite amount of time
A method that can be used by a computer for the solution of a problem.
May involve alternation, iteration or recursion
More than one algorithm possible for the same task
Essential features of an algorithm:Essential features of an algorithm:Essential features of an algorithm:Essential features of an algorithm:
1.1.1.1. Input: The algorithm should take zero or more input.
2.2.2.2. Output: The algorithm should produce one or more outputs.
3.3.3.3. Definiteness: Each and every step of algorithm should be defined unambiguously.
4.4.4.4. Effectiveness: A human should be able to calculate the values involved in the procedure of the
algorithm using paper and pencil.
5.5.5.5. Termination: An algorithm must terminate after a finite number of steps.
/* A/* A/* A/* A flowchart to find the largest of three numbers A, B, and Cflowchart to find the largest of three numbers A, B, and Cflowchart to find the largest of three numbers A, B, and Cflowchart to find the largest of three numbers A, B, and C */*/*/*/
5. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 5
Question 3Question 3Question 3Question 3 Why “C” is called structured programming language? Explain.
AnswerAnswerAnswerAnswer
C language is called structured programming language because it is based on structured programming
approach.
StructuredStructuredStructuredStructured programming:programming:programming:programming: means the collection of principles and practices that are directed toward
developing correct programs which are easy to maintain and understand. A structured program is
characterized by clarity and simplicity in its logical flow.
The Three important constructs upon which structured programming is built are:
SequenceSequenceSequenceSequence Steps are performed one after the other.
SelectionSelectionSelectionSelection Logic is used to determine the execution of a sequence of steps. For example, if then else
construct.
IterationIterationIterationIteration A set of actions are repeated until a certain condition is met. For example while construct.
The various advantages of structured programmingadvantages of structured programmingadvantages of structured programmingadvantages of structured programming are:
1. Complexity of program reduces.
2. Easy maintenance.
3. Simplified understanding.
4. Reusability of code increases.
5. Testing and debugging become easier.
6. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 6
QuestionQuestionQuestionQuestion 4444Convert the following: (2.5)
I. (1101101.101)2 to Octal III. (357)8 to Decimal
II. (B678A) 16 to Binary IV. (125)10 to Hexadecimal
AnswerAnswerAnswerAnswer
I. (1101101.101)2 to Octal
001 101 101 . 101
1 5 5 . 5
AnswerAnswerAnswerAnswer ((((155.5155.5155.5155.5)8)8)8)8
I. (B678A) 16 to Binary
B 6 7 8 A
1011 0110 0111 1000 1110
AnswerAnswerAnswerAnswer (1011 0110 0111 1000 1110)(1011 0110 0111 1000 1110)(1011 0110 0111 1000 1110)(1011 0110 0111 1000 1110)2222
II. (357)8 to Decimal
7 * 80
=7
5 * 81
=40
3 * 82
=192
=239=239=239=239
AnswerAnswerAnswerAnswer (239)(239)(239)(239)10101010
III. (125)10 to Hexadecimal
16|125
16|7 13 >D
16|0 7
AnswerAnswerAnswerAnswer (7(7(7(7DDDD))))16161616
QuestionQuestionQuestionQuestion 5555 What are Translators? Why we need translators? Explain with reasons.
AnswerAnswerAnswerAnswer
Language TranslatorLanguage TranslatorLanguage TranslatorLanguage Translator
Translator – A translator is a program that translates our source code written in high level language (C
program) in machine code.
Source Code
Translator
Machine Code
7. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 7
Why do we need languageWhy do we need languageWhy do we need languageWhy do we need language translator?translator?translator?translator?
Computer can understand instructions only when they are written in their own language – the machine
language. Therefore, a program written in any other Language should be translated into machine language.
Special program called language processors or translators are available to do this job. These special programs
accept the user programs and check each statement and, if it is correct, produce a corresponding set of
machine language instructions.
Types of TranslatorTypes of TranslatorTypes of TranslatorTypes of Translator ––––
Translators are two types
1. Compiler
2. Interpreter
CompilerCompilerCompilerCompiler:::: A compiler converts the source program (user written program) into an object code (machine
language by checking the entire program before execution. If the program is error free, object program is
created and loaded into memory for execution. A compiler produces an error list of the program in one go
and all have to be taken care even before the execution of first statement begin. It takes less time for
execution.
InterpreterInterpreterInterpreterInterpreter:::: An interpreter is also a language translator that translates and executes statements in the
program one by one. It work on one statement at a time and if error free, executes the instruction before
going to second instruction. Debugging is simpler in interpreter as it is done in stages. An interpreter takes
more time for execution of a program as compared to a compiler.
QQQQuestionuestionuestionuestion 6666 What is Data Type? Define all type of data type supported by c language.
Answer:Answer:Answer:Answer:
Data Type of a variable tell to the compiler
Name of variable
Which type of value that variable can hold
How much memory space it reserve
Data types in c are –
1.1.1.1. Primary data type
2.2.2.2. Derived data type
3.3.3.3. User defined data type
1.1.1.1. PrimaPrimaPrimaPrimary data type orry data type orry data type orry data type or predefined data type orpredefined data type orpredefined data type orpredefined data type or Fundamental data typeFundamental data typeFundamental data typeFundamental data type are those data type that are defined
in c already.
Data TypeData TypeData TypeData Type Used forUsed forUsed forUsed for Size(in Byte)Size(in Byte)Size(in Byte)Size(in Byte) RangeRangeRangeRange FormatFormatFormatFormat
Int Integer 2 32768 to 32767 %d
8. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 8
Char Character 1 128 to 127 %c
Float Single precision floating point number 4 3.4e38 to +3.4e38 %f
Double double precision floating point number 8 1.7e308 to
+1.7e308
%lf
Void
Data Type ModifiersData Type ModifiersData Type ModifiersData Type Modifiers ––––There are four modifiers available.
Signed
Unsigned
Short
Long
Data TypeData TypeData TypeData Type Size(in Byte)Size(in Byte)Size(in Byte)Size(in Byte) RangeRangeRangeRange FormatFormatFormatFormat
signed char 1 128 to + 127 %c
unsigned char 1 0 to 255 %c
signed int 2 32768 to +32767 %d
unsigned int 2 0 to 65535 %u
short signed int 2 32768 to +32767 %d
short unsigned int 2 0 to 65535 %u
long double 10 1.7e4932 to +1.7e4932 %Lf
2.2.2.2. DerivedDerivedDerivedDerived data tydata tydata tydata typepepepe are those data type that are derived by predefined data type.
ExampleExampleExampleExample Array, pointer
3.3.3.3. User defined data typeUser defined data typeUser defined data typeUser defined data type are those data type that are defined by the user itself.
ExampleExampleExampleExample struct, union
QuestionQuestionQuestionQuestion 7777 Define operator? How many types of operators are available in c programming? Explain any four
of them with examples.
AnsweAnsweAnsweAnswerrrr
OperatorOperatorOperatorOperator
Operator is a symbol that operates on one or more operands. Operands can be a variable, a constant or an
expression.
When an operator operates on one operand, is called unary operatorunary operatorunary operatorunary operator.
When an operator operates on two operands, is called binarybinarybinarybinary operator.operator.operator.operator.
When an operator operates on three operands, is called ternary operatorternary operatorternary operatorternary operator.
Types of OperatorsTypes of OperatorsTypes of OperatorsTypes of Operators
1.1.1.1. Arithmetic operator
9. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 9
2.2.2.2. Relational operator
3.3.3.3. Logical operator
4.4.4.4. Ternary operator
5.5.5.5. Increment operator
6.6.6.6. Assignment operator
7.7.7.7. Bitwise operator
8.8.8.8. Sizeof() operator
1.1.1.1. AritAritAritArithmetic operatorhmetic operatorhmetic operatorhmetic operator are used to perform to arithmetic operation on two operands.
OperatorOperatorOperatorOperator MeaningMeaningMeaningMeaning PrecedencePrecedencePrecedencePrecedence AssociativityAssociativityAssociativityAssociativity
+ Addition 2 Left to Right
Subtraction 2 Left to Right
* Multiplication 1 Left to Right
/ Division 1 Left to Right
% Modulo division 1 Left to Right
2.2.2.2. Relational operatorRelational operatorRelational operatorRelational operator are used to compare two operands.
OperatorOperatorOperatorOperator MeaningMeaningMeaningMeaning PrecedencePrecedencePrecedencePrecedence AssociativityAssociativityAssociativityAssociativity
< Less Than 1 Left to Right
<= Less than or equal to 1 Left to Right
> Greater than 1 Left to Right
>= Greater than or equal to 1 Left to Right
== Equal to 2 Left to Right
!= Not equal to 2 Left to Right
3.3.3.3. Logical operatorLogical operatorLogical operatorLogical operator are used to logically relate the sub expression.
OperatorOperatorOperatorOperator MeaningMeaningMeaningMeaning PrecedencePrecedencePrecedencePrecedence AssociativityAssociativityAssociativityAssociativity
&& Logical And 2 Left to Right
|| Logical or 3 Left to Right
! Negation 1 Right to Left
Truth Table of Logical operatorTruth Table of Logical operatorTruth Table of Logical operatorTruth Table of Logical operator
OpOpOpOpnnnnd1d1d1d1 Opnd2Opnd2Opnd2Opnd2 Opnd1&&Opnd2Opnd1&&Opnd2Opnd1&&Opnd2Opnd1&&Opnd2 Opnd1||Opnd2Opnd1||Opnd2Opnd1||Opnd2Opnd1||Opnd2 !Opnd1!Opnd1!Opnd1!Opnd1
0000 0000 0000 0000 1111
0000 1111 0000 1111 1111
1111 0000 0000 1111 0000
10. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 10
1111 1111 1111 1111 0000
4.4.4.4. Ternary operatorTernary operatorTernary operatorTernary operator operates on three operands .It is also called conditional operatorconditional operatorconditional operatorconditional operator....
It comes with the pair “ ?“ ?“ ?“ ? : ”
General form is-
exp1? exp2:exp3
ExplanationExplanationExplanationExplanation
If expression1 is true then expression2 is executed otherwise expression3 is executed.
QuestionQuestionQuestionQuestion 8888 What are storage classes? Define all storage classes with examples.
AnswerAnswerAnswerAnswer
Storage classesStorage classesStorage classesStorage classes
All variables have a data type, also have a storage class. Storage class of a variable tell to the compiler
Where will be the variable is stored?
What will be the initial value of the variable?
What will be the scope of the variable?
What will be the life of the variable?
There are four storage classes in C:
1.1.1.1. Automatic storage classAutomatic storage classAutomatic storage classAutomatic storage class
The features of variables are as follows
StorageStorageStorageStorage Memory
Default initial valueDefault initial valueDefault initial valueDefault initial value Garbage value
ScopeScopeScopeScope Local to the block in which defined
LifeLifeLifeLife till the control remains within the block in which defined
ExamplExamplExamplExample:e:e:e:
void main()
{
auto int a=5;
{
auto int a=10;
printf(“a=%d”,a); // display a=10
}
printf(“a=%d”,a); // display a=5
}
2.2.2.2. Register storage classRegister storage classRegister storage classRegister storage class
11. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 11
The features of variables are as follows
StorageStorageStorageStorage CPU registers
Default initial valueDefault initial valueDefault initial valueDefault initial value Garbage value
ScoScoScoScopepepepe Local to the block in which defined
LifeLifeLifeLife till the control remains within the block in which defined
Example:Example:Example:Example:
void main()
{
register int a=5;
{
register int a=10;
printf(“a=%d”,a); // display a=10
}
printf(“a=%d”,a); // display a=5
}
The only difference between auto and register is, the execution of register variable is much faster than auto
variable.
3.3.3.3. Static storage classStatic storage classStatic storage classStatic storage class
The features of variables are as follows
StorageStorageStorageStorage Memory
Default initial valueDefault initial valueDefault initial valueDefault initial value Zero
ScopeScopeScopeScope Local to the block in which defined
LifeLifeLifeLife value of variable persists between different function calls.
ExampleExampleExampleExample
void main()
{
Increment();
Increment();
Increment();
}
void Increment()
{
static int a=1;
printf(“a=%d”,a);
a++;
12. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 12
}
OutputOutputOutputOutput
a=1
a=2
a=3
4.4.4.4. External storage classExternal storage classExternal storage classExternal storage class
The features of variables here are as follows
StorageStorageStorageStorage Memory
Default initial valueDefault initial valueDefault initial valueDefault initial value Zero
ScopeScopeScopeScope global
LifeLifeLifeLife As long as program execution does not come to an end.
ExampleExampleExampleExample
void main()
{
extern int a;
printf(“a=%d”,a); //display a=0;//display a=0;//display a=0;//display a=0;
}
int a;
QuestionQuestionQuestionQuestion 9999 Write a program and flowchart to print Fibonacci series up to n terms.
AnswerAnswerAnswerAnswer
The Fibonacci numbers are the numbers in the following sequence:
0,1,1,2,3,5,8,13,21,34,55,89,144
By definition, the first two Fibonacci numbers are 0 and 1, and each remaining number is the sum of the
previous two.
/* A program to p/* A program to p/* A program to p/* A program to print Fibonacci series up to n terms */rint Fibonacci series up to n terms */rint Fibonacci series up to n terms */rint Fibonacci series up to n terms */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b ,c,i,n;
printf(“n Enter the terms”);
scanf(“%d”,&n);
a=0;
13. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 13
b=1;
printf(“n%d %d”,a,b);
for(i=1;i<=n 2;i++)
{
c=a+b;
printf(“%d”,c);
a=b;
b=c;
}
getch();
}
Fibonacci Flowchart
The following flowchart illustrates how to generate Fibonacci Series for the first N numbers.
14. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 14
Question 10Question 10Question 10Question 10 Write a program to read two numbers from the keyboard and swap that numbers without
using third variable.
AnswerAnswerAnswerAnswer
/*A program to swap two values without the help of third variable*//*A program to swap two values without the help of third variable*//*A program to swap two values without the help of third variable*//*A program to swap two values without the help of third variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("n Enter the value of a and b >");
scanf("%d %d",&a,&b);
printf("n Before swapping >");
printf("a=%d and b=%d",a,b);
a=a+b;
b=a b;
a=a b;
printf("n After swapping >");
printf("a=%d and b=%d",a,b);
getch();
}
Question 11Question 11Question 11Question 11 Write a program and flowchart to print factorial of a number.
AnswerAnswerAnswerAnswer /*A program to find out factorial of given number*//*A program to find out factorial of given number*//*A program to find out factorial of given number*//*A program to find out factorial of given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact;
printf("n Enter the number >");
scanf("%d",&n);
fact=1;
for(i=1;i<=n;i++)
{
15. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 15
fact=fact*i;
}
printf("n The factorial is %d",fact);
getch();
}
/* A flow chart of factorial of N number */
QueQueQueQuestion 12stion 12stion 12stion 12 Write a program to print all prime number from 1 to n terms.
AnswerAnswerAnswerAnswer
/* A program to print all prime number from/* A program to print all prime number from/* A program to print all prime number from/* A program to print all prime number from 1 to n terms */1 to n terms */1 to n terms */1 to n terms */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,t;
printf(“n Enter the terms”);
scanf(“%d”,&t);
for(n=1;n<=t;n++)
{
for(i=2;i<n;i++)
{
16. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 16
if(n%i==0)
break;
}
if(n==i)
printf(“n %d is prime number”,n);
}
getch();
}
QuestionQuestionQuestionQuestion 11113333 Explain switch case statement with the help of example.
AnswerAnswerAnswerAnswer
Switch StatementSwitch StatementSwitch StatementSwitch Statement
The control statement that allows us to make a decision from the number of choices is called a switch, or more
correctly a switch case default statement.
General formGeneral formGeneral formGeneral form
switch ( integer expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
The integer expression following the keyword switch is any C expression that will yield an integer
value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer.
The keyword case is followed by an integer or a character constant.
Each constant in each case must be different from all the others.
Consider the following program:
void main( )
{
int i = 2 ;
17. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 17
switch ( i )
{
case 1 :
printf ( "I am in case 1 n" ) ;
case 2 :
printf ( "I am in case 2 n" ) ;
case 3 :
printf ( "I am in case 3 n" ) ;
default :
printf ( "I am in default n" ) ;
}
}
The output of this program would be:
I am in case 2
I am in case 3
I am in default
Question 14Question 14Question 14Question 14 Differentiate between global variable and local variable with the help of an example.
AnswerAnswerAnswerAnswer
GlGlGlGlobal and Local Variablesobal and Local Variablesobal and Local Variablesobal and Local Variables
Global variables:Global variables:Global variables:Global variables:
The features are as follows
Declared outside of all functions or before main
These can be used in all the functions in the program.
It need not be declared in other functions.
A global variable is also known as an external variable.
Local variables:Local variables:Local variables:Local variables:
The features are as follows
Declared inside a function where it is to be used.
These are not known to other function in the program.
These variables are visible and meaningful inside the functions in which they are declared.
Example:Example:Example:Example:
int a; //global variable
void main()
{
int b ; //local variable
18. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 18
a=5;
b=10;
printf(“a=%d b=%d”,a,b);
increment();
}
void increment()
{
a=a+1;
printf(“a=%d”,a);
}
Explanation:Explanation:Explanation:Explanation:
Since a variable is global variable therefore it can be accessed anywhere in the program. And b variable is
local variable therefore is cannot be accessed outside the main (), since it’s declared inside the main () and
cannot be accessed in the increment () function.
QuestionQuestionQuestionQuestion 11115555 Define variable declaration and variable initialization with the help of an example.
AnswerAnswerAnswerAnswer
Declaration of a variableDeclaration of a variableDeclaration of a variableDeclaration of a variable tells the compiler
Data Type of variable
Which type of value that variable contained?
And size of variable.
General form of declaration of variable
DataDataDataDataTTTType variaype variaype variaype variableblebleble_name_name_name_name;;;;
ExampleExampleExampleExample
int var;int var;int var;int var;
Here we declare a variable var of int data type that contains integer value and reserve 2 byte memory space.
IIIInitializationnitializationnitializationnitialization of variableof variableof variableof variable tells the compiler That we are assigning or initialize a value to that variable.
General form of Initialization of variable is
Variable_Name=Value;Variable_Name=Value;Variable_Name=Value;Variable_Name=Value;
ExampleExampleExampleExample
var=10;var=10;var=10;var=10;
Here we assign value 10 to variable var.
19. Kanpur Institute of Technology
Computer Concept and C Programming (Question Bank-1)
Somnath Gupta Page 19
Question 16Question 16Question 16Question 16 Read a number from the keyboard .Write a program to check that number is Armstrong or not.
AnswerAnswerAnswerAnswer
/*A program to check given numbe/*A program to check given numbe/*A program to check given numbe/*A program to check given number is Armstrong number or not */r is Armstrong number or not */r is Armstrong number or not */r is Armstrong number or not */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,,t,r,s=0;
printf("n Enter the n digit number >");
scanf("%d",&n);
t=n;
while(n!=0)
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
if(t==s)
printf("n The number %d is Armstrong number ",t);
else
printf("n The number %d is not Armstrong number",t);
getch();
}