[go: up one dir, main page]

100% found this document useful (1 vote)
1K views40 pages

C Record CORRECTED

The document contains a C program to read values into a two dimensional array. The program begins by declaring a two dimensional array. It then reads values into the array. Finally, it prints the elements of the array to display the output. The program demonstrates the use of a two dimensional array in C.

Uploaded by

MR Kishore
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
100% found this document useful (1 vote)
1K views40 pages

C Record CORRECTED

The document contains a C program to read values into a two dimensional array. The program begins by declaring a two dimensional array. It then reads values into the array. Finally, it prints the elements of the array to display the output. The program demonstrates the use of a two dimensional array in C.

Uploaded by

MR Kishore
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/ 40

Ex.

No: 1 I/O STATEMENTS, OPERATORS, EXPRESSIONS

AIM:
To write a program for I/O statements, operators, expressions using C.

ALGORITHM:
STEP1:Start the program.
STEP2:Declare the variables totamt,amount,subtot,distant,taxamt,quantity,value,discount,tax.
STEP3:Read the values for the variables.
STEP4:Perform addition, subtraction and multiplication.
STEP5:Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
Void main()
{
float totamt,amount,subtot,distant,taxamt,quantity,value,discount,tax;
printf("\nenter the quantity of item sold:");
scanf("%f",&quantity);
printf("\n enter the value of item:");
scanf("%f",&value);
printf("\n enter the discount percentage:");
scanf("%f",&discount);
printf("\n enter the tax:");
scanf("%f",&tax);
amount=quantity*value;
distant=(amount*discount)/100.0;
subtot=amount-distant;
taxamt=(subtot*tax)/100.0;
totamt=subtot+taxamt;
printf("\n\n\n******bill******");
printf("\n quantity sold:%f",quantity);
printf("\n priceperitem:%f",value);
printf("\n amount %f",amount);
printf("\n discount:%f",distant);
printf("\n discounted total: %f",subtot);
printf("\n tax:+ %f",taxamt);
printf("\n total amount %f",totamt); getch();
}

1
OUTPUT:
Enter the quantity of item sold:10
Enter the value of item:50
Enter the discount percentage:10
Enter the tax:10
Quantity sold:10.00
Price per item:50.00
Amount:500.00
Discount:50.00
Discounted total:450.00
Tax:+45
Total amount:495.00

RESULT:
Thus the program for I/O statements, operators and expressions are executed
successfully.

2
DECISION-MAKING CONSTRUCTS: IF-ELSE, GOTO,
Ex.No: 2
SWITCH-CASE, BREAK-CONTINUE

AIM:
To write a Program to check whether a person is eligible to Vote or Not using IF-ELSE.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables.
STEP3:Read the values for the variables.
STEP4:Check age is greater than or equal to 18, if true display eligible to vote otherwise display
not eligible to vote.
STEP5:Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[50];//or*name
printf("\n Type the name of the candidate:");
gets(name);
printf("\n Enter the age:");
scanf("%d",&age);
if(age>=18)
printf("\n %s is Eligibile for vote",name);
else
printf("\n %s is not Eligibile for vote",name);
getch();
}

OUTPUT:
Type the name of the candidate:
Ramu
Enter the age:
25
Ramu is eligible to vote

RESULT:
Thus the program for if-else using C is executed successfully.

3
Ex.No: 2 GOTO
AIM:

Write a program in C using goto.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables.
STEP3:Read the values for the variables.
STEP4:Check for I less than or equal to 10, if not print the values of multiplication table .
STEP5:Stop the program.
PROGRAM:

#include <stdio.h>
#include<conio.h>
Void main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table; getch();
}

OUTPUT:

Enter the number whose table you want to print?


6
6x1=6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

RESULT:
Thus the program for goto is executed successfully.

4
Ex.No: 2 SWITCH-CASE
AIM:

Write a program in C using switch-case statement.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables.
STEP3:Read the values for the variables.
STEP4:Check for the value in day matches with the case value, if matches print the day .
STEP5:Stop the program.

