C Important Question With Answer

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

PART-A

1. Draw the structure of C program


There are 6 basic sections responsible for the proper execution of a program.
Sections are mentioned below:
 Documentation
 Pre-processor Section
 Definition
 Global Declaration
 Main() Function
 Sub Programs

2. What is the purpose of format specifier in I/O Statements? Give an example.


 Format specifiers are used to control how data is displayed, making it more
readable and visually appealing.
 These I/O functions are versatile and support a wide range of data types,
including integers, floating-point numbers, characters, and more.

3. Write short notes on keywords in C language.


Keywords are predefined or reserved words that have special meanings to the
compiler. These are part of the syntax and cannot be used as identifiers in the
program. A list of keywords in C or reserved words in the C programming language
are mentioned below

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

4. Write down the syntax and give an example for array initialization.
data_type array_name[size];
Or
Data_type array_name[size 1] [size 2] [size n] ;

Eg:
#include <stdio.h>
int main()
{
// declaring array of integers
int arr_int[5];
// declaring array of characters
char arr_char[5];
return 0;
}

5. Define Array.
An array in C is a fixed-size collection of similar data items stored in contiguous memory
locations. It can be used to store the collection of primitive data types such as int, char,
float, etc., and also derived and user-defined data types such as pointers, structures, etc.
6. What is the use of preprocessor – directive?
Preprocessor directives, such as #define and #ifdef , are typically used to make source
programs easy to change and easy to compile in different execution environments.
Directives in the source file tell the preprocessor to take specific actions .
7. Keywords cannot be used as identifiers-Justify the correctness of statement.
Keywords cannot be used as identifiers for other purposes (e.g. variable/function/method
naming). Keywords are reserved words so cannot be used as identifier, but in case
sensitive languages, in which keywords are generally in lower case, so you can use
uppercase name of keyword as an identifier.
8. List out Various Input & Output Statements in C.
Basic Input And Output In C
 scanf() ...
 printf() ...
 Integer Input: scanf("%d", &intVariable); Output: printf("%d", intVariable);
 Float Input: scanf("%f", &floatVariable); Output: printf("%f", floatVariable);

9. Declare a float array of size 6 and assign 5 values to it.


