[go: up one dir, main page]

0% found this document useful (0 votes)
22 views42 pages

Cse II Lab

Uploaded by

sasi.oasys
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views42 pages

Cse II Lab

Uploaded by

sasi.oasys
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

CS3271-CP LABORATORY

1. I/O statements, operators, expressions


2. decision-making constructs: if-else, goto, switch-case, break-continue
3. Loops: for, while, do-while
4. Arrays: 1D and 2D, Multi-dimensional arrays, traversal
5. Strings: operations
6. Functions: call, return, passing parameters by (value, reference), passing arrays to
function.
7. Recursion
8. Pointers: Pointers to functions, Arrays, Strings, Pointers to Pointers, Array of
Pointers
9. Structures: Nested Structures, Pointers to Structures, Arrays of Structures and
Unions.
10. Files: reading and writing, File pointers, file operations, random access, processor
directives.
EX 1A C PROGRAM USING I/O STATEMENTS

AIM:
To write a C program using I/O statements using built-in printf() and scanf() function.

ALGORITHM:
Step1: Start
Step2:Declare and initialize the variables for a, b,
c. Step3:Display addition of numbers.
Step4:End
PROGRAM:

#include<stdio
.h>
#include<coni
o.h> void
main()
{
int a,b,c;
printf("Please enter any two numbers: \n");
scanf("%d %d", &a, &b);
c = a+ b;
printf("The addition of two number is: %d", c);
}

OUTPUT:
Please enter any two numbers:
4 5 The addition of two
number is 9

RESULT:

Thus the C program to perform input and output operations using built-in printf() and scanf() function
has been done and verified successfully.
EX 1B PROGRAM TO PERFORM THE CALCULATOR
OPERATIONS,NAMELY,ADDITION,SUBTRACTION,MULTIPLICATI
ON,DIVISION AND SQUARE OF A NUMBER AND EXPRESSIONS

AIM:
To write a C program to perform the calculator operations namely addition, subtraction, multiplication, division and

square of a number and expressions.

ALGORITHM:

Step1: Start the program

Step 2 :Declare the functions

Step3: Get the input values

Step4:Perform the operations using functions

Step5:Print the results

Step6:Stop the program

PROGRAM:

#include<stdio.h> int add(int

n1,intn2);

int subtract(int n1, int n2); int

multiply(intn1,intn2); int

divide(intn1,intn2);

int square(intn1); int main()