PROGRAM:

#include <stdio.h>
#include<conio.h>
Void main() {
int day ;
printf("Enter the day of week");
scanf("%d",&day);
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break; getch();
}}

5
OUTPUT:

Enter the day of week


2
Tuesday

RESULT:

Thus the program for switch-case using C is executed successfully.

6
Ex.No: 2 BREAK
AIM:

Write a program in C using break.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables.
STEP3:Read the values for the variables.
STEP4:Check for I equal to five, if true break .
STEP5:Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
if(i == 5)
break;
printf("%d ",i);
}
printf("came outside of loop i = %d",i);
getch();
}

OUTPUT:
1
2
3
4
came outside of loop i = 5

RESULT:

Thus the program for break using C is executed successfully.

7
Ex.No: 2 CONTINUE
AIM:

Write a program in C using continue.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables.
STEP3:Read the values for the variables.
STEP4:Check for I equal to five, if true skip the next statements and execute the next iteration .
STEP5:Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main(){
int i=1;
for(i=1;i<=10;i++){
if(i==5){
continue;
}
printf("%d \n",i);
}end of for loop
getch();
}

OUTPUT:
1
2
3
4
6
7
8
9
10

RESULT:
Thus the program for continue is executed successfully.

8
Ex.No: 3 LOOPS: FOR, WHILE, DO-WHILE

AIM:
Write a program in C using FOR loop.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables.
STEP3:Read the values for the variables.
STEP4:Find the sum of n numbers and print the sum using loop.
STEP5:Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int Number, i, Sum = 0;
printf("\nPlease Enter any Integer Value\n");
scanf("%d", &Number);
for(i = 1; i <= Number; i++)
{
Sum = Sum + i;
}
printf("Sum of Natural Numbers = %d", Sum);
getch();
}

OUTPUT:
Please Enter any Integer Value
5
Sum of Natural Numbers =15

RESULT:
Thus the program to find sum of n numbers using for loop is executed successfully.

9
Ex.No: 3 WHILE
AIM:
Write a program in C using while loop.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables.
STEP3:Read the values for the variables.
STEP4:Assign the entered number to temp.
STEP5:Divide the number by 10 and get the reminder. Calculate sum=sum+(r*r*r).
STEP6:Check for sum equal to temp. If so display as Armstrong number.
STEP7:Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
getch();
}

OUTPUT:
Enter the number
407
Armstrong number

RESULT:
Thus the program to find Armstrong number using while loop is executed successfully.

10
Ex.No: 3 DO-WHILE
AIM:
Write a program in C using do-while loop.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables.
STEP3:Read the values for the variables.
STEP4:Display 0 and 1.
STEP5:Calculate n3=n1+n2.
STEP6:Assign n2 to n1 and n3 to n2.
STEP7:Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n1=0,n2=1,n3,i=2,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
do
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
i=1+1;
} while(i<=number);
getch();
}
OUTPUT:
Enter the number of elements:5

0
1
1
2
3
5
8
RESULT:
Thus the program for Fibonacci series using do-while is executed successfully.
11
ARRAYS: 1D AND 2D, MULTI-DIMENSIONAL
Ex.No: 4
ARRAYS, TRAVERSAL

AIM:
Write a program in C using one dimensional array.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the array.
STEP3:Read the values in the array.
STEP4:Compare first two numbers in array.
STEP5:If first number is greater than second number, swap the values.
STEP6:Continue until all the numbers are arranged in ascending order.
STEP7:Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
getch();
}

12
OUTPUT:

10, 9, 7, 101, 23, 44, 12, 78, 34, 23


Printing sorted element list
7,9,10,12,23,23,34,44,78,101

RESULT:
Thus the program to arrange numbers in ascending order using one dimensional array is executed
successfully.

13
Ex.No: 4 TWO DIMENSIONAL ARRAY

