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

C PRO ANS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C PRO ANS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

I.

FILL IN THE BLANKS:

1. The C language was originally developed from------------language.


B
2. C language is well suited for------------programming.
structure

3. C program execution begins from------------.


main ( )
4. The operator “- -’’ is known as ------------ operator.
decrement
5. The ------------ is equivalent to a = a-1.
--a
6. Consider the following statement:
a=10;
b=++a;
then the value of b is ------------
-11
7. The ------------ statement causes the next iteration of the loop structure.
continue
8. ----------------function can be used to read a single character.
getch ( )

9. The -----------operator can be used to determine the length of array and structures.
sizeof

10. The operator “++’’ adds the value ------------ to the operand.
1

II.

1.
 Algorithms are step-by-step procedures designed to solve specific problems and
perform tasks efficiently in the realm of computer science and mathematics.
 A flowchart is a type of diagram that represents a workflow or process.
2.
Top-down design means to repeatedly divide a problem into (smaller) sub-problems,
until they are directly solvable. A top-down design process can be represented by a
structure chart.
3.

 Keywords are the predefined words in a programming language.


 It only contains alphabetical characters.
 Identifiers are the names given to variables, functions, lists, etc.
 It contains only numerals, letters and underscores. No special symbols are allowed.
4.
Entry controlled loop is a loop in which the test condition is checked first, and then the loop
body will be executed. If the test condition is false, the loop body will not be executed, For
Loop and While Loop is the example of Entry controlled loop. for (i = 0; i < 10 ; ++i)
//Statements.

5.
They are used for inputting the data of the arithmetic unit (ALU), outputting the calculation
result, transferring the operation of the data stored in memory, etc.

6.
A Structure is a data structure that can contain variables of different data types. An
Array is a data structure that can only contain variables of the same data type.

7.

Precedence level Associativity Operators

Primary left to right () [ ] . –>

Unary right to left ++ -- - + ! ~ & * (typename) sizeof

Multiplicative left to right *⁄%

Additive left to right +-

8.
Type variableName = value; Where type is one of C types (such as int ), and
variableName is the name of the variable (such as x or myName). The equal sign is used
to assign a value to the variable.

9.
 printf function is used to print character stream of data on stdout
console.
Syntax:
printf(const char* str, ...);
 fprintf is used to print the string content in file but not on the stdout
console.
Syntax: fprintf(FILE *fptr, const char
*str, ...);
10.
Function fopen() is used both to open or create a file, and fclose() is used to close an
already opened file. File 'read' and 'write' operations can be performed after opening a file.
The fputc() and fputs() functions are used to write characters and strings respectively in a file.

III.

1.
 A function that calls itself is known as a recursive function. And, this technique
is known as recursion.
 The recursion continues until some condition is met to prevent it.
 To prevent infinite recursion, if...else statement (or similar approach) can be
used where one branch makes the recursive call, and other doesn't.

#include <stdio.h>
int factorial(int n);
int main() {
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);

if (num < 0) {
printf("Error: Factorial of a negative number is undefined.\n");
} else {
int result = factorial(num);
printf("Factorial of %d = %d\n", num, result);
}

return 0;
}

// Recursive function to calculate factorial


int factorial(int n) {
// Base case: factorial of 0 is 1
if (n == 0) {
return 1;
} else {
// Recursive case: n! = n * (n-1)!
return n * factorial(n - 1);
}
}
2.

In if- else-if ladder statement, if a condition is true then the statements defined in the if
block will be executed, otherwise if some other condition is true then the statements
defined in the else-if block will be executed, at the last if none of the condition is true then
the statements defined in the else block .

3. Basic Types:
 int: Used to store integers (whole numbers), such as -5, 0, 42.
 char: Used to store single characters, such as 'a', '5', or '$'.
 float: Used to store single-precision floating-point numbers, such as 3.14,
-0.001, 7.0.
 double: Used to store double-precision floating-point numbers, with more
precision than float. For example, 3.14159.

4.
#include <stdio.h>
#include <string.h>

void sortString(char *str);

int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

// Remove the newline character at the end of the input


str[strcspn(str, "\n")] = 0;

sortString(str);

printf("String in alphabetical order: %s\n", str);

return 0;
}

void sortString(char *str) {


int length = strlen(str);
char temp;

// Bubble sort to sort characters


for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - i - 1; j++) {
if (str[j] > str[j + 1]) {
// Swap characters
temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
}

5.

1. strlen() - String Length

This function is used to find the length of a string, i.e., the number of characters in the string
excluding the null terminator '\0'.

Syntax - size_t strlen(const char *str);

2. strcpy() - String Copy

This function is used to copy the contents of one string into another. It copies the string along
with the null terminator '\0'.

Syntax:

char *strcpy(char *destination, const char *source);

3. strcmp() - String Compare

This function is used to compare two strings lexicographically. It returns an integer less than,
equal to, or greater than zero if the first string is found, respectively, to be less than, to match,
or be greater than the second string.

Syntax:

int strcmp(const char *str1, const char *str2);

IV.
1)
a) 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; test expression; update
expression)
{
//
// body of for loop
//
}

b) 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. In the do-
while loop, the loop body will execute at least once irrespective of the test
condition.
Syntax:
initialization_expression;
do
{
// body of do-while loop

update_expression;

} while (test_expression);

c) 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. If the test condition
will become false then it will break from the while loop else body will be
executed.

Syntax:
initialization_expression;
while (test_expression)
{
// body of the while loop

update_expression;
}

4)

Program

#include <stdio.h>

#include <string.h>

#define MAX_SIZE 100

int main() {

char input[MAX_SIZE];

int words = 0, characters = 0, i;

printf("Enter a line of text: ");

fgets(input, MAX_SIZE, stdin);

// Counting words

for (i = 0; input[i] != '\0'; i++) {

if (input[i] == ' ' || input[i] == '\n' || input[i] == '\t') {

words++;

// Adding one for the last word

words++;

// Counting characters

characters = strlen(input);
printf("Number of words: %d\n", words);

printf("Number of characters: %d\n", characters);

return 0;

5) Program

#include <stdio.h>
int isPrime(int num) {
if (num <= 1) {
return 0; // 0 and 1 are not prime numbers
}

for (int i = 2; i <= num / 2; i++) {


if (num % i == 0) {
return 0; // Not a prime number
}
}
return 1; // Prime number
}
int main() {
int lower = 50, upper = 500;
printf("Prime numbers between %d and %d are:\n", lower, upper);
for (int i = lower; i <= upper; i++) {
if (isPrime(i)) {
printf("%d\n", i);
}
}

return 0;
}

You might also like