Short Questions 2M
Short Questions 2M
UNIT - 1 :
1. Explain Characteristics of algorithm.
Ans.
Algorithm: it is step by step process of solving a problem.
Characteristics of algorithm:
Finiteness: algorithm must always terminate after finite number of steps.
Definiteness: each and every step should be clearly understood.
Effectiveness :each instruction should be performed in finite amount of time.
Input and Output :algorithm how zero or more input and one or more outputs.
Generality: same input to generate different outputs.
UNIT - 2 :
10. What is the difference between while loop and do…while loop? .
Ans.
While loop :
• It is a entry control loop
• It is used to check the condition. if the condition is true then while block is executed
continuously until the condition is false
Syntax : while(condition)
{
//Code
}
Eg program :
//to print the hello world message two times /
#include<stdio.h>
int main()
{
int i=1;
while(i<=2)
{
printf("Hello World\n");
i++;
}
return 0;
}
Do-While loop :
•It is exit control loop
•First, do the statement at least one time afterwards and check the condition. If the condition
is true then the loop is executed repeatedly until the condition is false.
SYNTAX: do
{
// Code
}
while(condition);
Eg program:
//To print hello world message//
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("Hello World\n");
i++;
}
while(i<=2);
return 0;
}
12. Write a C program to find all even numbers up to the given numbers.
Ans.
//To find the sum of all numbers upto the given number//
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n ;
printf("Enter the number:");
scanf("%d",&n);
printf("Even numbers upto the given number are :\n");
for(i=1 ; i<=n ; i++ )
{
if(i%2==0)
{
printf("%d" ,i );
}
}
return 0 ;
}
UNIT - 3 :
3. scanf() with %s format specifier - Read a string from the standard input.
Eg :
#include <stdio.h>
int main()
{
char str[50];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
return 0;
}
Eg:
for( i=0; i<n;i++)
{
Scanf(" %d'", &a[i] );
}
Storing values into 2D array :
SYNTAX :
for (initialisation1;condition1;increament)
{
for(initialization2; condition2; increament2)
{
scanf( "format specifier ", arrayname );
}
}
Eg:
for( i=0 ; i< 3; i++)
{
for( j=0 ; j<3; j++)
{
scanf("%d" , a[i][j]);
}
}
7. Develop a C program to find the length of the given string without using predefined
functions?
Ans.
//to find the length of string//
#include<stdio.h>
#include<string.h>
int main()
{
char str[128];
int i,count=0;
printf("Enter a string:");
scant("%s'str);
for(i=0 ;str[i]!=0;i++)
{
count++;
}
printf(“length of the string =%d",count);
return 0;
}
Note: In Method 3, the string "John" is stored in read-only memory, so you cannot modify it.
Eg :
//To demonstrates declaring a string:
#include <stdio.h>
int main()
{
char name[] = "John";
printf("Hello, %s!\n", name);
return 0;
}
gets()
- Reads a line of input from the standard input (usually the keyboard)
- Reads until it encounters a newline character
- gets()` reads a line of input until the newline character and includes whitespace characters.
Syntax : gets(string_name);
Eg :
#include <stdio.h>
int main()
{
char name[20];
printf("Enter your name: ");
gets(name);
printf("Name: %s\n", name);
return 0;
}
UNIT - 4 :
1. Write a program to convert floating point numbers into integers using pointers.
Ans.
//to convert float to int using pointers //
#include<stdio.h>
#include<conio.h>
Int main()
{
Int x, *p;
float y, *q;
printf( "Enter the floating point numbers :");
Scant ("%f",&y);
q=&y;
p=&x;
x= y;
Printf("floating number %f in integers is %d", *q,*p);
getch();
return O;
}
Arrow operator(->):
1.it is also called as structure pointer operator.
2.It is used to accessing members of pointer to structure
syntax:
calloc() : It is used to reserve multiple blocks of memory each of same size and then sets all
bytes to zero .calloc stands for continue memory allocation .it is primarily used to allocate
memory for arrays .
SYNTAX :
Pointername=(castname*)calloc(n,sizeof(data type));
Eg: p=(int*)calloc(10*sizeof(int));
fscanf(): fscanf() is a function that reads formatted input from a file. It is similar to `scanf()`,
but instead of reading from the standard input (usually the keyboard), it reads from a file.
Syntax: fscanf(file_pointer, format_string, argument1, argument2, ...);
Example:
FILE *file = fopen("example.txt", "r");
int x;
char name[20];
fscanf(file, "%d %s", &x, name);
printf("x = %d, name = %s\n", x, name);
fclose(file);
This will read an integer and a string from the file "example.txt" and print them to the
console.
Text Streams:
5. How to pass pointers as parameters and what were the advantages of it.
Ans.
Passing a pointer as a parameter to a function is a common practice in programming,
especially in languages like C. Here's how to do it:
int main() {
data_type variable_name;
function_name(&variable_name);
return 0;
}
Eg Program:
#include<stdio h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x =5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y); return 0;
}
Text Modes
1. r(Read): Opens a file for reading.
2. *w* (Write): Opens a file for writing.
3. *a* (Append): Opens a file for appending.
4. *r+* (Read and Write): Opens a file for both reading and writing.
5. *w+(Read and write): open a file for both reading and writing.
6. *a+* (Read and Append): Opens a file for both reading and appending.
Binary Modes
1. *rb* (Read Binary): Opens a file for reading in binary mode.
2. *wb* (Write Binary): Opens a file for writing in binary mode..
3. *ab* (Append Binary): Opens a file for appending in binary mode.
4. *r+b* (Read and Write Binary): Opens a file for both reading and writing in binary mode.
The file pointer is positioned at the beginning of the file.
5. *w+b* (Read and Write Binary): Opens a file for both reading and writing in binary mode.
Input Functions:
fopen(): Opens a file in a specified mode (e.g., "r" for reading, "w" for writing).
Eg: FILE *fp = fopen("example.txt", "r");
Output Functions
fopen(): Opens a file in a specified mode (e.g., "w" for writing, "a" for appending).
Eg: `FILE *fp = fopen("example.txt", "w")
Other Functions
fclose(): Closes a file.
Eg: fclose(fp);
10. Explain return type and return value with syntax
Ans.
Return Type: The return type is the data type of the value that a function returns.
Syntax:
return-type function-name()
{
// function body
return return-value;
}
Eg:
int addNumbers()
{
int sum = 2 + 3;
return sum; // returns an integer value
}
In this example, `int` is the return type.
Return Value: The return value is the actual value that a function returns.
Syntax: return return-value;
Example :
int addNumbers()
{
int sum = 2 + 3;
return sum; // returns the value 5
}
In this example, '5' is the return value.
12. What will be passed if an array is passed as parameter to the function & how to
manipulate array
in the function.
Ans.
When an array is passed as a parameter to a function, the following things happen:
1. Address of the first element is passed to a function, the address of the first element of the
array is passed, not the entire array.
2. Array decays to a pointer: In the function parameter list, the array name decays to a
pointer to the first element of the array.
Eg:
#include <stdio.h>
void manipulate_array(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
*(arr + i) = i * 2;
}
for (int i = 0; i < size; i++)
{
arr[i] = i * 3;
}
}
int main()
{
int arr[5];
int size = sizeof(arr) / sizeof(arr[0]);
manipulate_array(arr, size);
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}