AIM:
Write a program in C using two dimensional array.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the array.
STEP3:Read the values in two array.
STEP4:Do matrix addition, subtraction,multiplication,division,modules.
STEP5:Print the output.
STEP6:Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, rows, columns, a[10][10], b[10][10];
int Addition[10][10], Subtraction[10][10], Module[10][10];
float Division[10][10];

printf("\nPlease Enter Number of rows and columns\n");


scanf("%d %d", &i, &j);

printf("\nPlease Enter the First Array Elements\n");


for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
printf("\nPlease Enter the Second Array Elements\n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &b[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{

14
for(columns = 0;columns < j;columns++)
{
Addition[rows][columns] = a[rows][columns] + b[rows][columns];
Subtraction[rows][columns] = a[rows][columns] - b[rows][columns];
Division[rows][columns] = a[rows][columns] / b[rows][columns];
Module[rows][columns] = a[rows][columns] % b[rows][columns];
}
}
printf("\nAdd\t Sub\t Div\t Mod");
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
printf("\n%d \t ", Addition[rows][columns]);
printf("%d \t ", Subtraction[rows][columns]);
printf("%.2f \t ", Division[rows][columns]);
printf("%d \t ", Module[rows][columns]);
}
}
getch();
}

Enter first matrix


33
33
Enter second matrix
22
22
Matrix addition
55
55
Matrix subtraction
11
11
Matrix multiplication
12 12
12 12

RESULT:
Thus the program for two dimensional array using C is executed successfully.

15
Ex.No: 4 MULTI-DIMENSIONAL ARRAY

