Programming in C Essay
Programming in C Essay
Module 1
1. Write a C program to check whether the entered year is leap year or not.
#include<stdio.h>
void main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if((year%4==0) && ((year%400==0) || (year%100!==0)))
{
printf("%d is a leap year", year);
}
else
{
printf("%d is not a leap year", year);
}
}
Output
Enter a year: 2004
2004 is a leap year
2. Explain different operators in C.
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Arithmetic Operators
Operator Description
Relational Operators
Operator Description
== Checks if the values of two operands are equal or not. If yes, then
the condition becomes true.
> Checks if the value of left operand is greater than the value of
right operand. If yes, then the condition becomes true.
< Checks if the value of left operand is less than the value of right
operand. If yes, then the condition becomes true.
>= Checks if the value of left operand is greater than or equal to the
value of right operand. If yes, then the condition becomes true.
<= Checks if the value of left operand is less than or equal to the
value of right operand. If yes, then the condition becomes true.
Logical Operators
Operator Description
&& Called Logical AND operator. If both the operands are non-
zero, then the condition becomes true.
Bitwise Operators
Operator Description
& Binary AND Operator copies a bit to the result if it exists in both
operands.
^ Binary XOR Operator copies the bit if it is set in one operand but not
both.
<< Binary Left Shift Operator. The left operands value is moved left by the
number of bits specified by the right operand.
>> Binary Right Shift Operator. The left operands value is moved right by the
number of bits specified by the right operand.
Assignment Operators
Operator Description
+= Add AND assignment operator. It adds the right operand to the left
operand and assign the result to the left operand.
/= Divide AND assignment operator. It divides the left operand with the
right operand and assigns the result to the left operand.
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
return 0;
}
Output
Enter an integer: -7
-7 is odd.
Selection Structure.
Use to make a decision or comparison and then, based on the result of that
decision or comparison, to select one of two paths. The condition must result
in either a true (yes) or false (no) answer.
There are two types of selection structures
a) two way selection structures
b) multi-way selection structures
a)if statement
b)if-else statement
a)if statement
Example
#include <stdio.h>
int main ()
{
int a = 10;
if( a < 20 )
{
printf("a is less than 20\n" );
}
return 0;
}
b)if-else statement
Example
#include <stdio.h>
int main ()
{
int a = 100;
if( a < 20 )
{
printf("a is less than 20\n" );
}
else
{
printf("a is not less than 20\n" );
}
return 0;
}
Output
a is not less than 20
MULTIWAY SELECTION STRUCTURE
case value1 :
statement(s);
break;
case value 2 :
statement(s);
break;
default :
statement(s);
break;
}
Flow Diagram
Example
#include<stdio.h>
int main()
{
float a,b,c;
int ch;
printf("Enter a number\n");
scanf("%f",&a);
printf("Enter another number\n");
scanf("%f",&b);
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("Please enter your choice\n");
scanf("%d",&ch);
switch(ch)
{ case 1: c=a+b;
printf("sum=%f",c);
break;
case 2: c=a-b;
printf("sub=%f",c);
break;
case 3: c=a*b;
printf("product=%f",c);
break;
case 4: c=a/b;
printf("division=%f",c);
break;
default: printf("Invalid choice");
break;
}
return 0;
}
Output
Enter a number
2
Enter another number
5
1. Addition
2. Subtraction
3. Multiplication
4. Division
Please enter your choice
3 Product=10.00000
c)nested if statements
which means you can use one if or else if statement inside another if or else if
statement(s).
Syntax
if( boolean_expression 1)
{
if(boolean_expression 2)
{
Statements;
}
}
Example
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers\n");
scanf("%d%d %d ",&a,&b,&c);
if (a>b)
{
if(a>c)
{
printf("The number %d is largest",a);
}
else
{
printf("The number %d is largest",c);
}
}
else
{
if(b>c)
{
printf("The number %d is largest",b);
}
else
{
printf("The number %d is largest",c);
}
}
return 0;
}
Output
Enter three numbers
2
4
8
The number 8 is largest
A C program is divided into different sections. There are six main sections to a basic
c program.
The six sections are,
Documentation
Link
Definition
Global Declarations
Main functions
Subprograms
Documentation Section
The documentation section is the part of the progra
programm where the programmer gives
the details associated with the program. He usually gives the name of the program,
the details of the author and other details like the time of coding and description. It
gives anyone reading the code the overview of the code.
Example
/*
MULTILINE COMMENT
File Name: Helloworld.c
Author: deepthy
date: 09/08/2019
*/
Link Section
This part of the code is used to declare all the header files that will be used in the
program. This leads to the compiler being told to link the header files to the system
libraries.
Example
#include<stdio.h>
Definition Section
In this section, we define different constants. The keyword define is used in this
part.
#define PI=3.14
Example
Float area;
int a=7;
a) A declaration part
b) Execution part.
The declaration part is the part where all the variables are declared. The execution
part begins with the curly brackets and ends with the curly close bracket. Both the
declaration and execution part are inside the curly braces.
Example
int main(void)
int a=10;
return 0;
date: 09/08/2019
*/
#include<stdio.h>//link section
float r;
scanf("%f",&r);
return 0;
Output
Example
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers\n");
scanf("%d%d %d ",&a,&b,&c);
if (a>b)
{
if(a>c)
{
printf("The number %d is largest",a);
}
else
{
printf("The number %d is largest",c);
}
}
else
{
if(b>c)
{
printf("The number %d is largest",b);
}
else
{
printf("The number %d is largest",c);
}
}
return 0;
}
Variables in C are associated with data type. Each data type requires an amount of
memory and performs specific operations.
There are some common data types in C −
int − Used to store an integer value.
char − Used to store a single character.
float − Used to store decimal numbers with single precision.
double − Used to store decimal numbers with double precision.
9.Write a C program to read the month number (between 1 and 12) and display
the corresponding month name (1-Jan, 2-Feb,…12-Dec) using switch statement
#include<stdio.h>
int main()
{
int m;
printf("enter a number between 1 and 12:");
scanf("%d" ,&m);
switch(m)
{
case 1: printf("january");
break;
case 2: printf("february");
break;
case 3: printf("march");
break;
case 4: printf("april");
break;
case 5: printf("may");
break;
case 6: printf("june");
break;
case 7: printf("july");
break;
case 8: printf("august");
break;
case 9: printf("september");
break;
case 10: printf("october");
break;
case 11: printf("november");
break;
case 12: printf("december");
break;
default: printf("invalid");
break;
}
return 0;
}
10.List the rules for defining variables
A variable in simple terms is a storage place which has some memory allocated to
it. Basically, a variable used to store some form of data.
Rules for defining variables
1. A variable can have alphabets, digits, and underscore.
2. A variable name can start with the alphabet, and underscore only. It can’t start
with a digit.
3. No whitespace is allowed within the variable name.
4. A variable name must not be any reserved word or keyword, e.g. int, goto , etc.
5. A variable does not contain any special characters and white space
#include<stdio.h>
int main()
{
int a,b,c,per,;
printf("enter 3 sides\n");
scanf("%d %d %d" ,&a,&b,&c);
if(a <b+c &&b<a+c &&c<a+b)
{
printf("triangle valid\n");
per=a+b+c;
printf("perimeter=%d" ,per);
}
else
{
printf("invalid triangle");
}
Return 0;
}
12. What is the use of 'default' statement in switch?
A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for
each switch case.
A switch statement can have an optional default case, which must appear at the
end of the switch. The default case can be used for performing a task when none of
the cases is true. No break is needed in the default case.
13Write a C program and check whether a given character is a vowel or not
#include<stdio.h>
int main()
{
char Al;
printf("enter an alphabet\n");
scanf("%c" ,&Al);
if(Al=='A'||Al=='a'||Al=='e'||Al=='E'||Al=='I'||Al=='i'||Al=='o'|| Al
=='O'||Al=='U'||Al=='u')
{
printf("%c is vowel" ,Al);
}
else
{
printf("%c is consonant" ,Al);
}
return 0;
}
Module 2
1) Write C program to find the sum of first N odd numbers using while loop.
#include<stdio.h>
int main()
scanf("%d", &num);
if(count%2 != 0)
count++;
return 0;
Output :
Enter a integer number
5
Sum of ODD integer number is 9
syntax
data_type array_name[rows][columns];
creation of 2D Array in C
method 1
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
eg:
#include<stdio.h>
int main()
{
int i=0,j=0;
Int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}
}
return 0;
}
Method 2
#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
}
Entry control loop checks condition The exit control loop first executes the body of the
first and then loop and
body of the loop will be executed. checks condition at last.
The body of the loop may or may The body of the loop will be executed at least once
not be executed at all. because the condition is checked at last
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
#include <stdio.h>
int main()
{
//Initialize array
int arr[10] = {5, 2, 8, 7, 1,6,10,9,4,3};
int temp,res;
int i, j;
Output
5
2
8
7
1
6
10
9
4
3
Result=1
int main() {
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
}
When the above program is executed, it produces the following result −
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
It was simple, but again, let's consider another situation when you want to
write Hello, World! a thousand times.
We can certainly not write printf() statements a thousand times.
Almost all the programming languages provide a concept called loop, which
helps in executing one or more statements up to a desired number of
times. All high-level programming languages provide various forms of
loops, which can be used to execute one or more statements repeatedly.
example
#include <stdio.h>
int main() {
int i = 0;
while ( i < 5 ) {
printf( "Hello, World!\n");
i = i + 1;
}
}
When the above program is executed, it produces the following result −
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
The above program makes use of a while loop, which is being used to
execute a set of programming statements enclosed within {....}. Here, the
computer first checks whether the given condition, i.e., variable "a" is less
than 5 or not and if it finds the condition is true, then the loop body is
entered to execute the given statements. Here, we have the following two
statements in the loop body −
First statement is printf() function, which prints Hello World!
Second statement is i = i + 1, which is used to increase the value of variable i
After executing all the statements given in the loop body, the computer goes
back to while( i < 5) and the given condition, (i < 5), is checked again, and the
loop is executed again if the condition holds true. This process repeats till the
given condition remains true which means variable "a" has a value less than
5.
To conclude, a loop statement allows us to execute a statement or group of
statements multiple times.
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d\t”,a[i][j]);
}
print(“\n”);
}
// print transpose of A
for(i=0;i<c;i++)
{
for(j=0;j<r;<j++)
{
printf(“%d\t”,a[j[i]);
}
print(“\n”);
}
return 0;
}
#include <stdio.h>
int main()
{
int n, rev = 0, rem, org;
printf("Enter an integer: ");
scanf("%d", &n);
org= n;
// reversed integer is stored in reversed variable
while (n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n =n/10;
}
return 0;
}
Output
Enter an integer: 1001
1001 is a palindrome.
Module 3
1)Explain any three possible pointer arithmetic operations.
Pointer Arithmetic’s in C
Pointers variables are also known as address data types because they are used to
store the address of another variable. The address is the memory location that is
assigned to the variable. It doesn’t store any value.
The pointer operations are:
1. Increment/Decrement of a Pointer
2. Addition of integer to a pointer
3. Subtraction of integer to a pointer
4. Subtracting two pointers of the same type
Increment/Decrement of a Pointer
Increment: It is a condition that also comes under addition. When a pointer is
incremented, it actually increments by the number equal to the size of the data
type for which it is a pointer.
ForExample:
If an integer pointer that stores address 100 is incremented, then it will
increment by 2(size of an int) and the new address it will points to 102.
While if a float type pointer is incremented then it will increment by 4(size of a
float) and the new address will be 104.
main()
0 1 2 3 4
{
5 8 9 6 10
Int A[5]={5,8,9,6,10}; 100 102 104 106 108
Int*p,*q;
P=&A[0];
P++; P++
p
100 102
}
Decrement: It is a condition that also comes under subtraction. When a pointer is
decremented, it actually decrements by the number equal to the size of the data
type for which it is a pointer.
ForExample:
If an integer pointer that stores address 102 is decremented, then it will
decrement by 2(size of an int) and the new address it will points to 100.
While if a float type pointer is decremented then it will decrement by 4(size of a
float) and the new address will be 98.
main()
0 1 2 3 4
{
5 8 9 6 10
Int A[5]={5,8,9,6,10}; 100 102 104 106 108
Int*p,*q;
P=&A[0];
P--; P
p--
100 102
}
Addition
When a pointer is added with a value, the value is first multiplied by the size of data
type and then added to the pointer.
main()
0 1 2 3 4
{
5 8 9 6 10
Int A[5]={5,8,9,6,10}; 100 102 104 106 108
Int *p,*q;
P=&A[0];
P=p+2; P+2
p
100 104
}
Subtraction
When a pointer is subtracted with a value, the value is first multiplied by the size of
the data type and then subtracted from the pointer.
main()
0 1 2 3 4
{
5 8 9 6 10
Int A[5]={5,8,9,6,10}; 100 102 104 106 108
Int *p,*q;
P=&A[0];
p=p-2; P
p-2
100 104
}
Subtraction of Two Pointers
The subtraction of two pointers is possible only when they have the same data type.
The result is generated by calculating the difference between the addresses of the
two pointers and calculating how many bits of data it is according to the pointer
data type. The subtraction of two pointers gives the increments between the two
pointers.
For Example:
Two integer pointers say ptr1(address:100) and ptr2(address:106) are subtracted.
The difference between address is 6 bytes. Since the size of int is 2 bytes, therefore
the increment between ptr1 and ptr2 is given by (6/2) = 3.
main()
0 1 2 3 4
{
5 8 9 6 10
Int A[5]={5,8,9,6,10}; 100 102 104 106 108
Int *p,*q,l;
p=&A[0];
q=&A[3]
l=p-q; q
p-2
106
100
}
2. Write a C program to read a string and get its duplicate without using string
function.
#include <stdio.h>
#include <string.h>
int main()
{
char Str[100], CopyStr[100];
int i;
printf("\n Please Enter any String : ");
gets(Str);
3. Explain the use of NULL character in the declaration and initialization of strings.
An array of characters (or) collection of characters is called a string.
Declaration
Refer to the declaration given below −
char stringname [size];
For example - char a[50]; a string of length 50 characters.
Initialization
The initialization is as follows −
Using single character constant −
char string[20] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
Pointer Comparison in C
When two pointers are compared, the result depends on the relative locations in
the address space of the objects pointed to. If two pointers to object types both
point to the same object, or both point one past the last element of the same array
object, they compare equal.( ര ് pointers compare െച ുേ ാൾ, pointed object ന്െറ relative position
ആ ശയി ിരി ും result. pointers ര ും ഒേര object േലേ ാ അെ ിൽ ഒേര array object ന്െറ lastelement കഴി
Pointer കളിേലേ ാ ആെണ ിൽ, അവ equal മായി compare െച ു ു.)
#include <stdio.h>
int main()
int A[]={1,2,3,4,5]
int *ptrA,*ptrB;
ptrA = &A[1];
ptrB = &A[4];
if(ptrB> ptrA)
return(0);
OUTPUT
Actually, you do not place the null character at the end of a string constant. The C
compiler automatically places the '\0' at the end of the string when it initializes the
array.
#include <stdio.h>
int main () {
#include <stdio.h>
int main()
{
char str[100];
char *ptr;
return 0;
}
Output
#include<stdio.h>
#include<string.h>
int main()
{
char studentname[20]=”Amit”;
int n;
n=strlen(studentname);
printf(“\nNumber of characters=%d”,n);
return(0);
}
Output
Number of characters=4
int main()
{
char source[] = "GeeksForGeeks";
printf("%s", target);
return 0;
}
Output:
GeeksForGeeks
strndup() :
#include<stdio.h>
#include<string.h>
int main()
// memory is returned.
printf("%s", target);
return 0;
Output:
Geeks
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping x = %d y = %d ", x, y);
return 0;
}
9. Describe the string handling functions to concatenate two strings with
example.
In C, the strcat() function is used to concatenate two strings.
The function takes two strings as input; the destination string, and the source
string. The pointer of the source string is appended to the end of the destination
string, thus concatenating both strings.
Example
#include <stdio.h>
#include <string.h>
int main()
{
char destination[] = "Hello ";
char source[] = "World!";
strcat(destination,source);
printf("Concatenated String: %s\n", destination);
return 0;
}
Output
Concatenated String: Hello World!
Module4
1. Write a C program to find out sum of two numbers using function
#include <stdio.h>
int sum(int a, int b)
{
return a+b;
}
int main()
{
int num1, num2, num3;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
Output: 1