0% found this document useful (0 votes)
7 views

C Program Internal-1 Question and Answer (2)

The document consists of a series of questions and answers related to programming in C, covering topics such as programming paradigms, data types, loops, decision-making statements, and operators. It includes definitions, distinctions between concepts, and example C programs for practical understanding. The document is structured into three parts, with Part A focusing on definitions and distinctions, Part B on programming constructs and examples, and Part C on operators and patterns.

Uploaded by

su ja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C Program Internal-1 Question and Answer (2)

The document consists of a series of questions and answers related to programming in C, covering topics such as programming paradigms, data types, loops, decision-making statements, and operators. It includes definitions, distinctions between concepts, and example C programs for practical understanding. The document is structured into three parts, with Part A focusing on definitions and distinctions, Part B on programming constructs and examples, and Part C on operators and patterns.

Uploaded by

su ja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Part-A(2*5=10)

1.Define Programming Paradigms.


2.Distinguish between character and string.
3.Invent the difference between ++a and a++.
4.Classify the different types of storage class.
5.Summarize the types of operators.
Part-B(13*2=26)
6 a) i) Describe the structure of aC program with an example.(9 marks)
ii) Write a C program to check the integer is palindrome or not.(4 marks)
b) Describe the decision making statement in C with an example.
7 a) Write short notes,syntax and necessary program on the following:
i) For loop
ii) While Loop
iii) Do while Loop
b i) Illustrate about the various data types and its program.(9 marks)
ii) Write a C program to check whether a given number is leap year or not.
Part-C (14*1=14)
8 a) What are the various types of operator in C and wrte down the necessary program for each
operator.
b)Write the C program for the following:
i) To Print Simple Pyramid Pattern like
1
22
333
4444
5 5 5 5 5 (7 marks)

ii) To generate the first number in a Fibonacci series. (7 marks)

Answers:
Part-A(2*5=10)
1.Define Programming Paradigms.
Programming paradigm is an approach to solve problem using some programming language or also
we can say it is a method to solve a problem using tools and techniques that are available to us
following some approach.
2.Distinguish between character and string.
3.Invent the difference between Pre-increment operation and Post increment operation.
Pre increment operation Post increment Operation
A pre-increment operator is used to increment A post-increment operator is used to increment
the value of a variable before using it in an the value of the variable after executing the
expression expression completely in which post-increment
is used
.In the Pre-Increment, value is first incremented In the Post-Increment, value is first used in an
and then used inside the expression. expression and then incremented.

Syntax: Syntax:

a = ++x; a = x++;

4.Classify the different types of storage class.


C Storage Classes are used to describe the features of a variable/function
Classification:
i) Auto
ii) Extern
iii) Static
iv) Register
5.Summarize the types of operators.
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Comma Operator
 Unary Operators
 Binary Operators
 Ternary Operators
 Sizeof Operator

Part-B

6 a) i) Describe the structure of a C program with an example.(9 marks)


The basic structure of a C program is divided into 6 parts which makes it easy to read, modify,
document, and understand in a particular format. C program must follow the below-mentioned
outline in order to successfully compile and execute. Debugging is easier in a well-structured C
program.
Sections of the C Program
There are 6 basic sections responsible for the proper execution of a program. Sections are
mentioned below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs

1. Documentation

This section consists of the description of the program, the name of the program, and the creation
date and time of the program. It is specified at the start of the program in the form of comments.
Documentation can be represented as:
// description, name of the program, programmer name, date, time etc.

2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program.
Header files help us to access other’s improved code into our code. A copy of these multiple files is
inserted into our program before the process of compilation.
Example:
#include<stdio.h>
#include<math.h>

3. Definition

Preprocessors are the programs that process our source code before the process of compilation.
There are multiple steps which are involved in the writing and execution of the program.
Preprocessor directives start with the ‘#’ symbol. The #define preprocessor is used to create a
constant throughout the program. Whenever this name is encountered by the compiler, it is replaced
by the actual piece of defined code.
Example:
#define long longll

4. Global Declaration

The global declaration section contains global variables, function declaration, and static variables.
Variables and functions which are declared in this scope can be used anywhere in the program.
Example:
int num = 18;

5. Main() Function

Every C program must have a main function. The main() function of the program is written in this
section. Operations like declaration and execution are performed inside the curly braces of the main
program. The return type of the main() function can be int as well as void too. void() main tells the
compiler that the program will not return any value. The int main() tells the compiler that the
program will return an integer value.
Example:
void main()
or
int main()

6. Sub Programs