#include<stdio.h>
int main()
{
Float array[5] – { 1.2 , 3.4 , 5.6, 7.8, 9.0};
For (i=0; i<5;i++)
{
Printf(“%f”,arr[i]);
}

10. How a character array is declared?


A character array can be declared in the following way:charArray = new char[10]; This
creates an array with a length of 10, which means it can store 10 characters.

PART –B

1. Explain about the various looping statement available in with appropriate sample
Programs.

 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.

There are mainly two types of loops in C Programming:

1. Entry Controlled loops: In Entry controlled loops the test condition is checked
before entering the main body of the loop. For Loop and While Loop is Entry-controlled
loops.
2. Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the
end of the loop body. The loop body will execute at least once, irrespective of whether
the condition is true or false. do-while Loop is Exit Controlled loop.
Note : Write the Example Program for all the Looping

2. Explain the decision making statement in c with example programs.

 Decision-Making Statements and are used to evaluate one or more conditions and
make the decision whether to execute a set of statements or not.
 These decision-making statements in programming languages decide the direction
of the flow of program execution.

Note : Explain all the statement and write a simple example programs
3. Evaluate the following expressions using operator precedence rule. Explain the order of
execution of operators.
(8*3-4) + (5%5? 3 : –9) + 5 – 20 / 10
a * b + (c / d) – 2 A 2 – (e + 4) where a = 2, b = 3, c = 8, d = 2, e = 5.
4. What is an array? Explain about One dimensional array with a sample program.

An array in C is a fixed-size collection of similar data items stored in contiguous memory


locations. It can be used to store the collection of primitive data types such as int, char,
float, etc., and also derived and user-defined data types such as pointers, structures, etc.

A 1-dimensional array is a linear data structure in the C programming language, consisting


of a fixed number of elements of the same data type, stored in contiguous memory
locations. It can be visualised as a sequence of values, indexed by a single integer. This
data structure allows for efficient access to individual elements and simple iteration
through its elements.

Example Program:

#include < stdio.h >

int main() {
//declaring and initializing one-dimensional array in C
int arr[3] = {10, 20, 30};

// After declaration, we can also initialize the array as:


// arr[0] = 10; arr[1] = 20; arr[2] = 30;

for (int i = 0; i < 3; i++) {


// accessing elements of array
printf(" Value of arr[%d]: %d\n", i, arr[i]);
}
}

OutPut:

Value of arr[0]: 10
Value of arr[1]: 20
Value of arr[2]: 30
5. Write a C program to read N integers into an array A and to find the Output the results
computed with appropriate headings
(i)sum of odd numbers,
#include<stdio.h>
Void main()
{
int a[10],i,sum=0;
printf("Enter upto 5 Values: ");
for(i=0; i<5; i++)
scanf("%d",&a[i]);
for(i=0; i<5; i++)
{
if(a[i]%2==1)
sum=sum+a[i];
}
printf("Sum of Odd values is: %d ",sum);
}
Output:
Enter upto 5 Values: 2 3 5 4 7
Total Sum of Odd values is: 15

(ii) sum of even numbers,


#include<stdio.h>
main()
{
int a[10],i,sum=0;
printf("Enter upto 5 Values: ");
for(i=0; i<5; i++)
scanf("%d",&a[i]);
for(i=0; i<5; i++)
{
if(a[i]%2==0)
sum=sum+a[i];
}
printf("Sum of Even values is: %d ",sum);
}
Output:
Enter upto 5 Values: 2 3 5 4 7
Total Sum of Even values is: 6

(iii) average of all numbers.


#include <stdio.h>
double average(int a[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
return (double)sum / n;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
int avg = average(arr, n);
printf("Average = %d ", avg);
return 0;
}
OUTPUT:
Average = 3

6. Write a C program to perform matrix addition using 2d array.

#include <stdio.h>
int main ()
{
int mat1[3][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8} };
int mat2[3][3] = { {9, 10, 11}, {12, 13, 14}, {15, 16, 17} };
int sum[3][3], i, j;

printf ("matrix 1 is :\n");


for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", mat1[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}

printf ("matrix 2 is :\n");


for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", mat2[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
// adding two matrices
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
}

// printing the sum 0f two matrices


printf ("\nSum of two matrices: \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", sum[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}

return 0;
}

OutPut:

matrix 1 is :
0 1 2

3 4 5

6 7 8

matrix 2 is :
9 10 11

12 13 14

15 16 17

Sum of two matrices:


9 11 13

15 17 19

21 23 25

PART -C
1. Write a C program to demonstrate the working concept of operators.

 An operator in C can be defined as the symbol that helps us to perform some specific
mathematical, relational, bitwise, conditional, or logical computations on values and
variables.
 The values and variables used with operators are called operands. So we can say that the
operators are the symbols that perform operations on operands.

Types of Operators in C

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
Note: Explain all the Operator concepts and simple example program
2. What is string? Write a C program that reads a sentence and prints the frequency of
each of the vowels and total count of consonants.

#include<stdio.h>
void main()
{
int i,cons=0,vow=0;
char a[100];
printf("Enter the sentence\n");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='a'||a[i]=='A')
vow=vow+1;
else if(a[i]=='e'||a[i]=='E')
vow=vow+1;
else if(a[i]=='i'||a[i]=='I')
vow=vow+1;
else if(a[i]=='o'||a[i]=='O')
vow=vow+1;
else if(a[i]=='u'||a[i]=='U')
vow=vow+1;
else
cons=cons+1;
}
printf("vowel count= %d\n",vow);
printf("consonants count = %d\n",cons);
}
Output :
Enter the sentence
Hello
vowels count = 2
consonants count = 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy