Coder Social home page Coder Social logo

adv-c-ans-key's Introduction

ADVANCED C PROGRAMMING ANSWER KEY

PART-A

QA101
Compare the for, while loop and do-while.

Ans:

for loop:

• Initialization is executed once, before the loop starts.
• Condition is checked. If true, the loop body executes.
• Increment/decrement is executed after each iteration.
• Steps 2-3 repeat until the condition is false.

while loop:

• Condition is checked. If true, the loop body executes.
• Steps 1-2 repeat until the condition is false.

do-while loop:

• The loop body executes at least once.
• Condition is checked. If true, the loop body executes again.
• Steps 2-3 repeat until the condition is false.

QA102
Write a program to initialize the value as 7680 & display the value on the monitor.

Ans:

#include

int main()
{
int value = 7680;
printf("The value is: %d\n", value);
return 0;
}

QA103
Illustrate the C Programming Structure.

Ans:

Structures are user-defined data types that allow you to group variables of different types under a single name. They provide a way to organize and manage related data as a single unit, making code more readable and maintainable.
QA104 Produce the output for the following C program

#include <stdio.h>

int main()
{
int number=1;
for(;number<10;)
{
printf("%d",number);
number++;
}
return 0;
}

Ans:

123456789
QA105
List the unconditional/special control statements in C programming language

Ans:

1. goto statement:
  1. break statement:

  2. continue statement:

  3. return statement:

QA106
write a program to find minimum between -300,-400,500 numbers using conditional operator?

Ans:

#include <stdio.h>

int main()
{
int num1 = -300;
int num2 = -400;
int num3 = 500;

int minimum = (num1 < num2) ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2 : num3);

printf("The minimum number is: %d\n", minimum);

return 0;
}

QA107
Write a C program to perform the basic left and right shift operation for 22 integer number.

Ans:

#include <stdio.h>

int main()
{
int num = 22;
int left_shifted, right_shifted;
left_shifted = num << 2;
printf("Left shift by 2 bits: %d\n", left_shifted);
right_shifted = num >> 3;
printf("Right shift by 3 bits: %d\n", right_shifted);
return 0;
}

QA108
Write a program to find product of two fraction values.

Ans:

#include <stdio.h>

int main()
{
int num1, denom1, num2, denom2, product_num, product_denom;

printf("Enter the first fraction (numerator denominator): ");
scanf("%d %d", &num1, &denom1);

printf("Enter the second fraction (numerator denominator): ");
scanf("%d %d", &num2, &denom2);

// Calculate the product of the numerators and denominators
product_num = num1 * num2;
product_denom = denom1 * denom2;

// Simplify the fraction if possible
int gcd = find_gcd(product_num, product_denom);
product_num /= gcd;
product_denom /= gcd;

printf("The product of the fractions is: %d/%d\n", product_num, product_denom);

return 0;
}

// Function to find the greatest common divisor (GCD)
int find_gcd(int a, int b)
{
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

QA109
write a  program to convert temperature 90.50 degree celsius to Fahrenheit?

Ans:

#include <stdio.h>

int main()
{
float celsius = 90.50;
float fahrenheit = (celsius * 9/5) + 32;
printf("%0.2f degrees Celsius is equal to %0.2f degrees Fahrenheit.\n", celsius, fahrenheit);
return 0;
}

QA110
Write a program to find perimeter of square

Ans:

#include <stdio.h>

int main()
{
int side;
scanf("%d", &side);
int perimeter = 4 * side;
printf("The perimeter of the square is: %d\n", perimeter);
return 0;
}

QA201
Elaborate the purpose of using functions in C?

Ans:

1. Code Reusability
2. Modularity
3. Abstraction
4. Code Testing
5. Collaboration
6. Problem Decomposition
QA202
Discuss the types of arguments passing in C.

Ans:

1. Call by Value:

When a function is called by value, copies of the actual arguments are passed to the function's formal parameters.

2. Call by Reference:

In call by reference, the addresses of the actual arguments are passed to the function's formal parameters.
QA203
Compare the pointer and pointer to pointer variable with suitable example.

Ans:

Pointer:

• A variable that stores the memory address of another variable.
• It's declared using the * operator: int *ptr;
• It points to the first byte of the variable it refers to.

Pointer to Pointer:

• A variable that stores the address of another pointer variable.
• It's declared using two * operators: int **ptrptr;
• It points to the memory location where another pointer is stored.

QA204
Write a C program to print the address of variable, pointer and pointer value.(Assume “i” is variable and “p” is pointer )

Ans:

#include <stdio.h>

int main()
{
int i = 10;
int *p = &i;
printf("Address of variable i: %p\n", &i);
printf("Address of pointer p: %p\n", &p);
printf("Value of pointer p (address of i): %p\n", p);
printf("Value stored at the address pointed to by p: %d\n", *p);
return 0;
}

QA205
Write a C Program for pointer arithmetic of any type.

Ans:

#include <stdio.h>

int main() {
// Declare a pointer of any type (modify as needed)
double *ptr;

// Allocate memory for 5 elements of the chosen type

ptr = (double *)malloc(5 * sizeof(double)); // Adjust sizeof for different types

// Initialize the elements using pointer arithmetic

for (int i = 0; i < 5; i++) {
*(ptr + i) = i * 2.5; // Assign values using pointer offset
}

// Print the elements using pointer arithmetic

for (int i = 0; i < 5; i++) {
printf("Element %d: %.1f\n", i, *(ptr + i)); // Access values using pointer offset
}

// Free the allocated memory

free(ptr);

return 0;
}

QA206
Write a C program for Multiplication of two numbers using functions (With argument and with return type)

Ans:

#include <stdio.h>

int multiply(int num1, int num2)
{
int product = num1 * num2;
return product;
}

int main() {
int num1, num2, result;
scanf("%d %d", &num1, &num2);
result = multiply(num1, num2);

printf("The product of %d and %d is %d\n", num1, num2, result);

return 0;
}

QA207
Write a C program for subtraction of two numbers using functions (With argument and without return type)

Ans:

#include <stdio.h>

void subtract(int num1, int num2) {
int difference = num1 - num2;
printf("The difference of %d and %d is %d\n", num1, num2, difference);
}

int main() {
int num1, num2;

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

subtract(num1, num2);

return 0;
}

QA208
Write a C program for Division of two numbers using functions (With argument and without return type)

Ans:

#include <stdio.h>

void divide(int num1, int num2) {
if (num2 == 0) {
printf("Error: Division by zero is not allowed.\n");
}
else {
float quotient = (float)num1 / num2;
printf("The quotient of %d and %d is %.2f\n", num1, num2, quotient);
}
}

int main() {
int num1, num2;

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

divide(num1, num2);

return 0;
}

QA209
Write a C program for addition and subtraction a =30 and b=20 using functions

Ans:

#include <stdio.h>

int add(int a, int b) {
return a + b;
}

int subtract(int a, int b) {
return a - b;
}

int main() {
int a = 30, b = 20;

int sum = add(a, b);
printf("The sum of %d and %d is %d\n", a, b, sum);

int difference = subtract(a, b);
printf("The difference of %d and %d is %d\n", a, b, difference);

return 0;
}

QA210
Write a C program for Multiplication and Division of a = 50 and b =5 using functions 

Ans:

#include <stdio.h>

int multiply(int a, int b) {
return a * b;
}

float divide(int a, int b) {
return a / b;
}

int main() {
int a = 50, b = 5;

int product = multiply(a, b);
printf("The product of %d and %d is %d\n", a, b, product);

float division = divide(a, b);
printf("The quotient of %d and %d is %.2f\n", a, b, quotient);

return 0;
}

QA301
Illustrate the Array and how to initialize an array?

Ans:

Arrays:

A collection of elements of the same data type, stored in contiguous memory locations.
Accessed using an index, starting from 0.
Represented with square brackets [] after the variable name.

Types of Array Initialization:

1.Initialization during declaration:
example: int numbers[5] = {10, 20, 30, 40, 50};

2.Initialization after declaration:
example:
float prices[3];
for (int i = 0; i < 3; i++) {
prices[i] = 12.5 * (i + 1);
}

QA302
Determine 3-Dimentional Array and indexing with neat diagram.
3-Dimensional Arrays:
Represent a collection of elements arranged in a 3D grid-like structure, with rows, columns, and depth.
Visualize as a cube of elements.

Declaration:
data_type array_name[rows][columns][depth];

Indexing:
Access elements using three indices:
array_name[row_index][column_index][depth_index]
Each index starts from 0.

diagram
QA303

Ans:

Found element 30 at position 2
QA304
List any two sorting techniques and searching techniques.

Ans:

Sorting Techniques:

Bubble Sort
Merge Sort

Searching Techniques:

Linear Search
Binary Search
QA305

Ans:

The length of the name is 30
QA306

Create  a C program to read n number of elements and print the first element of the array (integer).

Ans:

#include <stdio.h>

int main() {
int n, i;
int array[100];
scanf("%d", &n);

for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}

printf("The first element of the array is: %d\n", array[0]);

return 0;
}

QA307
Create  a C program to read n elements as input and print the second element of the array (integer).

Ans:

#include <stdio.h>

int main() {
int n, i;
int array[100];

scanf("%d", &n);

for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}

printf("The second element of the array is: %d\n", array[1]);

return 0;
}

QA308
Create  a C program to read n elements as input and print the last element of the array (integer)

Ans:

#include <stdio.h>

int main() {
int n, i;
int array[100];

scanf("%d", &n);

for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}

printf("The second element of the array is: %d\n", array[n-1]);

return 0;
}

QA309
Create  a C program to read n elements as input and print the elements of the array (character).

Ans:

#include <stdio.h>

int main() {
int i, n;
char array[100];

scanf("%d", &n);

for (i = 0; i < n; i++) {
scanf(" %c", &array[i]);

for (i = 0; i < n; i++) {
printf("%c ", array[i]);
}
printf("\n");

return 0;
}

QA310
Write a program in C to read n number of values in an array and display it in reverse order.

Ans:

#include <stdio.h>

int main() {
int n, i;
int array[100];
scanf("%d", &n);

for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}

for (i = n - 1; i >= 0; i--) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}

QA401
Define structure with an example

Ans:

In C programming, a structure is a user-defined composite data type that groups variables of different data types under a single name. It's used to represent a collection of related data items as a single unit, making code more organized and easier to manage.
Example:

#include <stdio.h>

struct Student {
int roll_no;
char name[50];
float marks;
};

int main() {
struct Student s1 = {123, "Alice", 85.5};
printf("Roll Number: %d\n", s1.roll_no);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);

return 0;
}

QA402

Ans:

Ravikumar 1981
QA403

Ans:

101 Saveetha
QA404
Define self referenced structure with an example?

Ans:

A self-referenced structure, also known as a self-referential structure, is a structure that contains a pointer to a variable of the same structure type. This creates a recursive relationship, allowing the structure to refer to itself.
Example:

#include<stdio.h>

struct person
{

int id;
float height;
};

int main()
{
struct person *personPtr, person1;
personPtr=&person1;
printf("Displaying:\n");
scanf("%d%f",&personPtr->id,&personPtr->height);
printf("id: %d\nheight: %f",personPtr->id,personPtr->height);
}

QA405
Compare the Single Linked List and Double Linked List.

Ans:

• Single Linked List: Contains nodes with data and a single link pointing to the next node. Nodes cannot be accessed from the end.
• Double Linked List: Contains nodes with data, a link to the next node, and a link to the previous node. Access is possible from both ends.
QA406
Define union with an example

Ans:

In C programming, a union is a user-defined data type that allows storing different data types in the same memory location, but only one member can hold a value at a time. This means that all members share the same memory space, and the size of the union is equal to the size of its largest member.
Example:

