C Program Internal-1 Question and Answer (2)
C Program Internal-1 Question and Answer (2)
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++;
Part-B
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:
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
}
#include <stdio.h>
int main()
int i = 10;
if (i == 10) {
if (i< 15)
if (i< 12)
else
}
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:
break;
return 0;
}
Output
Case 2 is executed
Default case is Executed
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
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
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()
size_of_char);
size_of_float);
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);
return0;
}
Output:
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);
#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;