AIM:
Write a program in C using multi dimensional array.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the array.
STEP3:Read the values in array.
STEP4:Display the values in array.
STEP5:Print the output.
STEP6:Stop the program.

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
int arr[3][3][3]= { { {11, 12, 13}, {14, 15, 16}, {17, 18, 19} }, { {21, 22, 23}, {24, 25, 26}, {27,
28, 29} }, { {31, 32, 33}, {34, 35, 36}, {37, 38, 39} }, };
clrscr();
printf(":::3D Array Elements:::\n\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++) {
for(k=0;k<3;k++) {
printf("%d\t",arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
getch();
}
OUTPUT:

3D Array Elements
{11, 12, 13}, {14, 15, 16}, {17, 18, 19} }, { {21, 22, 23}, {24, 25, 26}, {27, 28, 29} }, { {31, 32,
33}, {34, 35, 36}, {37, 38, 39}

RESULT:
Thus the program for multi dimensional array is executed successfully.

16
Ex.No: 5 STRINGS OPERATIONS

AIM:
Write a program in C for string operations.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the string.
STEP3:Read the value in string.
STEP4:Do operations like string concatenation, find length, string comparision and string copy.
STEP5:Print the output.
STEP6:Stop the program.

PROGRAM:
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main( )
{
char source[ ] = " fresh2refresh" ;
char target[ ]= " C tutorial" ;
printf ( "\nSource string = %s", source ) ;
printf ( "\nTarget string = %s", target ) ;
strcat ( target, source ) ;
printf ( "\nTarget string after strcat( ) = %s", target ) ;
getch();
}
OUTPUT:

Target string after strcat( ) =fresh2refresh ctutorial

Copying

#include <stdio.h>
#include <string.h>
#include<conio.h>
void main( )
{
char source[ ] = "fresh2refresh" ;
char target[20]= "" ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;

17
strcpy ( target, source ) ;
printf ( "\ntarget string after strcpy( ) = %s", target ) ;
getch();
}
OUTPUT:

Fresh2refresh

Comparing

#include <stdio.h>
#include <string.h>
#include<conio.h>
int main( )
{
char str1[ ] = "fresh" ;
char str2[ ] = "refresh" ;
int i, j, k ;
i = strcmp ( str1, "fresh" ) ;
j = strcmp ( str1, str2 ) ;
k = strcmp ( str1, "f" ) ;
printf ( "\n%d %d %d", i, j, k ) ;
getch();
}
OUTPUT:

0 -1 1

String Length

#include <stdio.h>
#include <string.h>
#include<conio.h>
void main( )
{
int len;
char array[20]="fresh2refresh.com" ;
len = strlen(array) ;
printf ( "\string length = %d \n" , len ) ;
getch();
}

18
OUTPUT:

string length =17

RESULT:

Thus the program for string operations are executed successfully.

19
FUNCTIONS: CALL, RETURN, PASSING
Ex.No: 6 PARAMETERS BY (VALUE, REFERENCE),
PASSING ARRAYS TO FUNCTION

AIM:
Write a program in C for function call and return.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the vriables.
STEP3:Read the value of variables.
STEP4:Call the function to do two numbers.
STEP5:Return the result to main program and print the output.
STEP6:Stop the program.

PROGRAM:

#include <stdio.h>
#include<conio.h>
int addNumbers(int a, int b); // function prototype

void main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
getch();
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
OUTPUT:

Enters two numbers 10 5


15

RESULT:
Thus the program to add two numbers using function is executed successfully.

20
PASSING PARAMETERS BY (VALUE,
Ex.No: 6
REFERENCE)

AIM:
Write a program in C for function call and return.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the vriables.
STEP3:Read the value of variables.
STEP4:Call the function to do two numbers.
STEP5:Return the result to main program and print the output.
STEP6:Stop the program.

PROGRAM:

#include <stdio.h>
#include<conio.h>
int sum (int n);
void main()
{
int a = 5;
printf("\n The value of 'a' before the calling function is = %d", a);
a = sum(a);
printf("\n The value of 'a' after calling the function is = %d", a);
getch();
}
int sum (int n)
{
n = n + 20;
printf("\n Value of 'n' in the called function is = %d", n);
return n;
}

OUTPUT:

The value of 'a' before the calling function is =5


The value of 'a' after calling the function is =25

21
Call by Reference

#include <stdio.h>
#include<conio.h>
int sum (int *n);
void main()
{
int a = 5;
printf("\n The value of 'a' before the calling function is = %d", a);
sum(&a);
printf("\n The value of 'a' after calling the function is = %d", a);
getch();
}
int sum (int *n)
{
*n = *n + 20;
printf("\n value of 'n' in the called function is = %d", n);
}
OUTPUT:

The value of 'a' before the calling function is =5


The value of 'a' after calling the function is =25

RESULT:

Thus the program for call by value and call by reference using function is executed successfully.

22
PASSING ARRAY TO FUNCTION
Ex.No: 6

AIM:
Write a program in C for passing array to function.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the vriable and array.
STEP3:Read the value of variables and array.
STEP4:Call the function find minimum numbers.
STEP5:Return the result to main program and print the output.
STEP6:Stop the program.
PROGRAM:

#include<stdio.h>
#include<conio.h>
int minarray(int arr[],int size){
int min=arr[0];
int i=0;
for(i=1;i<size;i++){
if(min>arr[i]){
min=arr[i];
}
}//end of for
return min;
}//end of function

void main(){
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};//declaration of array
min=minarray(numbers,6);//passing array with size
printf("minimum number is %d \n",min);
getch();
}
OUTPUT:
minimum number is 3

RESULT:
Thus the program to find minimum number using passing array to function is executed
successfully.

23
Ex.No: 7 RECURSION

AIM:
Write a program in C for recursion.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables.
STEP3:Read the value of variables.
STEP4:Find factorial by calling the function recursivily.
STEP5:Return the result to main program and print the output.
STEP6:Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
int find_factorial(int);
void main()
{
int num, fact;
//Ask user for the input and store it in num
printf("\nEnter any integer number:");
scanf("%d",&num);
//Calling our user defined function
fact =find_factorial(num);
//Displaying factorial of input number
printf("\nfactorial of %d is: %d",num, fact);
getch();
}
int find_factorial(int n)
{
//Factorial of 0 is 1
if(n==0)
return(1);
//Function calling itself: recursion
return(n*find_factorial(n-1));
}
OUTPUT:
factorial of 5 is: 120
8.Pointer for Function
RESULT:
Thus the program to find factorial of a number using recursion is executed successfully.

24
POINTERS: POINTERS TO FUNCTIONS,
Ex.No: 8
ARRAYS,STRINGS,
POINTERS TO POINTERS, ARRAY OF POINTERS
AIM:
Write a program in C for pointers to function.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the vriables.
STEP3:Read the value of variables.
STEP4:Call the function to do two numbers.
STEP5:Return the result to main program and print the output.
STEP6:Stop the program.
PROGRAM:

#include <stdio.h>
#include<conio.h>
int add(int,int);
void main()
{
int a,b;
int (*ip)(int,int);
int result;
printf("Enter the values of a and b : ");
scanf("%d %d",&a,&b);
ip=add;
result=(*ip)(a,b);
printf("Value after addition is : %d",result);
getch();
}
int add(int a,int b)
{
int c=a+b;
return c;
}
OUTPUT:
Enter the values of a and b : 25 40
Value after addition is : 65

RESULT:
Thus the program for pointer to function using C is executed successfully.

25
Ex.No: 8 POINTERS TO ARRAYS

AIM:
Write a program in C for pointers to arrays.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables and array.
STEP3:Read the value of variables.
STEP4:Using the address of array find the balance.
STEP5:Print the output.
STEP6:Stop the program.

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main () {
/* an array with 5 elements */
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;
int i;
p = balance;
/* output each array element's value */
printf( "Array values using pointer\n");
for ( i = 0; i < 5; i++ ) {
printf("*(p + %d) : %f\n", i, *(p + i) );
}
printf( "Array values using balance as address\n");
for ( i = 0; i < 5; i++ ) {
printf("*(balance + %d) : %f\n", i, *(balance + i) );
}
getch();
}
OUTPUT:
Array values using pointer
1000.0, 2.0, 3.4, 17.0, 50.0
Array values using balance as address
1000.0, 2.0, 3.4, 17.0, 50.0

RESULT:

Thus the program for pointer to array is executed successfully.

26
Ex.No: 8 POINTERS TO STRING

AIM:
Write a program in C for pointers to string.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables and array.
STEP3:Read the value of variables.
STEP4:Using the pointer print the students name.
STEP5:Print the output.
STEP6:Stop the program.

PROGRAM:

#include <stdio.h>
#include <String.h>
#include<conio.h>
void main(){
//initializing the pointer string array
char *students[]={"bhanu","ramu","hari","pinky",};
int i,j,a;
printf("The names of students are:\n");
for(i=0 ;i<4 ;i++ )
printf("%s\n",students[i]);
getch();
}
OUTPUT:

Bhanu,ramu,hari,pinky

RESULT:

Thus the program for pointer to array is executed successfully.

27
Ex.No: 8 POINTER TO POINTER

AIM:
Write a program in C for pointer to pointer.

ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables and pointer variable.
STEP3:Read the value of variables.
STEP4:Using the pointer print the value and address of variables.
STEP5:Print the output.
STEP6:Stop the program.

PROGRAM:

#include <stdio.h>
#include<conio.h>
void main()
{
int num=123;

//A normal pointer pr2


int *pr2;

//This pointer pr2 is a double pointer


int **pr1;

/* Assigning the address of variable num to the


* pointer pr2
*/
pr2 = #

/* Assigning the address of pointer pr2 to the


* pointer-to-pointer pr1
*/
pr1 = &pr2;

/* Possible ways to find value of variable num*/


printf("\n Value of num is: %d", num);
printf("\n Value of num using pr2 is: %d", *pr2);

28
printf("\n Value of num using pr1 is: %d", **pr1);
/*Possible ways to find address of num*/
printf("\n Address of num is: %p", &num);
printf("\n Address of num using pr2 is: %p", pr2);
printf("\n Address of num using pr1 is: %p", *pr1);

/*Find value of pointer*/


printf("\n Value of Pointer pr2 is: %p", pr2);
printf("\n Value of Pointer pr2 using pr1 is: %p", *pr1);
/*Ways to find address of pointer*/
printf("\n Address of Pointer pr2 is:%p",&pr2);
printf("\n Address of Pointer pr2 using pr1 is:%p",pr1);
/*Double pointer value and address*/
printf("\n Value of Pointer pr1 is:%p",pr1);
printf("\n Address of Pointer pr1 is:%p",&pr1);
getch();
}
OUTPUT:

Value of num is: 123


Value of num using pr2 is: 123
Value of num using pr1 is: 123
Address of num is: 0x7ffcf8f1f61cAddress of num using pr2 is: 0x7ffcf8f1f61c
Address of num using pr1 is: 0x7ffcf8f1f61c
Value of Pointer pr2 is: 0x7ffcf8f1f61c
Value of Pointer pr2 using pr1 is: 0x7ffcf8f1f61c
Address of Pointer pr2 is:0x7ffcf8f1f610
Address of Pointer pr2 using pr1 is:0x7ffcf8f1f610
Value of Pointer pr1 is:0x7ffcf8f1f610
Address of Pointer pr1 is:0x7ffcf8f1f608

RESULT:

Thus the program from pointer to pointer is executed successfully.

29
Ex.No: 9 STRUCTURES: NESTED STRUCTURES, POINTERS
TO STRUCTURES, ARRAYS OF STRUCTURES AND UNIONS

AIM:
Write a program in C for nested structures, pointers to structures, arrays of structures and unions.
ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables and pointer variable.
STEP3:Read the value of variables.
STEP4:Using the structure do process.
STEP5:Print the output.
STEP6:Stop the program.

PROGRAM:

#include <stdio.h>
#include <string.h>
#include<conio.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
void main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;

30
//printing first employee information
printf( "employee id : %d\n", e1.id);
printf( "employee name : %s\n", e1.name);
printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.y
yyy);
getch();
}