#include <stdio.h>

union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;

data.i = 10;
printf("Integer value: %d\n", data.i);
data.f = 3.14;
printf("Float value: %f\n", data.f);

strcpy(data.str, "Hello");
printf("String value: %s\n", data.str);

return 0;
}

QA407

Ans:

Ravikumar 4202500
QA408

Ans:

101 e
QA409
Compare the structure and union.

Ans:

QA410
Compare structure and self-referenced structure with an example?

Ans:

QA501
Write a program to find sum of two fraction values

Ans:

#include <stdio.h>

int main() {
int numerator1, denominator1, numerator2, denominator2, resultNumerator, resultDenominator;

// Input for the first fraction
printf("Enter the numerator and denominator of the first fraction separated by space: ");
scanf("%d %d", &numerator1, &denominator1);

// Input for the second fraction
printf("Enter the numerator and denominator of the second fraction separated by space: ");
scanf("%d %d", &numerator2, &denominator2);

// Check if denominators are not zero
if (denominator1 == 0 || denominator2 == 0) {
    printf("Error: Denominator cannot be zero.\n");
    return 1; // Exit with an error code
}

// Calculate the sum of the fractions
resultNumerator = numerator1 * denominator2 + numerator2 * denominator1;
resultDenominator = denominator1 * denominator2;

// Output the result
printf("The sum of the fractions %d/%d and %d/%d is: %d/%d\n",
       numerator1, denominator1, numerator2, denominator2, resultNumerator, resultDenominator);

return 0; 

}

QA502
Write a program to find the ASCII value of character ‘R’ ?

Ans:

#include <stdio.h>

int main() {
char character = 'R';
int ascii_value = character;

printf("The ASCII value of '%c' is %d\n", character, ascii_value);

return 0;
}

QA503
Define Conditional Operator with an example?

Ans:

The conditional operator (also known as the ternary operator) is a concise way to express an if-else statement in a single line of code. It's often used to assign a value to a variable based on a condition.

•syntax:

condition ? expression1 : expression2

•Example:

int age = 25;
char *category = (age >= 18) ? "Adult" : "Minor";
printf("You are categorized as: %s\n", category);

QA504
Write a C program to search an element in an array ?

Ans:

QA505
Write the difference between argc and argv arguments in main()

Ans:

QA506
write a c program to copy a  string  into another string without using strcpy() using for loop.

Ans:

QA507 write a Program to compare two strings without using strcmp().
input: saveetha cse
output: strings are not same

Ans:

QA508 write a Program To Convert The String 'Saveetha' To Uppercase
Output: Upper Case String Is :SAVEETHA

Ans:

QA509 write a Program To Convert The String 'SAVEETHA' To Lowercase
Output: Lower Case String Is :saveetha

Ans:

QA510
write a program to find the ASCII value of *.

Ans:

PART-B

QB101 (b)

Ans:

QB102 (b)
Write a C program to calculate the area of a square(side*side) and perimeter of a square(4*side)

Ans:

QB103 (a)
Build a C program to print leap years in given range from 1900 to 1999?

Ans:

QB104 (a)
Write a C program to read the student marks and print the which class he/she got? marks>=70 print FIRST CLASS WITH DISTINCTION 60>= marks <70 print FIRST CLASS 50>= marks <60 print SECOND CLASS 40>= marks <50 print THIRD CLASS marks<40 print U r Failed...Better luck next time

Ans:

QB105 (a)
Write a C program to input number from the user and check whether the number is even or odd using a SWITCH CASE.

Ans:

QB201 (b)
Write Program to print odd numbers in reverse order, range from M to N (including M and N values) using functions.

Ans:

QB202 (a)
Write a C program to swap two numbers using pointers without using 3rd variable ?.

Ans:

QB203 (b)
Write a C program to display the factorial of a number using recursive function

Ans:

QB204 (a)
Write a C program for Multiplication and Division of two numbers using functions (Without argument and with return type)

Ans:

QB205 (a)
Write a C program for finding the factorial of a given number using function without return type with arguments.