User-defined functions are called in this section of the program. The control of the program is
shifted to the called function whenever they are called from the main or outside the main() function.
These are specified as per the requirements of the programmer.
Example:
int sum(int x, int y)
{
return x+y;
}
Example:
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a,b,sum;
Clrscr();
Printf(“Enter the first value:”);
Scanf(“%d”, &a);
Printf(“Enter the second value:”);
Scanf(“%d”, &b);
Sum=a+b;
Printf(“The sum of given two numbers %d”,sum);
getch();
}
ii) Write a C program to check the integer is palindrome or not.(4 marks)
Definition:
Palindrome numbers are those numbers which after reversing the digits equals the original number.
Example:
#include <stdio.h>
intmain() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return0;
}
Output:

Enter an integer: 1001


1001 is a palindrome.
b) Describe the decision making statement in C with an example.
Definition:
They are also known as Control Statements and are used to evaluate one or more conditions and
make the decision whether to execute a set of statements or not.
1. if in C
The if statement is the most simple decision-making statement. It is used to decide whether a
certain statement or block of statements will be executed or not i.e if a certain condition is true then
a block of statements is executed otherwise not.
Syntax of if Statement
if(condition)
{
// Statements
}

C program
#include <stdio.h>
int main()
{
int i = 10;
if (i< 15) {
printf("15 is greater than 10");
}
}
Output
15 is greater than 10
2. if-else in C
The if statement alone tells us that if a condition is true it will execute a block of statements and if
the condition is false it won’t.
Syntax of if else in C
if (condition)
{
// Statement
}
else
{
// Statement
}

C program
#include <stdio.h>
int main()
{
int i = 20;
if (i< 15) {
printf("i is smaller than 15");
}
else {
printf("i is greater than 15");
}
return 0;
}
Output
i is greater than 15

3. Nested if-else in C
A nested if in C is an if statement that is the target of another if statement. Nested if statements
mean an if statement inside another if statement.
Syntax of Nested if-else
if (condition1)
{
// Statement
if (condition2)
{
// Statement
}
else
{
// Statement
}

// C program to illustrate nested-if statement

#include <stdio.h>

int main()

int i = 10;

if (i == 10) {

if (i< 15)

printf("i is smaller than 15\n");

if (i< 12)

printf("i is smaller than 12 too\n");

else

printf("i is greater than 15");

}
return 0;
}

Output
i is smaller than 15
i is smaller than 12 too

switch Statement in C
The switch case statement is an alternative to the if else if ladder that can be used to execute the
conditional code based on the value of the variable specified in the switch statement.
Syntax of switch
switch (expression) {
casevalue1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}

C Program

#include <stdio.h>
int main()

int var = 2;

switch (var) {

case 1:

printf("Case 1 is executed");

break;

case 2:

printf("Case 2 is executed");

break;

default:

printf("Default Case is executed");

break;

return 0;
}

Output
Case 2 is executed
Default case is Executed

7 a) Write short notes,syntax and necessary program on the following:


i) For loop
ii) While Loop
iii) Do while Loop
Looping Statement
Loops in programming are used to repeat a block of code until the specified condition is met. A
loop statement allows programmers to execute a statement or group of statements multiple times
without repetition of code.
For loop:
For loop in C programming is a repetition control structure that allows programmers to write a loop
that will be executed a specific number of times. for loop enables programmers to perform n
number of steps together in a single line.
Syntax:
for (initialize expression; Condition;Increment/Decrement)
{
//
// body of for loop
//
}
Example:
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i<= 5; i++)
{
printf( "Hello World\n");
}
return 0;
}
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
While Loop:
While loop does not depend upon the number of iterations. In for loop the number of iterations was
previously known to us but in the While loop, the execution is terminated on the basis of the test
condition.
Syntax:
while (Condition)
{
// body of the while loop
}
program
#include <stdio.h>
int main()
{
int i = 0;
while(i<5)
{
printf( "Hello World\n");
i++;
}
return 0;
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
do-while Loop
The do-while loop is similar to a while loop but the only difference lies in the do-while loop test
condition which is tested at the end of the body.
Syntax
do
{
// body of do-while loop
} while (Condition);
Program:
#include <stdio.h>
int main()
{
int i = 2;

do
{
printf( "Hello World\n");
i++;
} while (i< 1);
return 0;
}
Output
Hello World

b i) Illustrate about the various data types and its program.(9 marks)
Definition:
Data type is an attribute associated with a piece of data that tells a computer system how to interpret
its value. Understanding data types ensures that data is collected in the preferred format and that the
value of each property is as expected.
Types:
 Integer
 Character
 Float
 Double
 Size of data type
Integer Data Type
The integer datatype in C is used to store the integer numbers(any number including positive,
negative and zero without decimal part). Octal values, hexadecimal values, and decimal values can
be stored in int data type in C.
 Range: -2,147,483,648 to 2,147,483,647
 Size: 4 bytes
 Format Specifier: %d
Syntax of Integer

intvar_name;
Program:
#include <stdio.h>
int main()
{
int a = 9;
int b = -9;
printf("Integer value with positive data: %d\n", a);
printf("Integer value with negative data: %d\n", b);
return 0;
}
Output:
Integer value with positive data: 9
Integer value with negative data: -9
Character Data Type
Character data type allows its variable to store only a single character. The size of the character is 1
byte. It is the most basic data type in C. It stores a single character and requires a single byte of
memory in almost all compilers.
 Range: (-128 to 127) or (0 to 255)
 Size: 1 byte
 Format Specifier: %c

Syntax of char
charvar_name;

Program:
#include <stdio.h>
int main()
{
char a = 'a';
char c;
printf("Value of a: %c\n", a);
a++;
printf("Value of a after increment is: %c\n", a);
c = 99;
printf("Value of c: %c", c);
return 0;
}
Output
Value of a: a
Value of a after increment is: b
Value of c: c

Float Data Type


In C programming float data type is used to store floating-point values. Float in C is used to store
decimal and exponential values. It is used to store decimal numbers (numbers with floating point
values) with single precision.
 Range: 1.2E-38 to 3.4E+38
 Size: 4 bytes
 Format Specifier: %f
Syntax of float

floatvar_name;
Program:
#include <stdio.h>
int main()
{
float a = 9.0f;
float b = 2.5f;
printf("%f\n", a);
printf("%f\n", b);
return 0;
}
Output
9.000000
2.500000

Double Data Type


A Double data type in C is used to store decimal numbers (numbers with floating point values) with
double precision. It is used to define numeric values which hold numbers with decimal values in C.

Syntax of Double
The variable can be declared as double precision floating point using the double keyword:

doublevar_name;

Program:

#include <stdio.h>

int main()

double a = 123123123.00;

double b = 12.293123;

double c = 2312312312.123123;

printf("%lf\n", a);

printf("%lf\n", b);

printf("%lf", c);

return 0;
}

Output:
123123123.000000
12.293123
2312312312.123123
Size of Data Types in C

The size of the data types in C is dependent on the size of the architecture, so we cannot define the
universal size of the data types. For that, the C language provides the sizeof() operator to check the
size of the data types.
Program:

#include <stdio.h>

int main()

int size_of_int = sizeof(int);

int size_of_char = sizeof(char);

int size_of_float = sizeof(float);

int size_of_double = sizeof(double);

printf("The size of int data type : %d\n", size_of_int);

printf("The size of char data type : %d\n",

size_of_char);

printf("The size of float data type : %d\n",

size_of_float);

printf("The size of double data type : %d",

size_of_double);

return 0;
}

Output
The size of int data type : 4
The size of char data type : 1
The size of float data type : 4
The size of double data type : 8

ii) Write a C program to check whether a given number is leap year or not.
A leap year is a year that contains an additional day in February month i.e. February 29. It means
that a leap year has 366 days instead of the usual 365 days. In this article, we will see the program
to check for leap year in C.
#include <stdio.h>
intmain() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
elseif (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
elseif (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return0;
}
Output:

Enter a year: 1900


1900 is not a leap year.
Enter a year: 2024
2024 is a leap year.

Part-C (14*1=14)
8 a) What are the various types of operator in C and wrte down the necessary program for each
operator.
Definition:
Operators are symbols that represent operations to be performed on one or more operands. They are
the basic components of the C programming
(study your notes)
b)Write the C program for the following:
i) To Print Simple Pyramid Pattern like
1
22
333
4444
5 5 5 5 5 (7 marks)

Answer:
Definition:
Pyramid pattern printing is a logical program by which we can develop logical thinking in
programming. We can use conditional statements and loop concepts for making pyramid patterns.
Program:
#include <stdio.h>
int main()
{
int rows;
printf("Number of rows: ");
scanf("%d", &rows);

for (int i = 1; i<= rows; i++) {


for (int j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n");
}
return 0;
}
Output :
Number of rows: 5
1
22
333
4444
55555

ii) To generate the first number in a Fibonacci series. (7 marks)


Definition:
The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The
first two terms of the Fibonacci sequence are 0 followed by 1.
The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21

#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;

printf("Enter the number of terms: ");


scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i<= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Ouput

Enter the number of terms: 10


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

You might also like