OUTPUT:
Sonoo jaisol
6/6/2004

RESULT:
Thus the program for structure is executed successfully.

31
Ex.No: 9 POINTERS TO STRUCTURES

AIM:
Write a program in C for pointers to structures.
ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables and pointer variable.
STEP3:Read the value of variables.
STEP4:Using the structure do process.
STEP5:Print the output.
STEP6:Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct person{
int age;
float weight;
};
void main(){
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Enter weight: ");
scanf("%f", &personPtr->weight);
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
getch();
}

OUTPUT:
Enter age: 25
Enterweight:50.3
Age 25
Weight 50.3

RESULT:
Thus the program for pointers to structure is executed successfully.

32
Ex.No: 9 ARRAY OF STRUCTURES

AIM:
Write a program in C for array of structures.
ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables and structure.
STEP3:Read the value of variables.
STEP4:Using the structure do process.
STEP5:Print the output.
STEP6:Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
void main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
getch();
}

33
OUTPUT:
Student information list
1 rosy
2 john
3 raju
4 jessy
5 amutha

RESULT:
Thus the program for array of structures is executed successfully.

34
Ex.No: 9 ARRAY OF UNION

AIM:
Write a program in C for array of UNION.
ALGORITHM:

STEP1:Start the program.


STEP2:Declare the variables and union.
STEP3:Read the value of variables.
STEP4:Using the union do process.
STEP5:Print the output.
STEP6:Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
//declaration of student array.
union student s[3];

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