{
int num1,num2;
printf("Enter two numbers:");

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

printf("%d + %d = %d\n", num1, num2, add(num1, num2));

printf("%d - %d = %d\n", num1, num2, subtract(num1, num2));

printf("%d * %d = %d\n", num1, num2, multiply(num1, num2));


printf("%d / %d = %d\n", num1, num2, divide(num1, num2));

printf(“%d^%d=%d\n”,num1,num1,square(num1));

return0;

int add(intn1,intn2)
{

int result; result=n1+n2;return

result;

int subtract(intn1,intn2)

int result;

result = n1 - n2; return result;

int multiply(intn1,intn2)

int result; result=n1*n2;

return result;

int divide(intn1,intn2)
{

int result;

result = n1 / n2;

return result;

int square(intn1)

int result;
result = n1*n1;

return result;

OUTPUT:

Enter two numbers: 205

20+5=25

20–5=15

20*5=100

20/5=4

20^20=400

RESULT

Thus the C Program to perform the Calculator operations,namely,addition,subtraction, multiplication,


division and square of a number has been executed and results are verified.
EX 2A C PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS LEAP YEAR
OR NOT USING IF-ELSE.

AIM:

To write a C program to check whether a given number is leap year or not using IF-ELSE.
ALGORITHM:

STEP 1:Start the program.


STEP 2:Declare the required variables.
STEP 3:Read the input from the user and store it in a
variable. STEP 4:Using if..else statement,
i. Check whether the given input is divisible by 400
ii. Check whether the given input is divisible by 100
iii. Check whether the given input is divisible by 4 If all conditions are
stratified, then go to step 5 otherwise go to step 6.
STEP 5:Print the result as “It is a leap year” and goto step 7.
STEP 6:Print the result as “It is not a leap year”
STEP 7:Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.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);

getch();
}
OUTPUT:
Enter a year:
2004 2004 is a
leap year

RESULT:

Thus the program to check for given year is leap year or not has been written in C and the output was
verified successfully.
C program to check if a number is
EX 2B even or not using goto statement

AIM:

To write a C Program to find whether the given number is odd or even using goto statement.

ALGORITHM:

Step1:Start

Step2:Take integer variable year

Step3:Check if year is divisible by 400 then DISPLAY "is a leap year"

Step4:Check if year is not divisible by 100 AND divisible by 4 then DISPLAY "is a
leap year"

Step5:Otherwise, DISPLAY "is not a leap

year" Step6:Stop

PROGRAM:
#include <stdio.h>
void check EvenOrNot(int num)
{
if (num % 2 == 0)
// jump to
even goto
even;
else
// jump to
odd goto
odd; even:
printf("%d is even", num);
// return if even
return;
odd:
printf("%d is odd", num);
}
int main()
{ int num =
26;
check EvenOrNot(num);
return 0;
}
OUTPUT: 26 is even , 5 is odd

RESULT:
Thus the C Program to write a C Program to find whether the given number is odd or even
using go to statement has been verified and executed successfully.
EX2C C PROGRAM TO CHECK WHETHER THE ENTERED
CHARACTER IS VOWEL OR NOT (USE SWITCH CASE)

AIM:

To write a C program to check whether the entered character is vowel or not(Use switch case)

ALGORITHM:

Step1: Start
Step2:Declare and initialize the variables
Step3:Get the input from the user and compare with each cases

Step 4: if match found, print vowel otherwise print consonant

Step5:End

PROGRAM:

#include<stdio.h>
#include<conio.h>
intmain()
{
charch;
printf("Enter a character: ");
scanf("%c",&ch);
//condition to check character is alphabet or
notif((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
{
switch(ch)
{
case'A':
case'E':
case'I':
case'O':
case'U':
case'a':
case'e':
case'i':
case'o':
case'u':
printf("%c is aVOWEL.\n",ch);
break;
default:
printf("%c is aCONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}
return0;
}

OUTPUT:

Enter a character

E is a vowel

Enter a character

X is a consonant

Enter a character

+ is not an alphabet

RESULT:

Thus the C Program check whether the entered character is vowel or not (Use switch
case) has been executed and the output was verified.
EX 2D C PROGRAM USING BREAK STATEMENT

AIM:

To write a C program using break statement.

ALGORITHM:

Step1: Start
Step2:Declare and initialize the variables
Step3:If the variable is greater than 15 then terminate the loop
using break statement.

Step 4: End
PROGRAM:

#include<stdio.h>
#include<conio.h>
int main ()
{
int a =10;
while( a <20)
{
printf("value of a: %d\n",
a); a++;
if( a >15){
break;
}
}
getch();
return0;
}

OUTPUT:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

RESULT:

Thus the C Program using break statement has been executed and the output was
verified.
EX 2E C PROGRAM USING CONTINUE STATEMENT

AIM:

To write a C program using continue statement.

ALGORITHM:

Step1: Start
Step2:Declare and initialize the variables
Step3:if the variable is 15 then skip the iteration.

Step 4: then continue the further execution.

Step5:End

PROGRAM:

#include<stdio.h>
int main ()
{
int a =10;
do{
if( a ==15){
a = a +1; continue;
}
printf("value of a: %d\n", a); a++;
}while( a <20);
return0;
}
OUTPUT:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

RESULT:
Thus the C Program using continues statement has been executed and the output was verified.
EX 3A C PROGRAM USING FOR LOOP

AIM:

To write a C program using for loop.

ALGORITHM:

Step1: Start
Step2:Declare and initialize the local variables
Step3:Then declare execution of for loops

Step 4: Print the result

Step5:End

PROGRAM:

#include<stdio.h>

int main ()
{
int n, times=5;

for( n =1; n <= times; n = n +1)


{
printf("C for loops: %d\n", n);
}

return0;
}
OUTPUT:

C for loops: 1
C for loops: 2
C for loops: 3
C for loops: 4
C for loops: 5

RESULT :

Thus the C Program using for loop has been executed and the output was verified.
EX 3B C PROGRAM USING WHILE LOOP

AIM:

To write a C program using while loop.

ALGORITHM:

Step1: Start
Step2:Initializing the variables
Step3:Perform while loop with condition
Step 4: Check the Incrementing the operation

Step5:End

PROGRAM:
#include<stdio.h>
#include<conio.h> int main()
{
int num=1;
while(num<=10)
{
printf("%d\n" ,num);
num++;
}
return 0;
}

OUTPUT:

1
2
3
4
5
6
7
8
9
10

RESULT:

Thus the C Program using while loop has been executed and the output was verified.
EX 3C C PROGRAM USING DO-WHILE LOOP

AIM:

To write a C program using do-while loop.

ALGORITHM:

Step1: Start
Step2:Initializing the variables
Step3:Perform do-while loop with condition
Step 4: Check the Incrementing the operation

Step5:End

PROGRAM:

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
do
{
printf("%d\n",2*num);
num++;
}while(num<=10);
return 0;
}

OUTPUT:
2
4
6
8
10
12
14
16
18
20

RESULT:
Thus the C Program using do-while loop has been executed and the output was verified.
EX4A
C PROGRAM FOR ACCESS ARRAY ELEMENTS
USING ONE DIMENSIONAL ARRAY

AIM: To write a C program for access array elements using one dimensional array.

ALGORITHM:
Step 1: Start
Step 2:Initialize array name and array size.
Step 3:Access the array elements after dynamic initialization
Step 4:Print the result.
Step 5:Stop.

PROGRAM:
#include <stdio.h>
int main() {
int nums[5];
printf("\n Run-Time Initialization Example:\n");
printf("\n Enter array elements: ");

for (int i = 0; i < 5; i++) {


scanf("%d", &nums[i]);
}

printf(" Accessing array elements after dynamic Initialization: ");

for (int i = 0; i < 5; i++) {


printf("%d ", nums[i]);
}

return 0;
}

OUTPUT:
Run-Time Initialisation Example:

Enter array elements: 10 20 30 40 50

Accessing array elements after dynamic Initialization: 10 20 30 40 50

RESULT:
Thus the C program for access array elements using one dimensional array has been verified and
executed successfully.
EX 4B C PROGRAM FOR TWO DIMENSIONAL ARRAY

AIM:
To write a C program for access array elements using two dimensional array.

ALGORITHM:
Step 1: Start
Step 2:Initialize array name and array size for 2D declaration.
Step 3:Access the counter variables for the loop and display the array elements.
Step 4:Print the result.
Step 5:Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int disp[2][3];
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
Output:
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6

Two Dimensional array elements:


123
456
RESULT:

Thus a C program for access array elements using two dimensional array has been verified and executed
successfully.
EX 4C C PROGRAM FOR MULTI DIMENSIONAL ARRAY

AIM:
To write a C program for access array elements using Multi dimensional array.

ALGORITHM:
STEP 1: Start
STEP 2: Declare the 12 values entered by the user.
STEP 3: Displaying values using for loop
STEP 4: Printing values with proper
index STEP 5: Stop
PROGRAM:

#include
<stdio.h> int
main()
{
int i,j,k;
int test[2][3][2];

printf("Enter 12 values:

\n");

for (i = 0; i < 2; ++i)


{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
scanf("%d", &test[i][j][k]);
}
}
}

printf("\nDisplaying
values:\n"); for (i = 0; i < 2;
++i)
{
for(j = 0; j < 3; ++j)
{
for (k = 0; k < 2; ++k)
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}

return 0;
}
OUTPUT:

Enter 12 values:
1
20
5
05
05
5
8

8
8
8
8
8
8

8
8

8
8
Displaying values:
test[0][0][0] = 1
test[0][0][1] = 20
test[0][1][0] = 5
test[0][1][1] = 5
test[0][2][0] = 8
test[0][2][1] = 8
test[1][0][0] = 8
test[1][0][1] = 8
test[1][1][0] = 8
test[1][1][1] = 8
test[1][2][0] = 8
test[1][2][1] = 8

RESULT:

Thus the C program for access array elements using Multi dimensional array has been verified and
executed successfully.
EX 4D C PROGRAM TO DISPLAY ARRAY ELEMENTS USING
TRAVERSAL

AIM:
To write a C program to display array elements using traversal.

ALGORITHM:
Step 01: Start
Step 02: Declare and initialize array.
Step 03: Declare the size of array using sizeof(arr)
Step 04: Using for loop perform traversal on a array .
Step 05: Display the array elements.
Step 06: stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, size;
int arr[]={1, -9, 17, 4, -3};
size=sizeof(arr)/sizeof(arr[0]);
printf("The array elements are: ");
for(i=0;i<size;i++)
printf("\n arr[%d]= %d", i, arr[i]);
}
OUTPUT:

The array elements are:


arr[0]= 1
arr[1]= -9
arr[2]= 17
arr[3]= 4
arr[4]= -3

RESULT:
Thus the C program to display array elements using traversal has been executed and the output
was verified.
EX 5A C PROGRAM TO FIND STRING LENGTH

AIM:
To C program to find the length of the string.

ALGORITHM:
Step 1: Start the program

Step2: Get the string

Step 3: Find the length of the string

Step4:Display the length of the string

Step5:Stop the program

PROGRAM:

#include<stdio.h>

#include<conio.h>

int main()

int length;

char s[20] = “WELCOME TO CSE”;

length=strlen(s);

printf(“\Length of the string is = %d \n”, length);

return 0;

OUTPUT:

Length of the string is = 14

RESULT:
Thus the C program to find the length of the string has been verified and executed successfully .
EX5B
PROGRAMTOCOMPARE THE TWO STRINGS USING STRING
FUNCTION
AIM:

To write a c program to compare two strings using string functions.

ALGORITHM:
Step 1: Start the

program Step2: Get

the string1 Step

3:Get string2

Step 3: Compare both strings Step

4:If value is 0 then print string is

same.

Step5:else print string is not same

PROGRAM
1. #include <stdio.h>
2. #include<string.h>
3. int main()
4. {
5. char str1[20]; // declaration of char array
6. char str2[20]; // declaration of char array
7. int value; // declaration of integer variable
8. printf("Enter the first string : ");
9. scanf("%s",str1);
10. printf("Enter the second string : ");
11. scanf("%s",str2);
12. // comparing both the strings using strcmp() function
13. value=strcmp(str1,str2);
14. if(value==0)
15. printf("strings are same");
16. else
17. printf("strings are not same");
18. return 0;
19. }
OUTPUT:
Enter the first string: cse
Enter the second string:cse
strings are same

RESULT:

Thus the C program to compare two strings using string functions has been verified and executed
successfully.
EX 5C PROGRAM TO COPY CONTENT OF ONE STRING TO ANOTHER
STRING USING STRING FUNCTION.

AIM:
To program to copy content of one string to another string using string function.

ALGORITHM:
Step 1: Start the program. Step2: Get the string1.

Step 3: Initialize string2.

Step 4: Copy string1 to string2. Step 5: Display the string2.

Step6:Stop the program.

PROGRAM:

#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'c', 's', 'e', 'p', 'y', 't', 'h', 'o',’n’ '\0'};
char ch2[20];
strcpy(ch2,ch);
printf("Value of second string is: %s",ch2);
return 0;
}

OUTPUT:

Value of second string is cse python

RESULT:

Thus the C Program to copy content of one string to another string using string
function has been executed and the result was verified
EX 5D PROGRAM TO PERFORM STRING CONCATENATION

AIM: To write a C program to perform string concatenation.

ALGORITHM:
Step1:Start
Step2:Get the two Strings to be concatenated.
Step3: Declare a new String to store the concatenated String.

Step4: Insert the first string in the new string. Step5:Insert the second string in the new string.
Step6:Print the concatenated string.
Step7:Stop
PROGRAM:
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
5. char ch2[10]={'c', ‘s’,’e’,'\0'};
6. strcat(ch,ch2);
7. printf("Value of first string is: %s",ch);
8. return 0;
9. }

OUTPUT:
Value of first string is hello cse

RESULT:

Thus the C program to perform string concatenation has been verified and executed successfully.
EX 5E PROGRAM TO REVERSE THE STRING USING STRING OPERATION

AIM: To write a c program to reverse the string using string operation

ALGORITHM:
Step 1: Start the program

Step2: Get the string

Step 3:Reverse the string using strrev function.

Step4:Display

Step5:Stop.
PROGRAM:

1. #include<stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char str[20];
6. printf("Enter string: ");
7. gets(str);
8. printf("String is: %s",str);
9. printf("\nReverse String is: %s",strrev(str));
10. return 0;
11. }

OUTPUT:

Enter string : mookambigai


String is: mookambigai
Reverse String is: iagibmakoom

RESULT:

Thus the C program to reverse the string using string operation has been verified and executed successfully.
EX 6A PROGRAM TO SWAP TWO VALUES BY USING PASS BY VALUE

AIM: To write a c program to swap two values by using pass by value

ALGORITHM:

Step1: Take two numbers as input.

Step2: The values before calling the swap function swap(n1,n2)

Step 3: printing the value of a and b in main

Step 4:Print the value of actual parameters do not change by changing the formal parameters in call by value,

Step 5:stop.

PROGRAM:

1. #include <stdio.h>
2. void swap(int , int); //prototype of the function
3. int main()
4. {
5. int a = 10;
6. int b = 20;
7. printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and
b in main
8. swap(a,b);
9. printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do
not change by changing the formal parameters in call by value, a = 10, b = 20
10. }
11. void swap (int a, int b)
12. {
13. int temp;
14. temp = a;
15. a=b;
16. b=temp;
17. printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20,
b = 10

18. }

OUTPUT:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20

RESULT:
Thus the C program to swap two values by using pass by value has been verified and executed successfully.
EX6B PROGRAM TO SWAP TWO VALUES BY USING PASS BY REFERENCE

AIM: To write a c program to swap two values by using pass by reference


ALGORITHM:
Step 1: Declare prototype of the function. Step 2: Get two values as a ,b.
Step 3: Print the values of a , b in main
Step 4 :Print the actual parameters in call by reference Step 5:Stop.

PROGRAM:
#include <stdio.h>
void swap(int *, int *); //prototype of the function int main()
{
int a = 10; int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
.}
12. void swap (int *a, int *b)
13. {
14. int temp;
15. temp = *a;
16. *a=*b;
17. *b=temp;
18. printf("After swapping values in function a = %d, b = %d\n",*a,*b);
19. }

OUTPUT:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10

RESULT:
Thus the C program to swap two values by using pass by reference has been verified and executed
successfully.
EX 7 PROGRAM TO FIND THE NTH TERM OF FIBONACCI SERIES USING
RECURSION

AIM:
To write a C program to find the NTH term of Fibonacci series using recursion.

ALGORITHM:

Step1:Declare and initialize the variables


n, f. Step2: Get the value of n.
Step 3:Using Fibonacci function and display the Fibonacci
value Step4:if n is 0 the return 0 else return 1.
step 5:else return Fibonacci (n-1)+(n-2)
step6:stop

PROGRAM:
#include<stdio.h>
int fibonacci(int);
void main ()
{
int n,f;
printf("Enter the value of n"); scanf("%d",&n);
f = fibonacci(n); printf("%d",f);
}
int fibonacci (int n)
{
if (n==0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return fibonacci(n-1)+fibonacci(n-2);
}

OUTPUT:
Enter the value of n
12 144

RESULT:

Thus the C Program to find the NTH term of Fibonacci series using recursion has been executed and
verified successfully.
C PROGRAM FOR POINTERS TO
EX 8A FUNCTION

AIM: To write a c program for pointers to function.


ALGORITHM:

Step1:Start
Step2:Initialize the function with an int parameter.
Step3:Declare fun_ptr is a pointer to function fun() Step4:Invoking fun() using fun_ptr
Step5:Stop

PROGRAM:

#include <stdio.h> void fun(int a)


{
printf("Value of a is %d\n", a);
}
int main()
{
void (*fun_ptr)(int) = &fun; void (*fun_ptr)(int); fun_ptr = &fun; (*fun_ptr)(10);

return 0;
}

OUTPUT:

Value of a is 10

RESULT:
Thus the C Program to find the pointer to function has been executed and verified successfully.
EX 8B C PROGRAM FOR STRING POINTER

AIM: To write a C program for string pointer

ALGORITHM:
Step1:Initialize and declare variables.
Step2:Get a string
Step3:Use for loop & display each character address.
Step 4: Stop

PROGRAM

#include<stdio.h>
int main()
{
char str[6] = "Hello";
int i;
for(i = 0; str[i]; i++)
printf("&str[%d] = %p\n" ,i, str+i);
return0;
}

OUTPUT:

&str[0] =0x7fff7b7234f2
&str[1] =0x7fff7b7234f3
&str[2] =0x7fff7b7234f4
&str[3] =0x7fff7b7234f5
&str[4] =0x7fff7b7234f6

RESULT:

Thus the C Program to find the Sting to pointer has been executed and verified successfully.
EX 8C C PROGRAM FOR POINTERS TO POINTERS

AIM: To write a C program for pointers of pointers.

ALGORITHM:
Step1:Start the program.
Step2:Declare and initialize variables.
Step3:Initialize pointer 1,2 for variables
Step 4: Storing address of variable in ptr2
Step 5: Storing address of ptr2 in ptr1
Step 6: Displaying value of variable using
both single and double pointers.
Step 7:Stop the program.
PROGRAM:
#include <stdio.h>

int main()
{
int var = 789;
int *ptr2;
int **ptr1;
ptr2 = &var;
ptr1 = &ptr2;
printf("Value of var = %d\n", var );
printf("Value of var using single pointer = %d\n", *ptr2 );
printf("Value of var using double pointer = %d\n", **ptr1);

return 0;
}

OUTPUT:

Value of var = 789


Value of var using single pointer = 789
Value of var using double pointer = 789

RESULT:

Thus the C Program to find the pointer to pointer has been executed and verified successfully.
EX 8D
C PROGRAM FOR ARRAY OF POINTER

AIM: To write a C program for array of pointer.

ALGORITHM:
Step1:Initialize array name and size.
Step2:Declare the elements of array.
Step3:Use %p print the value of pointer address.

PROGRAM:

#include<stdio.h>
#define size 5

int main()
{
int *arr[size];
int a = 10, b = 20, c = 30, d = 40, e = 50, i;

arr[0] = &a;
arr[1] = &b;
arr[2] = &c;
arr[3] = &d;
arr[4] = &e;

printf("Address of a = %p\n",arr[0]);
printf("Address of b = %p\n",arr[1]);
printf("Address of c = %p\n",arr[2]);
printf("Address of d = %p\n",arr[3]);
printf("Address of e = %p\n",arr[4]);
for(i = 0; i < size; i++)
printf("value stored at arr[%d] = %d\n",i,*arr[i]);
return 0;
}

OUTPUT:

Address of a =0x7ffcd716b178
Address of b =0x7ffcd716b17c
Address of c =0x7ffcd716b180
Address of d =0x7ffcd716b184
Address of e =0x7ffcd716b188
value stored at arr[0] =10
value stored at arr[1] =20
value stored at arr[2] =30
value stored at arr[3] =40
value stored at arr[4] =50

RESULT:

Thus the C Program to find the array of pointer has been executed and verified successfully
EX 9A C PROGRAM FOR NESTED STRUCTURES

AIM: To write a c program for nested structures.

ALGORITHM:
Step1:Initialize the structure 1 and 2.
Step2: Combine the two structure.
Step3:Get the structure information
Step4:Display the structure
information Step5:stop the program

PROGRAM:
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
void main ()
{
struct employee emp;
printf("Enter employee information\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin,
emp.add.phone); printf("Printing the employee information \n");
printf("name: %s\nCity: %s\nPincode: %d\nPhone:
%s",emp.name,emp.add.city,emp.add.pin,emp.ad d.phone);
}
Output:
Enter employee information hardik
mumbai
121212
6786532567
Printing the employee information....
name: hardik
City: mumbai Pincode: 121212
Phone: 6786532567

RESULT:

Thus the C Program to find the nester structures has been executed and verified successfully
EX 9B C PROGRAM FOR POINTERS TO STRUCTURE

AIM:
To write a C program for pointers to structure

ALGORITHM:
STEP1:Declare the structure name as student.
STEP2:Initialize the variables.
STEP3:Get details of the student
STEP4:Print the result STEP5:Stop

PROGRAM:
#include<stdio.h>
struct student
{
int sno;
char
sname[30];
float marks;
};
main ( ){
struct student s;
struct student *st;
printf("enter sno, sname, marks:");
scanf ("%d%s%f", & s.sno, s.sname, &s. marks);
st = &s;
printf ("details of the student are");
printf ("Number = %d\n", st ->sno);
printf ("name = %s\n", st->sname);
printf ("marks =%f\n", st ->marks);
getch ( );
}

OUTPUT:
enter sno, sname, marks:3
Lucky 88 details of the student
are:
Number =
3 name =
Lucky
marks =88.000000

RESULT:

Thus the C Program to find the pointer to structures has been executed and verified successfully
EX 9C C PROGRAM FOR STRUCTURES AND
UNION

AIM:
To write a C program for structures and union.

ALGORITHM:
Step 01: Start
Step 02: Declaring structure and union.
Step 03: Creating variable for structure and initialize the values
Step 04: Creating variable for union and initialize the values

Step 05: Find the difference between structures and union.


Step 06: Difference for accessing one member at a time.
Step 07: Print the values.
Step 08: Stop

PROGRAM:

#include <stdio.h>
#include
<string.h>
struct struct_example
{
int integer;
float decimal;
char name[20];
};
union union_example
{
int integer;
float decimal;
char name[20];
};
void main()
{
struct struct_example stru ={5, 15, "John"};
union union_example uni = {5, 15, "John"};
printf("data of structure:\n integer: %d\n decimal: %.2f\n name: %s\n", stru.integer, stru.decimal, stru.name);
printf("\ndata of union:\n integer: %d\n" "decimal: %.2f\n name: %s\n", uni.integer, uni.decimal, uni.name);
printf("\nAccessing all members at a time:");
stru.integer = 163;
stru.decimal = 75;
strcpy(stru.name, "John");
printf("\ndata of structure:\n integer: %d\n " "decimal: %f\n name: %s\n", stru.integer, stru.decimal, stru.na
me);

uni.integer = 163;
uni.decimal = 75;
strcpy(uni.name, "John");
printf("\ndata of union:\n integeer: %d\n " "decimal: %f\n name: %s\n", uni.integer, uni.decimal, uni.name);

printf("\nAccessing one member at a time:");


printf("\ndata of structure:");
stru.integer = 140;
stru.decimal = 150;
strcpy(stru.name, "Mike");
printf("\ninteger: %d", stru.integer);
printf("\ndecimal: %f", stru.decimal);
printf("\nname: %s", stru.name);
printf("\ndata of union:");
uni.integer = 140;
uni.decimal = 150;
strcpy(uni.name, "Mike");
printf("\ninteger: %d", uni.integer);
printf("\ndecimal: %f", uni.decimal);
printf("\nname: %s", uni.name);
printf("\nAltering a member value:\n");
stru.integer = 512;
printf("data of structure:\n integer: %d\n decimal: %.2f\n name: %s\n", stru.integer, stru.decimal, stru.name);
uni.integer = 512;
printf("data of union:\n integer: %d\n decimal: %.2f\n name: %s\n", uni.integer, uni.decimal, uni.name);
printf("\nsize of structure: %d\n", sizeof(stru));
printf("size of union: %d\n", sizeof(uni));
}
OUTPUT:

data of structure:
integer: 5
decimal: 15.00
name: John

data of union:
integer: 5
decimal:0.00
name:John

Accessing all members at a time:


data of structure:
integer: 163
decimal:75.000000
name: John

data of union:
integer: 1852337994
decimal: 17983765624912253034071851008.000000
name: John

Accessing one member at a time:


data of structure:
integer: 140
decimal: 150.000000
name: Mike
data of union:
integer: 1701538125
decimal: 69481161252302940536832.000000
name: Mike
Altering a member value:
data of structure:
integer: 512
decimal: 150.00
name: Mike
data of union:
integer: 512
decimal: 0.00
name:Mike
size of tructure:28
size of union: 20

RESULT:

Thus the C program for structures and union has been executed and verified successfully.
EX 10A C PROGRAM FOR WRITING IN A FILE

AIM:
To write c program to writing a file.

ALGORITHM:
Step 1: Start
Step 2: Declare the file pointer
Step 3: Get the data to be written in file
Step 4: Open the existing file GfgTest.c using fopen() in write mode using "w" attribute Step
5: Check if this file Pointer is null which maybe if the file does not exist
Step 6: Write the data To Be Written into the file Step
7: Writing in the file using fputs()
Step 8: Closing the file using fclose() Step
9: Stop

PROGRAM:
# include <stdio.h>
# include <string.h>
int main( )
{
FILE *filePointer ;
char dataToBeWritten[50]
= "GeeksforGeeks-A Computer Science Portal for Geeks";
filePointer = fopen("GfgTest.c",
if ( filePointer == NULL )
{
printf( "GfgTest.c file failed to open." ) ;
}
else
printf("The file is now opened.\n")
// Write the dataToBeWritten into the file
if ( strlen ( dataToBeWritten ) > 0 )
{
// writing in the file using fputs()
fputs(dataToBeWritten, filePointer) ;
fputs("\n", filePointer) ;
} // Closing the file using fclose()
fclose(filePointer) ;
printf("Data successfully written in file GfgTest.c\n");
printf("The file is now closed.") ;
}
return 0;
}

OUTPUT:
The file is now opened.
Data successfully written in file GfgTest.c
The file is now closed.

RESULT:

Thus the C program for File open in write mode has been executed and verified successfully
EX 10B
C PROGRAM FOR FILE OPERATIONS READ A FILE
AIM:
To write a C program for file operations using read a file

ALGORITHM:
Step 1: Start
Step 2: Declare the file pointer
Step 3: Declare the variable for the data to be read from file
Step 4: Open the existing file GfgTest.c using fopen() in read mode using "r" attribute Step 5: Read
the data To Be Read from the file using fgets() method
Step 6: Closing the file using fclose()
Step 7: Stop

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Driver code
int main()
{
FILE* ptr;
char ch;
// Opening file in reading mode
ptr = fopen("test.txt", "r");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
// Printing what is written in file
// character by character using loop.
do {
ch = fgetc(ptr);
printf("%c", ch);
// Checking if character is not EOF.
// If it is EOF stop reading.
} while (ch != EOF);
// Closing the file
fclose(ptr);
return 0;
}

OUTPUT:
c:\Desktop\program\test.txt
c”\Desktop\program\test.txt
content
This is Programming in C

RESULT:

Thus the C program for File open in read mode has been executed and verified successfully.
C PROGRAM TO DEMONSTRATE RANDOM
EX 10C
ACCESS FILE

AIM:
To write a C program to demonstate random access file.

ALGORITHM:
Step1: Start
Step2: Declare the file pointer
Step 3: The file pointer points to the starting of the file, ftell() will return 0
Step 4: Here we traverse the entire file and print it's contents until we reach it's end. Step 5: Print Size of file in bytes
Step 6: Stop

PROGRAM:

#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen(“scaler.txt”, “r”);
if(!fp)
{
printf(“Error : File cannot be opened \n” );
return 0;
}
printf(“position pointer in the beginning : %d\n”, ftell(fp));
char ch;
while(fread(&ch,sizeof(ch),1,fp)==1)
{
printf(“%c”, ch);
}
printf(“\n Size of file in bytes is : %d” \nftell (fp));
fclose(fp);
return0;}

OUTPUT:

position pointer in the beginning : 0


Scaler is amazing
Size of file in bytes is : 17

RESULT:

Thus the C program for demonstrate random access file has been executed and verified successfully.
EX 10D
C PROGRAM TO DEMONSTRATE FILE PREPROCESSOR
DIRECTIVES

AIM:
To write a C program to demonstrate file preprocessor directives.

ALGORITHM:
Step1: Start
Step2: Declare the macro with parameter Step 3:
Initialize two values and define macro Step 4:
Calculate the area of rectangle
Step 4: Print the result. Step 5:
Stop

PROGRAM:

#include<stdio.h> #define
AREA(1,b)(1*b) int main()
{
int 11=10, 12=5, area;
area=AREA(11,12);
printf(“Area of rectangle is:%d”, area); return 0;
}

OUTPUT:
Area of rectangle is:50

RESULT:

Thus the C program to demonstrate file preprocessor directives has been verified and executed successfully

You might also like