[go: up one dir, main page]

0% found this document useful (0 votes)
11 views9 pages

Long Type Questions

The document provides an overview of the structure of C programming, including components like preprocessor directives, the main function, variable declarations, and function definitions. It explains various operators, tokens, control statements, and provides examples of C programs for tasks such as calculating the sum of digits, finding the maximum of three numbers, and printing prime numbers. Additionally, it discusses the differences between control flow statements like break and continue, and includes programs for calculating GCD and LCM, as well as printing specific patterns.
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)
11 views9 pages

Long Type Questions

The document provides an overview of the structure of C programming, including components like preprocessor directives, the main function, variable declarations, and function definitions. It explains various operators, tokens, control statements, and provides examples of C programs for tasks such as calculating the sum of digits, finding the maximum of three numbers, and printing prime numbers. Additionally, it discusses the differences between control flow statements like break and continue, and includes programs for calculating GCD and LCM, as well as printing specific patterns.
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/ 9

1. Explain in brief structure of C programming?

The structure of a C program typically consists of the following components:



• Preprocessor Directives: These are lines that begin with #, such as #include , which
include standard libraries necessary for input/output operations.
• Main Function: Every C program must have a main() function, which is the entry
point of the program. It is defined as int main() { /* code */ return 0; }.
• Variable Declarations: Variables must be declared before they are used, specifying
their data types (e.g., int, float).
• Function Definitions: Functions can be defined to perform specific tasks. They can
take parameters and return values.
• Statements and Expressions: The body of the program contains statements that
execute actions, including control structures like loops and conditionals.

A simple structure looks like this:


#include

int main() {
// Variable declarations
int num;

// Code execution
printf("Enter a number: ");
scanf("%d", &num);

return 0;
}

2. What is an operator? Enlist all operators used in C.


An operator in C is a symbol that tells the compiler to perform specific mathematical or
logical manipulations on operands. Operators are categorized into several types:

• Arithmetic Operators: +, -, *, /, %
• Relational Operators: ==, !=, <, >, <=, >=
• Logical Operators: && (AND), || (OR), ! (NOT)
• Bitwise Operators: &, |, ^, ~, <<, >>
• Assignment Operators: ‘=’, ‘+=’, ‘-=’, ‘*=’, ‘/=’, ‘%=’
• Increment/Decrement Operators: ‘++’, ‘–‘
• Conditional (Ternary) Operator: ‘? :’
• Sizeof Operator: ‘sizeof’
• Comma Operator: ‘,’
• Pointer Operator: ‘*’, ‘&’
1
• 3. What is a token? Explain the different types of tokens in C?
A token in C is the smallest element in a program that is meaningful to the compiler.
Tokens are classified into several types:
1. Keywords: Reserved words with special meaning (e.g., int, return, if).
2. Identifiers: Names given to entities like variables, functions, arrays, etc.
3. Constants: Fixed values that do not change during program execution (e.g., numeric
constants like 10 or character constants like ‘a’).
4. Strings: A sequence of characters enclosed in double quotes (e.g., “Hello”).
5. Operators: Symbols that specify operations to be performed on operands.

4. Explain switch statement with its syntax and example?


The switch statement allows multi-way branching based on the value of an expression. It
evaluates an expression and executes code corresponding to matching case labels.
Syntax:
switch(expression) {
case constant1:
// code block
break;
case constant2:
// code block
break;
default:
// default code block
}
Example:
#include

int main() {
int day = 3;

switch(day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
2
}

return 0;
}

5. Write a C program to find the sum of digits in a given number?


#include

int main() {
int num, sum = 0, digit;

printf("Enter a number: ");


scanf("%d", &num);

while(num > 0) {
digit = num % 10; // Get last digit
sum += digit; // Add it to sum
num /= 10; // Remove last digit
}

printf("Sum of digits: %d\n", sum);

return 0;
}

6. Write a C program to find the maximum of three numbers using ternary operator?
#include

int main() {
int a, b, c;

printf("Enter three numbers: ");


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

int max = (a > b && a > c) ? a : (b > c ? b : c);

printf("Maximum number is: %d\n", max);

return 0;
}

7. What is a control statement? Explain the purpose of decision-making and looping


3
statement.

Control statements dictate the flow of execution within a program based on certain
conditions or iterations.
• Decision-Making Statements: These include if statements and switch statements
that allow programs to make choices based on conditions.
• Looping Statements: These include for loops, while loops, and do-while loops that
enable repeated execution of code blocks until certain conditions are met.
Both types enhance flexibility and functionality by allowing dynamic responses based on
user input or other runtime conditions.