{
printf("\nEnter name of student: ");
gets(s[i].name);//taking name of student
printf("\nEnter the student roll no:");
scanf("%d",&s[i].rollno);//taking student rollno
printf("\nstudent division:");
scanf("%c",s[i].div);//taking student division
}

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


{
printf("\nstudent roll no and Name is %d and %s and studies in %c
division",s[i].rollno,s[i].name,s[i].div);
}
getch();
}

35
OUTPUT:

Enternameofstudent: Roy
Enterthestudentrollno: 1
studentdivision: A

Enternameofstudent: Jason
Enterthestudentrollno: 2
studentdivision: B

Enternameofstudent: Tom
Enterthestudentrollno: 3
student division: A

student roll no and Name is 1 and Roy and studies in A division


student roll no and Name is 2 and Jason and studies in B division
student roll no and Name is 3 and Tom and studies in A division

RESULT:
Thus the program for array of union is executed successfully.

36
Ex.No: 10 FILES: READING AND WRITING, FILE POINTERS, FILE
OPERATIONS, RANDOM ACCESS, PROCESSOR DIRECTIVES

AIM:
Write a program in C for files reading and writing, file pointers, file operations, random access,
processor directives.

Reading a file

fgetc(file_pointer): It returns the next character from the file pointed to by the file pointer.
When the end of the file has been reached, the EOF is sent back.

