Summer-2023-Ans Pic-22226
Summer-2023-Ans Pic-22226
b)
ANS:
printf( ) & scanf( ):
printf() and scanf() functions are library functions in C programming language defined in
“stdio.h” header file.
Uses of printf():
1. It is used to print the any message onto the output screen.
2. It is used to print formatted text and values to the standard output stream.
ANS: Definition:
A pointer is a variable that stores memory address of another variable which is of
similar data type.
Declaration:
datatype *pointer_variable_name;
Eg: int *ptr;
g) Define algorithm.
OUTPUT
Enter three numbers: 10 11 15 15.00
is the largest number.
ANS: The C goto statement is a jump statement which is sometimes also referred to as an
unconditional jump statement. The goto statement can be used to jump from anywhere to
anywhere within a function.
Syntax:
label;bel;
. |.
label:
In the above syntax, the first line tells the compiler to go to or jump to the statement marked as a
label. Here, the label is a user-defined identifier that indicates the target statement.
The statement immediately followed after ‘label:’ is the destination statement. The ‘label:’ can also
appear before the ‘goto label;’ statement in the above syntax.
Type 1: In this case, we will see a situation similar to as shown in Syntax1 above. Suppose we
need to write a program where we need to check if a number is even or not and print accordingly
using the goto statement. The below program explains how to do this:
#include <stdio.h>
printNumbers()
{ int n = 1;
label:
n++; if (n <=
int main() {
printNumbers();
return 0;
Output:
1 2 3 4 5 6 7 8 9 10
{ int feet;
float inch;
main()
{ printf("Enter feet and inches for the first distance: \n");
sum.inch); return 0;
Output:
d) Write a program to calculate sum of all elements stored in given array using pointers.
{ scanf("%d", a +
i);
i);
sum); return 0;
49
10
56
100
Comments in C
Comments can be used to explain code, and to make it more readable. It can also be used to
prevent execution when testing alternative code.
Comments can be singled-lined or multi-lined.
Single-line Comments
Any text between // and the end of the line is ignored by the compiler (will not be executed).
// This is a comment
printf("Hello World!");
C Multi-line Comments
Example /*
The code below will print the words Hello World! to the screen,
and it is amazing */ printf("Hello World!");
b) Explain any two math function with syntax and give example of each. ANS:
The C Math Functions can be classified into two main categories: Basic Math Functions and
Advanced Math Functions. In addition to these main categories, there are also a number of utility
functions that are used for specific applications. Let's delve deeper into these categories.
These Basic Math Functions can handle most simple arithmetic operations in your programs.
These Advanced Math Functions are typically used in more specialized tasks and scientific
applications where a higher level of precision is required.
In C language, hea)der files contain a set of predefined standard library functions. The .h is the
extension of the header files in C and we request to use a header file in our program by including
it with the C preprocessing directive “#include”.
C Header files offer the features like library functions, data types, macros, etc by importing them
into the program with the help of a preprocessor directive “#include”.
Syntax of Header Files in C
We can include header files in C by using one of the given two syntax whether it is a predefined
or user-defined header file.
#include <filename.h> // for files in system/default directory or
#include "filename.h" // for files in same directory as source file
ANS:
The call by valuemethod of passing arguments to a function copies the actual value of an
argument into the formal parameter
of the function. In this case, changes made to the parameter
inside the function have no effect on the argument.
By default, C programming uses call by valueto pass arguments. In general, it means the code
within a function cannot alter the arguments use d to call the function. Consider the
functionswap()definition as follows.
int temp;
return; Now, let us call the function swap() by passing actual values as in the following example −
}
Live Demo
#include <stdio.h>
int main () {
return 0; }
void swap(int x, int y) {
int temp;
return; }
Let us put the above code in a single C file, compile and execute it, it will produce the following
result −
int main() { int num, i, ctr = 0; // Declare variables for user input, loop control, and a counter.
// Start a loop to check for factors of the input number. for (i = 2; i <= num
+; // Increment the counter. break; // Exit the loop since a factor has been
found.
// If no factors were found and the number is not 1. if (ctr == 0 && num != 1) printf("%d
else printf("%d is not a prime number.\n", num); // Print that the number is not prime.
Input a number: 13
13 is a prime number.
Flowchart:
p
{
{
}
}
p
{
{
}
}
f
{
f
{
m
{
}
}
}
ANS:include<stdio.h>
int main() {
int i, j, rows;
printf("Enterthenumberof rows:");
scanf("%d", &rows);
for(i = 1; i <= rows;++) i {
for(j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return0;
}
C Structure Declaration
We have to declare structure in C before using it in our program. In structure declaration, we
specify its member variables along with their
datatype. We can use the struct keyword to
declare the structure in C using the following syntax:
Syntax
5. printf("Enter a number: ");
6. scanf("%d",&number);
7. for(i=1;i<=number;i++){
8. fact=fact*i;
9. }
10. printf("Factorial of %d is: %d",number,fact);
11. return 0;
12. }
Output:
Enter a number: 5
Factorial of 5 is: 120
struct point
{ int value;
};
int main()
{
struct point s;
return 0;
}