8. WAP to find the sum of digits of a number using function with arguments and with
return type.

#include

int sumOfDigits(int num) {


int sum = 0;

while(num > 0) {
sum += num % 10; // Add last digit to sum
num /= 10; // Remove last digit
}

return sum; // Return total sum


}

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

int result = sumOfDigits(number);

printf("Sum of digits: %d\n", result);

return 0;
}

4
9. Write functions to compute the factorial and power operation?
#include

// Function for factorial calculation


long long factorial(int n) {
if(n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

// Function for power calculation


double power(double base, int exponent) {
if(exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}

int main() {
int num;
double base;
int exp;

printf("Enter a number for factorial: ");


scanf("%d", &num);
printf("Factorial of %d is %lld\n", num, factorial(num));

printf("Enter base and exponent for power calculation: ");


scanf("%lf %d", &base, &exp);
printf("%.2lf raised to power %d is %.2lf\n", base, exp, power(base, exp));

return 0;
}

10. What is the difference between while and do-while loops?


The primary difference between while and do-while loops lies in when their condition
checks occur:
• In a while loop, the condition is evaluated before executing any statements inside its
block; thus it may not execute at all if the condition is false initially.
Example:
5
while(condition) {
// code block
}
• In a do-while loop, the statements inside its block execute at least once because its
condition check occurs after executing its body.
Example:
do {
// code block
} while(condition);
This guarantees at least one iteration regardless of whether the condition holds true
initially.

11. Differentiate between Continue and Break statements.


Both continue and break are control flow statements used within loops but serve different
purposes:
• The break statement terminates the nearest enclosing loop or switch statement
immediately when encountered.
Example:
for(int i = 0; i < n; i++) {
if(i == target)
break; // Exit loop when target found.
}
• The continue statement, on the other hand, skips over one iteration within its loop
but continues executing subsequent iterations.
Example:
for(int i = 0; i < n; i++) {
if(i == skipValue)
continue; // Skip current iteration when skipValue matches.
}
In summary, use break to exit from loops entirely and continue to skip only specific
iterations.

12. Write a C program to calculate the grade of a student by considering range marks
using switch-case statement.
#include

int main() {
int marks;

printf("Enter marks: ");


scanf("%d", &marks);

6
switch(marks / 10) { // Integer division gives ranges from grades.
case 9:
case 8:
printf("Grade: O\n");
break;
case 7:
printf("Grade: E\n");
break;
case 6:
printf("Grade: A\n");
break;
case 5:
printf("Grade: B\n");
break;
case 4:
printf("Grade: C\n");
break;
case 3:
printf("Grade: D\n");
break;
default:
if(marks >=0 && marks <=100)
printf("Grade: F\n");
else
printf("Invalid Marks!\n");
}

return 0;
}

13. Write a program in C to print Prime numbers present between 1 to n?


#include

int main() {
int n;

printf("Enter upper limit n: ");


scanf("%d", &n);

for(int i = 2; i <= n; i++) {


int prime = 1;

7
for(int j =2; j*j <= i; j++) {
if(i % j ==0) {
prime =0;
break;
}
}

if(prime ==1)
printf("%d ", i);
}

return(0);
}

14. Write a program in C to find GCD and LCM of two numbers?


#include

// Function to compute GCD using Euclidean algorithm.


int gcd(int a, int b) {
while(b !=0){
int temp=b;
b=a%b;
a=temp;
}

return(a);
}

// Function to compute LCM using GCD.


int lcm(int x,int y){
return(x*y/gcd(x,y));
}

int main(){
int num1,num2;

printf ("Enter two integers:\n");


scanf ("%d%d",&num1,&num2);

printf ("GCD=%d \nLCM=%d \n",gcd(num1,num2),lcm(num1,num2));

return(0);
8
}

15. Write a program to print below pattern A ABCBA ABCDCBA


#include

int main() {
char ch;

for(int i=65;i<69;i++){// Loop through rows A-D.


ch=i;// Set starting character each row

for(int j=65;j<=i;j++){// Print increasing part A-B-C-D...


putchar(ch++);
}

ch-=2;// Reset character back down

for(int j=i;j>65;j--){// Print decreasing part D-C-B-A...


putchar(ch--);
}

putchar('\n');
}

return(0);
}

You might also like