fgets(buffer, n, file_pointer): It reads n-1 characters from the file and stores the string in a
buffer in which the NULL character ‘\0’ is appended as the last character.

fscanf(file_pointer, conversion_specifiers, variable_adresses): It is used to parse and analyze


data. It reads characters from the file and assigns the input to a list of variable pointers
variable_adresses using conversion specifiers. Keep in mind that as with scanf, fscanf stops
reading a string when space or newline is encountered.

PROGRAM:

#include <stdio.h>
int main()
{
FILE * file_pointer;
char buffer[30], c;
file_pointer = fopen("fprintf_test.txt", "r");
printf("----read a line----\n");
fgets(buffer, 50, file_pointer);
printf("%s\n", buffer);
printf("----read and parse data----\n");
file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer
char str1[10], str2[2], str3[20], str4[2];
fscanf(file_pointer, "%s %s %s %s", str1, str2, str3, str4);
printf("Read String1 |%s|\n", str1);
printf("Read String2 |%s|\n", str2);
printf("Read String3 |%s|\n", str3);
printf("Read String4 |%s|\n", str4);
printf("----read the entire file----\n");
file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer
while ((c = getc(file_pointer)) != EOF) printf("%c", c);

37
fclose(file_pointer);
return 0;
}

Writing to file
The above program writes a single character into the fputc_test.txt file until it reaches the next
line symbol “\n” which indicates that the sentence was successfully written. The process is to
take each character of the array and write it into the file.
#include <stdio.h>
int main() {
int i;
FILE * fptr;
char fn[50];
char str[] = "Guru99 Rocks\n";
fptr = fopen("fputc_test.txt", "w"); // "w" defines "writing mode"
for (i = 0; str[i] != '\n'; i++) {
/* write to file using fputc() function */
fputc(str[i], fptr);
}
fclose(fptr);
return 0;
}
Random Access in File
fseek() function is used to take file pointer to a particular position data file.
Syntax: fseek(Fpointer, Position, Initial);
Fpointer is name of file pointer variable.

Position number of characters to be jumped.

Initial specifies the position from where file pointer should be jumped. It can three
values :
0: From the beginning of file.

1: Current position.
2: End of file
#include<stdio.h>
int main()
{
FILE *f1;
char line[80] , s;
int n;
f1=fopen(“data.txt”,”r”);
fgets(line,80,f1);
printf(“%s”,line);
fseek(f1,2,0);
/*File pointer jumped to 3rd character from beginning*/

38
s=getc(f); /*5th Character read from file*/
printf(“\n%c”,s);

fseek(f1,2,1);
/*File pointer jumped to 3rd character from current
position*/
s=getc(f);
printf(“\n%c”,s);
fseek(f1,0,2);
/*File pointer jumped end of file* /
s=getc(f);
printf(“\n%c”,s);
fclose(f);
return(0);
}

Preprocessor Directive
The #error preprocessor directive indicates error. The compiler gives fatal error
if #error directive is found and skips further compilation process.

#include<stdio.h>
#ifndef __MATH_H
#error First include then compile
#else
void main(){
float a;
a=sqrt(7);
printf("%f",a);
}
#endif

RESULT:
Thus the program in C for files reading and writing, file pointers, file operations, random access,
processor directives is executed successfully.

39
40

You might also like