[go: up one dir, main page]

0% found this document useful (0 votes)
6 views7 pages

C Programming

The document provides definitions and explanations of algorithms, flowcharts, C programming concepts, data types, input/output statements, tokens, type casting, operators, and control structures like switch statements. It includes specific algorithms and C code examples for tasks such as checking for prime numbers, swapping numbers, counting passed students, and performing arithmetic operations. Additionally, it outlines various constants and symbols used in C programming.

Uploaded by

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

C Programming

The document provides definitions and explanations of algorithms, flowcharts, C programming concepts, data types, input/output statements, tokens, type casting, operators, and control structures like switch statements. It includes specific algorithms and C code examples for tasks such as checking for prime numbers, swapping numbers, counting passed students, and performing arithmetic operations. Additionally, it outlines various constants and symbols used in C programming.

Uploaded by

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

Question Bank Answers :-

1. Define an algorithm. Write an algorithm to find whether a number is prime or not.

Answer :

Definition: An algorithm is a finite set of instructions to solve a specific problem.

Algorithm to check prime:

Step 1: Start
Step 2: Read number n
Step 3: If n <= 1, then print "Not Prime" and stop
Step 4: Set i = 2
Step 5: Repeat while i <= n/2
If n % i == 0
Print "Not Prime"
Stop
i=i+1
Step 6: Print "Prime"
Step 7: Stop

---

2. Define a flowchart. Explain the commonly used flowchart symbols along with their
meanings and example.

Answer :

Definition: A flowchart is a diagram that represents a process or algorithm using symbols.

Common Symbols:
Oval - Terminator - Represents the start or end of a flowchart - Start, End
Diamond - Decision - Represents a decision with Yes/No paths. - Is number > 0?
Rectangle - Process - Indicates a processing step - sum = a + b
Parallelogram - Input/Output - Used to take input or display output​ - Input , Output
Arrow​ Flowline - Shows the direction of flow between steps​ - Connects all symbols

---
3. Develop a C program to swap two numbers using XOR (^) bitwise operator without
using a temporary variable.

Answer :

#include <stdio.h>
int main() {
int a = 5, b = 10;
printf("Before swap: a=%d, b=%d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("After swap: a=%d, b=%d\n", a, b);
return 0;
}

---

4. Classify and describe the various data types available in c language with an example

Answer :

Primary Data Types: int, float, char, double


Derived Types: Arrays, Pointers
User-defined: struct, union, enum
Void: Indicates no value

Example:

int a = 5;
float pi = 3.14;
char ch = 'A';

(OR)

Primary Data Types


int – for integers → int a = 10;
float – for decimal values → float pi = 3.14;
double – for large decimal values → double g = 9.81;
char – for single characters → char ch = 'A';

Derived Data Types


Array – int marks[5];
Pointer – int *ptr;
Function – int sum(int a, int b);

User-Defined Data Types


struct – Combines different data types → struct student { int id; char name[20]; };
union – Like struct but shares memory
enum – For named integer constants
typedef – To rename data types

Void Type
void – Means no value or return → void display();

---

5. Describe different Input and Output statements in C:

Answer :

Input: scanf()
Output: printf()

Example:

int a;
scanf("%d", &a);
printf("Value: %d", a);

---

6. List types of C tokens and explain Various Constants that are available in c

Answer :

Tokens in C:

1. Keywords
2. Identifiers
3. Constants
4. Operators
5. Special Symbols

Constants in C:
1.Integer constants: 10, -2
2.Float constants: 3.14
3.Character constants: 'a'
4.String constants: "hello"

---

7. Define Type Casting. Briefly explain the types of typecasting in c with examples.

Answer :

Definition: Converting one data type into another.

(OR)

Definition:

Type casting in C is the process of converting one data type into another. It helps in performing
operations between different types of variables.

Types of Type Casting in C:

Implicit Type Casting (Automatic)


1.Performed by the compiler.
2.Converts a smaller data type to a larger data type to prevent data loss.

Example:
int a = 5;
float b = a; // int automatically converted to float

Explicit Type Casting (Manual)


1.Performed by the programmer using the cast operator.
2.Syntax: (type) expression

Example:
float a = 10.75;
int b = (int) a; // float manually converted to int (decimal part lost)
printf("b = %d", b); // Output: 10

---

8. Explain different types operators in c with example

Answer :
Arithmetic: performs basic arithmetic operations
+, -, *, /, %
Relational: compares values , returns true or false
==, !=, <, >, <=, >=
Logical: combines multiple conditions
&&, ||, !
Assignment: assign value to a variable
=, +=, -=
Bitwise: works on bits
&, |, ^, ~, <<, >>

Example:
int a = 5, b = 3;
printf("%d", a + b); // Output: 8

---

9. Given a set of n students examination marks (0-100). Design an algorithm and draw
flowchart to make a count of number of students that passed the examination. (hint: pass
mark>-40).

Answer :

Algorithm:

Step 1: Start
Step 2: Read total number of students (n)
Step 3: Initialize count = 0
Step 4: Repeat steps 5–7 for i = 1 to n
Step 5: Read marks
Step 6: If marks > 40 then
Increment count by 1
Step 7: End loop
Step 8: Print count (number of students who passed)
Step 9: Stop

Flowchart:

Start (oval)

input n (parallelogram)

initialize count = 0 (rectangle)

→loop from i=1 to n (decision box or process)

input mark (parallelogram)

is mark › 40 ? (desicion)

If Yes: count = count + 1 (Rectangle)

After loop ends → Print count (Parallelogram)

End (Oval)

---

10. Explain the syntax of switch statement, Write a C program to perform


addition,subtraction, multiplication and division by using switch statement

Answer :

#include <stdio.h>

int main() {
int a, b, choice;
float result;

printf("Enter two numbers: ");


scanf("%d%d", &a, &b);

printf("Choose operation:\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);

switch(choice) {
case 1:
result = a + b;
printf("Sum = %.2f\n", result);
break;
case 2:
result = a - b;
printf("Difference = %.2f\n", result);
break;
case 3:
result = a * b;
printf("Product = %.2f\n", result);
break;
case 4:
if (b != 0)
result = (float)a / b;
else
printf("Error: Division by zero\n");
printf("Quotient = %.2f\n", result);
break;
default:
printf("Invalid choice\n");
}

return 0;
}

---

You might also like