CSE 121, Structured Programming Language (Eve) Shornaa
CSE 121, Structured Programming Language (Eve) Shornaa
avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}
Output:
Enter number 1
10
Enter number 2
10
Enter number 3
20
Enter number 4
40
Average of entered number is: 20
Functions in C Programming with
examples
A function is a block of statements that performs a specific task. Let’s
say you are writing a C program and you need to perform a same task in
that program more than once. In such case you have two options:
a) Use the same set of statements every time you want to perform the
task
b) Create a function to perform that task, and just call it every time you
need to perform that task.
Types of functions
1) Predefined standard library functions
Standard library functions are also known as built-in functions.
Functions such as puts(), gets(), printf(), scanf() etc are standard library
functions. These functions are already defined in header files (files
with .h extensions are called header files such as stdio.h), so we just call
them whenever there is a need to use them.
For example, printf() function is defined in <stdio.h> header file so in order
to use the printf() function, we need to include the <stdio.h> header file in
our program using #include <stdio.h>.
Now we will learn how to create user defined functions and how to use
them in C Programming
Syntax of a function
return_type function_name (argument list)
{
Set of statements – Block of code
}
return_type: Return type can be of any data type such as int, double,
char, void, short etc. Don’t worry you will understand these terms better
once you go through the examples below.
int main()
{
int var1, var2;
printf("Enter number 1: ");
scanf("%d",&var1);
printf("Enter number 2: ");
scanf("%d",&var2);
return 0;
}
Output:
Enter number 1: 100
Enter number 2: 120
Output: 220
C – Strings and String functions with
examples
String is an array of characters. In this guide, we learn how to declare
strings, how to work with strings in C programming and how to use the
pre-defined string handling functions.
We will see how to compare two strings, concatenate strings, copy one
string to another & perform various string manipulation operations. We
can perform such operations using the pre-defined functions of “string.h”
header file. In order to use these string functions you must include
string.h file in your C program.
String Declaration
Method 1:
char address[]="TEXAS";
In the above declaration NULL character (\0) will automatically be
inserted at the end of the string.
What is NULL Char “\0”?
'\0' represents the end of the string. It is also referred as String terminator
& Null Character.
/*Displaying String*/
printf("%s",nickname);
return 0;
}
Output:
Enter your Nick name:Negan
Negan
Note: %s format specifier is used for strings input/output
#include <stdio.h>
int main()
{
//Variable declaration
int num = 10;
//Pointer declaration
int *p;
Output:
System.out.println("Break Statement\n");
for(int i=1;i<=5;i++){
if(i==4) break;
System.out.println(i);
}
System.out.println("Continue Statement\n");
for(int i=1;i<=5;i++){
if(i==1) continue;
System.out.println(i);
}
}
Output
Break Statement
1
2
3
Continue Statement
2
3
4
5
What do you understand bynested loop? Give an
example of nested for loop.
A nested loop is a loop within a loop, an inner loop within the body of an outer one.
How this works is that the first pass of the outer loop triggers the inner loop, which
executes to completion. Then the second pass of the outer loop triggers the inner
loop again. This repeats until the outer loop finishes. Of course, a break within either
the inner or outer loop would interrupt this process.
# ===============================================
# Beginning of inner loop.
for b in 1 2 3 4 5
do
echo "Pass $inner in inner loop."
let "inner+=1" # Increment inner loop counter.
done
# End of inner loop.
# ===============================================
exit 0
Below program uses a nested for loop to print all prime factors of a number.in c
// C Program to print all prime factors
// of a number using nested loop
#include <math.h>
#include <stdio.h>
Output:
3 3 5 7