Ans:

QB301 (a)
Write a C Program to find largest and second largest elements in the given array Using sorting.

Ans:

QB302 (a)
Write a C Program to get the 6 subject marks from the user using array and display the average of the student in float. Also write the output for this program.

Ans:

QB303 (a) Write a C Program to arrange the following values 16, 14, 21, 61, 22 in ascending order using bubble sorting technique.
Before bubble sorting the elements are:
16 14 21 61 22
After bubble sorting the elements are:
14 16 21 22 61

Ans:

QB304 (a)
Create  a C program to read n elements as input and print the elements of an array present on odd position

Ans:

QB305 (a)
Write  a C program to read the elements of the n x n matrix and print the last element of the matrix

Ans:

QB401 (a)
Build a C Program for Single Linked List with Create () and DisplayReverse () operations.

Ans:

QB402 (a)
Create a C Program for Single Linked List with Delete () and Display () operations.

Ans:

QB403 (b)
Implement a C Program for Double Linked List with Delete () and Display () operations.

Ans:


QB404 (b)
Build a C Program for Single Linked List with all operations

Ans:

QB405 (b)
Build a C Program for Double Linked List with all operations

Ans:

QB501 (b)
Write a C program to read temperature in centigrade and display a suitable message according to temperature state below : Temp < 0 then Freezing weather, Temp 0-20 then Very Cold weather, Temp 20-30 then Cold weather, Temp 30-40 then Normal in Temp, Temp 40-50 then Its Hot, Temp >=50 then Its Very Hot

Ans:

QB502 (a)
Write a C program to read the wind speed in kph and display a suitable message according to the wind speed state below.(use ELSE-IF ladder) * Strong breeze at 39-49 kph (25-31 mph) * Moderate gale at 50-61 kph (32-38 mph) * Fresh gale at 62-74 kph (39-46 mph) * Strong gale at < 98 kph

Ans:

QB503 (a)
Write a c program to count total number of negative elements in an array

Ans:

QB504 (b)
Write a C Program to take two numbers as input from the user and then calculating the following with nested if statement. 1. check whether the number are equal or not. 2.  find the largest of two numbers

Ans:

QB505 (a)
Write  a C program to read the elements of the n x n matrix and print the elements of last row of the matrix

Ans:

PART-C

QC101 (b) Develop a C program to check whether a number is palindrome or not
Sample input: 3223
Sample output: 3223 is a palindrome

Sample input: 1234
Sample output: 4321 is not a palindrome

Ans:

QC102 (a)
Write a C program to read temperature in centigrade and display a suitable message according to temperature state below : Temp < 0 then Freezing weather Temp 0-10 then Very Cold weather Temp 10-20 then Cold weather Temp 20-30 then Normal in Temp Temp 30-40 then Its Hot Temp >=40 then Its Very Hot

Ans:

QC201 (a)

Ans:

QC202 (a)

Ans:

QC301 (a)
Construct a program in C for Bubble Sort Technique using Arrays and explain the Sorting technique with example.

Ans:

QC302 (a)
Write  a C program to read the elements of the n x n matrix and print the diagonal elements of the matrix

Ans:

QC401 (b)
Implement a C Program for Single Linked List with Create (), Insert (), Search () and Display () operations.

Ans:

QC402 (b)
Implement a C Program for Double Linked List with Create (), Insert (), Search () and Display () operations.

Ans:

QC501 (b) C program to find sum of each row of a matrix please enter number of rows and columns : 3 3
Input elements of matrix: {1 2 3} { 4 5 6} { 7 8 9}
Output: The Sum of Elements of a Rows in a Matrix:  6
The Sum of Elements of a Rows in a Matrix:  15
The Sum of Elements of a Rows in a Matrix:  24

Ans:

QC502 (a)
Write  a C program to read the elements of the n x n matrix and check whether the diagonal elements of the matrix is divisible by 3

Ans:

adv-c-ans-key's People

Contributors

r-sathish-02 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.