C Record
C Record
BONAFIDE CERTIFICATE
Page
S. No Date Experiments Sign
No
PROGRAMS USING I/O STATEMENTS AND EXPRESSIONS
EX. No: 1
WRITE A C PROGRAM FOR AMSTRONG OR NOT
DATE :
AIM:
ALGORITHM:
Step 1: Start
Step 2: Read an integer input number.
Step 3: Declare andInitialise the variables current_digit, sum = 0, digits = 0 and
num = number.
Step 4: Calculate the number of digits in the input integer number and store it in the
variable number_of_digits.
Step 5: Repeat Steps 5 to 7 Until num > 0
Step 6: current_digit = (num % 10)
Step 7: sum = sum + mnumber_of_digits
Step 8: num = num / 10
Step 9: Check if sum == number
a.ThenPrint “It is an Armstrong Number.”
Else
b.Print “It is not an Armstrong Number.”
Step 10: Stop
PROGRAM:
#include <stdio.h>
#include<conio.h>
int main()
{
int number, originalNumber, remainder, result = 0;
clrscr();
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
getch();
}
RESULT
Thus the C program to amstrong or not using i/o statement and expressions is executed
and the result is verified.
.
DECISION-MAKING CONSTRUCTS: IF-ELSE, GOTO, SWITCH-CASE, BREAK-
CONTINUE
EX. No: 2a
WRITE A C PROGRAM TO CHECK LEAP YEAR USING IF ELSE
DATE : STATEMENT
AIM:
ALGORITHM
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
/* Input year from user */
printf("Enter year : ");
scanf("%d", &year);
{
printf("LEAP YEAR");
}
else
{
printf("COMMON YEAR");
}
getch();
}
RESULT
Thus the C program to check leap year using if else statement is executed and the
result is verified.
DECISION-MAKING CONSTRUCTS: IF-ELSE, GOTO, SWITCH-CASE, BREAK-
CONTINUE
EX. No: 2b
TO CHECK IF A NUMBER IS EVEN OR NOT USING GOTO
DATE :
AIM:
ALGORITHM
PROGRAM:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
void main()
int num;
printf("Enter a number\n");
scanf("%d", &num);
if (num % 2 == 0)
goto even;
else
goto odd;
even:
printf("%d is even\n",num);
exit(0);
odd:
getch();
RESULT
Thus the C program to check if a number is even or not using goto statement is
executed and the result is verified.
DECISION-MAKING CONSTRUCTS: IF-ELSE, GOTO, SWITCH-CASE, BREAK-
CONTINUE
EX. No: 2c
PROGRAM TO CHECK VOWEL OR CONSONANT USING SWITCH
DATE : CASE STATEMENT
AIM:
ALGORITHM:
Step 2: The program is asked the user to enter an Alphabets to check whether vowel or
consonant
Step 4: Define cases for the character ch with vowel character both capital and small
Step 5: Next, the program checks every case using the given character.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
/* Switch value of ch */
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
getch();
}
RESULT
EX. No: 2d
LINEAR SEARCH USING BREAK
DATE :
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
if (item == a[i])
{
printf("\nItem found at location %d", i+1);
break;
}
if (i > 9)
printf("\nItem does not exist.");
getch();
}
RESULT
Thus the C program to Linear search Using break statement is executed and the
result is verified.
DECISION-MAKING CONSTRUCTS: IF-ELSE, GOTO, SWITCH-CASE, BREAK-
CONTINUE
EX. No: 2e
PRINT THE NUMBERS FROM 1 TO 10 USING CONTINUE
DATE : STATEMENT
AIM:
ALGORITHM:
Step 1 : Start
3.1 : print the value of i , increment value of i by 1 as i=i+1 and goto step 3until
if(i==6)
Step 4 : Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for ( i = 1; i <= 10; i++)
{
if (i == 6)
continue;
else
// otherwise print the value of i
printf("%d ", i);
}
getch();
}
RESULT
Thus the C program to Print The Numbers From 1 To 10 Using continue statement
is executed and the result is verified.
LOOPS: FOR, WHILE, DO-WHILE
EX. No: 3a
DATE : FIBONACCI SERIES UP TO N TERMS USING FOR LOOP
AIM:
To write a C program to Fibonacci series up to n terms using for loop
ALGORITHM:
Step 1: Start
Step 2: Read the number of terms n
Step 3: Initialize t1 0, t2 1
Step 4: print a and b values
Step 5: for i 3 to n
a. increment the i value
b. nextTerm = t1 + t2;
c. print next term value
d. t1 t2
e. t2 next term
PROGRAM:
#include <stdio.h>
#include<conio.h>
int main()
{
int i, n;
return 0;
}
RESULT
Thus the C program to to Fibonacci series up to n terms using for loop is executed
and the result is verified.
LOOPS: FOR, WHILE, DO-WHILE
EX. No: 3b
TO CHECK WHETHER THE GIVEN INTEGER IS PALINDROME
DATE :
OR NOT USING WHILE LOOP
AIM:
To write a C program To check whether the given integer is Palindrome or not using
while loop
ALGORITHM:
Step 1: Start
Step 2: read num
Step 3: Set number num to a variable n
n ← num
Step 4: Iterate until num is not equal to 0.
If num value becomes 0, control comes out of the loop. So num’s original value is lost. So,
num,value is stored in other variable n in step 3.
In step 4, reverse of the number is calculated.
while ( num != 0) do
remainder ← num mod 10
num ← num/10
rev ← rev * 10 +remainder
Step 5: Print reverse number
print rev
Step 6: Check if original number & reverse number are same. If it is, number is palindrome.
Otherwise,
not palindrome
if (rev = n) then
print palindrome
else
print not a palindrome
endif
Step 7: End
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int temp,rev=0,num,remainder ;
clrscr();
printf("Enter the number\n");
scanf("%d",&num);
temp=num;
while(num!=0) //Reversing the number
{
remainder = num%10;
num = num/10;
rev = rev*10+ remainder;
}
printf("The reverse number is %d",rev);
if(rev == temp)
printf("\n%d is a palindrome",temp);
else
printf("\n%d is not a palindrome", temp);
getch();
}
RESULT
Thus the C program to To check whether the given integer is Palindrome or not using
while loop is executed and the result is verified.
LOOPS: FOR, WHILE, DO-WHILE
EX. No: 3c
AIM:
To write a C program to Print total value Using do-while Loop
ALGORITHM:
Step 1: start
Step2: enter any value below 10, and the total variable initialized to 0.
Step 3: User entered value will assign to the number variable, and then the number is added to the
total.
Step 4: In the next line, we used ++ operator to increment the number value.
Step 5: After this line, the value in a Number variable tests against the while condition. If the
condition results true, it repeats the process. Otherwise, it will exit from it.
Step 6: In the next line, we used one more printf statement to show that it is coming from outside
the while loop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int number, total=0;
printf(" Total Value from outside the Loop is: %d \n", total);
getch();
}
RESULT
Thus the C program to Print total value Using do-While Loop is executed and the
result is verified.
ARRAYS: 1D AND 2D, MULTI-DIMENSIONAL ARRAYS, TRAVERSAL
EX. No: 4a
C IMPLEMENTATION TO FIND TWO SMALLEST ELEMENTS
DATE : IN A ONE DIMENSIONAL ARRAY
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
int n;
printf("enter no of elements\n");
scanf("%d",&n);
int *a=(int*) malloc(sizeof(int)*n);
return 0;
}
RESULT
Thus the C program to to find find smallest elements in a one dimensional array is
executed and the result is verified.
ARRAYS: 1D AND 2D, MULTI-DIMENSIONAL ARRAYS, TRAVERSAL
Ex. No: 4b
C PROGRAM TO DISPLAY THE HIGHEST MARKS IN EACH
Date : SUBJECT USING TWO DIMENSIONAL ARRAY
AIM:
To write a C program to Display The Highest Marks in each Subject using two
Dimensional Array
ALGORITHM:
Step 1: Start
Step 2: Create the array
Step 3: Declare the array name and type
Step 4: enter the 5 students marks in three subjects and store the marks using for loop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int marks[5][5],i,j,max_marks;
clrscr();
for(i=0;i<5;i++)
{
printf("\n enter the marks obtained by student %d",i+1);
for(j=0;j<3;j++)
{
printf("\n marks[%d}{%d]=",i,j);
scanf("%d",&marks[i][j]);
}
}
for(j=0;j<3;j++)
{
max_marks=-999;
for(i=0;i<5;i++)
{
if(marks[i][j]>max_marks)
max_marks=marks[i][j];
}
printf("\n the highest marks obtained in the subject%d=%d",j+1,max_marks);
}
getch();
}
RESULT
Thus the C program to Display The Highest Marks in each Subject using Two
Dimensional Array is executed and the result is verified.
ARRAYS: 1D AND 2D, MULTI-DIMENSIONAL ARRAYS, TRAVERSAL
EX. No: 4c
C PROGRAM TO STORE AND PRINT THE VALUES USING MULTI -
DATE : DIMENSIONAL ARRAY
AIM:
To write a C program to store and print the values using multi -dimensional array.
ALGORITHM:
Step 1: Start
Step 2: Create the array
Step 3: Declared three variables I j k for three for loops
Step 4: Initialize the array size int arr[2][3][3];
Step 5: enter the values in the array and store the array value using for loop
Step 6: first for loop represents the block of a 3d array
Step 7: second for loop represents the number of rows
Step 8: Third for loop represents the number of columns.
Step 9: for loops will print the inserted values in the matrix form.
Step 10: stop the program
PROGRAM :
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k; //variables for nested for loops
scanf("%d",&arr[i][j][k]);
}
}
}
printf("printing the values in array: \n");
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
{
printf("%d ",arr[i][j][k]);
if(k==3)
{
printf("\n");
}
}
}
printf("\n");
}
return 0;
}
RESULT
Thus the C program to store and print the values using multi –dimensional
array is executed and the result is verified.
ARRAYS: 1D AND 2D, MULTI-DIMENSIONAL ARRAYS, TRAVERSAL
EX. No: 4d
ARRAYS TRAVERSAL
DATE :
AIM:
ALGORITHM:
Step 1: Start
Step 2: [Initialize counter variable. ] Set i = LB.
Step 3: Repeat for i = LB to UB.
Step 4: Apply process to arr[i].
Step 5: [End of loop. ]
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, size;
int arr[]={1, -9, 17, 4, -3};
clrscr();
size=sizeof(arr)/sizeof(arr[0]); //sizeof(arr) will give 20 and sizeof(arr[0]) will give 4
printf("The array elements are: ");
for(i=0;i<size;i++)
printf("\narr[%d]= %d", i, arr[i]);
getch();
}
RESULT
Thus the C program to Arrays traversal is executed and the result is verified.
STRINGS: OPERATIONS
EX. No: 5a
TO FIND THE LENGTH OF A STRING
DATE :
AIM:
ALGORITHM:
Step 1: Start
Step 2: declare the string size
Step 3: initialize len=0
Step 4 : entered the string
Step 5: Read the entered string using gets() function
Step 6: The function strlen()calculates the length of the string and the value stored on len
Step 7 : Print the length value.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[10];
int len=0;
clrscr();
printf("\n enter the string");
gets(str);
len=strlen(str);
printf("enter string:%s",str);
printf ("the length of the string is :%d",len);
getch();
}
RESULT
Thus the C program to find the length of a string is executed and the result is verified.
STRINGS: OPERATIONS
EX. NO: 5b
DATE : TO COMPARE TWO STRINGS
AIM:
ALGORITHM:
Step 1: Start
Step 2: declare the string size
Step 3: initialize the variable
Step 4: entered the first string
Step 5: Read the entered first string using gets() function
Step 6: entered the second string
Step 7: Read the entered second string using gets() function
Step 8: compare the strings by using the strcmp() the value stored on variable
Step 9: check the if condition if(value==0) If the function returns 0 value means that both
the strings are same, otherwise the strings are not equal.
Step 10: Print the Result
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str1[10],str2[20];
int value;
clrscr();
printf("\n enter the first string");
gets(str1);
printf("\n enter the second string");
gets(str2);
value=strcmp(str1,str2);
if(value==0)
printf ("\n strings are same");
else
printf("\n strings are not same");
getch();
RESULT
Thus the C program to compare two strings is executed and the result is verified.
STRINGS: OPERATIONS
EX. No: 5c
TO CONCATENATE TWO STRINGS
DATE :
AIM:
ALGORITHM:
Step 1: Start
Step 2: declare the string size
Step 3 : entered the first string
Step 4: Read the entered first string using gets() function
Step 5: entered the second string
Step 6: Read the entered second string using gets() function
Step 7: concatenate the strings by using the strcat() function
Step 8: the strings are combined together to form a single string.
Step 9: Print str 1
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
printf("\n enter the first string");
gets(str1);
printf("\n enter the second string");
gets(str2);
strcat(str1,str2);
printf("\n output string is :%s",str1);
getch();
}
RESULT
Thus the C program to concatenate two string is executed and the result is verified.
STRINGS: OPERATIONS
EX. No: 5d
DATE : COPY A STRING TO ANOTHER STRING
AIM:
ALGORITHM:
Step 1: Start
Step 2: declare the string size
Step 3: entered the first string
Step 4: Read the entered first string using gets() function
Step 7: copy a string to another string(str2) by using the strcpy() function
Step 8: finally print the value of both the variable
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[]="abc";
int str2[];
clrscr();
strcpy(str2,str1);
printf("\n%s",str2);
getch();
}
RESULT
Thus the C program to copy a string to another string is executed and the result is
verified.
FUNCTIONS: CALL, RETURN, PASSING PARAMETERS BY
(VALUE, REFERENCE), PASSING ARRAYS TO FUNCTION
EX. No: 6a
DATE : SUM OF TWO NUMBER USING FUNCTION CALL
AIM:
ALGORITHM:
Step 1: Start
Step 3: add two variable values and assign the result to the variable sum.
Step 5: stop
PROGRAM:
#include <stdio.h>
#include<conio.h>
int add(int a, int b);
void main()
{
int sum;
int a, b;
clrscr();
printf(" Enter the first and second number \n");
scanf("%d %d", &a, &b);
sum = add(a, b); // call add() function
printf( "The sum of the two number is %d", sum);
}
int add(int n1, int n2) // pass n1 and n2 parameter
{
int c;
c = n1 + n2;
return c;
RESULT
Thus the C program to sum of two number using function call is executed and
the result is verified.
FUNCTIONS: CALL, RETURN, PASSING PARAMETERS BY
(VALUE, REFERENCE), PASSING ARRAYS TO FUNCTION
EX. No: 6b
FACTORIAL OF A GIVEN NUMBER
DATE :
AIM:
ALGORITHM:
Step 1: Start
Step 7: else
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int fact(int);
int n,f;
clrscr();
f=fact(n);
getch();
int fact(int n)
if(n==1)
return(1);
else
return n*fact(n-1);
RESULT
Thus the C program to Factorial of a given number is executed and the result
is verified.
FUNCTIONS: CALL, RETURN, PASSING PARAMETERS BY
(VALUE, REFERENCE), PASSING ARRAYS TO FUNCTION
EX. No: 6c
SWAPPING NUMBERS USING CALL BY VALUE AND
DATE :
REFERENCE
AIM:
ALGORITHM 1:
Step 1: Start
Step 2: Set x=10 and y=20
Step 3: Call the function swap(&x,&y)
Step 3a: Start fuction
Step 3b: assign t= a
Step 3c: assign a= b
Step 3d: assign b= t
Step 3e: end function
Step 5: print the result
Step 6: Stop the program
ALGORITHM 2:
Step 1: Start
Step 2: Set x=10 and y=20
Step 3: Call the function swap(&a,&b)
Step 3a: Start fuction
Step 3b: assign *t=*a
Step 3c: assign *a=*b
Step 3d: assign *b=*t
Step 3e: end function
Step 5: print the result
Step 6: Stop the program
PROGRAM 1:
#include <stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int x = 10, y = 20;
printf (" x = %d, y = %d from main before calling the function", x, y);
swap(x,y);
printf( "\n x = %d, y = %d from main after calling the function", x, y);
getch();
}
void swap( int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf (" \n x = %d, y = %d from modular function", a, b);
PROGRAM 2:
#include <stdio.h>
#include<conio.h>
void swap(int*,int*);
void main()
{
int x = 10, y = 20;
printf (" x = %d, y = %d from main before calling the function", x, y);
swap(&x,&y);
printf( "\n x = %d, y = %d from main after calling the function", x, y);
getch();
}
void swap( int *a,int *b)
{
int *t;
*t=*a;
*a=*b;
*b=*t;
Thus the C program to Swapping numbers using Call by value and Call by Reference
is executed and the result is verified.
FUNCTIONS: CALL, RETURN, PASSING PARAMETERS BY
(VALUE, REFERENCE), PASSING ARRAYS TO FUNCTION
EX. No: 6d
TO CALCULATE THE SUM OF ARRAY ELEMENTS BY PASSING
DATE : TO A FUNCTION
AIM:
ALGORITHM:
Step 1: Start
Step 2: declared and create an num variable
Step 3: num array is passed to calculateSum()
Step 4: calculatesum(num)function call ,Call the function float
calculateSum(float num[])
Step 5: Assign float sum = 0.0;
Step6: to find the sum of all element using for loop and return the value
Step 7: print the result
PROGRAM :
#include <stdio.h>
#include<conio.h>
float calculateSum(float num[]);
int main()
{
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};
return sum;
}
RESULT
Thus the C program to calculate the sum of array elements by passing to a function is
executed and the result is verified.
RECURSION
EX. No: 7
DATE : BINARY SEARCH USING RECURSION
AIM:
ALGORITHM:
Step 1: Start
Step 5 : if middle > key value, call the function with end_value = middle - 1 .
Step 6 : if middle < key value, call the function with start_value = middle + 1 .
Step 7 : exit.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int binarysearch(int[],int,int,int);
void main()
{
int a[10],n,i,first,last,middle,search,position;
clrscr();
printf("enter number of element\n");
scanf("%d",&n);
printf("enter the element\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter value to be search");
scanf("%d",&search);
first=0;
last=n-1;
position=binarysearch(a,search,first,last);
if(position==-1)
printf("key not found");
else
printf("key found at position%d",position+1);
getch();
}
int binarysearch(int b[],int k,int l,int h)
{
int middle;
if(l>h)
return -1;
middle=(l+h)/2;
if(k==b[middle])
return(middle);
else if(k<b[middle])
binarysearch(b,k,l,middle-1);
else
binarysearch(b,k,middle+1,h);
return(middle);
}
RESULT
Thus the C program to Binary Search using Recursion is executed and the
result is verified.
POINTERS: POINTERS TO FUNCTIONS, ARRAYS,STRINGS,
POINTERS TO POINTERS, ARRAY OF POINTERS
EX. No: 8a
DATE : SALARY CALCULATION USING POINTER TO FUNCTION
AIM:
ALGORITHM:
Step 1: Start
Step 2: declared the variable
Step 3: enter the employee current salary
Step 4: enter the bonus
Step 5: Call the function void salaryhike(int *var, int b)
Step 6: calculate the salary
Step 7: print the final salary
PROGRAM:
#include<stdio.h>
#include<conio.h>
void salaryhike(int *var, int b)
{
*var = *var+b;
getch();
}
int main()
{
int salary=0, bonus=0;
clrscr();
printf("Enter the employee current salary:");
scanf("%d", &salary);
printf("Enter bonus:");
scanf("%d", &bonus);
salaryhike(&salary, bonus);
printf("Final salary: %d", salary);
return 0;
}
RESULT
EX. NO: 8b
C PROGRAM TO ACCESS ELEMENTS OF A 2-D ARRAY
DATE : USING A POINTER TO AN ARRAY.
AIM:
ALGORITHM:
Step 1: Start
Step 2: declare array size and assign to the value
Step 3: declare the pointer variable ptr to an array of 4 integers
Step 4: ptr contains the base address of array arr.
Step 5: print the pointer variable address and accessing the value
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[3][4] = {
{10, 11, 12, 13},
{20, 21, 22, 23},
{30, 31, 32, 33}
};
int (*ptr)[4];
ptr = arr;
printf("%p %p %p\n", ptr, ptr + 1, ptr + 2);
printf("%p %p %p\n", *ptr, *(ptr + 1), *(ptr + 2));
printf("%d %d %d\n", **ptr, *(*(ptr + 1) + 2), *(*(ptr + 2) + 3));
printf("%d %d %d\n", ptr[0][0], ptr[1][2], ptr[2][3]);
return 0;
}
RESULT
Thus the C program to access elements of a 2-d array using a pointer to an array
is executed and the result is verified.
POINTERS: POINTERS TO FUNCTIONS, ARRAYS,STRINGS, POINTERS
TO POINTERS, ARRAY OF POINTERS
EX. No: 8c
C PROGRAM DEMONSTRATING THE CONCEPT OF
AIM:
ALGORITHM:
Step 1: Start
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i;
char *a[5]={"One","Two","Three","Four","Five"};
printf("The values in every string location are : \n");
for(i=0;i<5;i++)
{
printf("%s\n",a[i]);
}
printf("The address locations of every string values are : \n");
for(i=0;i<5;i++)
printf("%d\n",a[i]);
}
}
RESULT
EX. No: 8d
C PROGRAM TO STORE THE ADRESS OF ANOTHER
POINTER AND ACCESSING THE VALUE STORED AT
DATE :
THE POINTER
AIM:
To write a C program to store the adress of another pointer and accessing the
value stored at the pointer
ALGORITHM:
Step 1: Start
Step 8: print the value stored at the address contained by the pointer stored at pp
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
clrscr();
p = &a;
pp = &p;
printf("address of a: %x\n",p);
printf("address of p: %x\n",pp);
printf("value stored at p: %d\n",*p);
printf("value stored at pp: %d\n",**pp);
getch();
}
RESULT
Thus the C program to store the address of another pointer and accessing the
value stored at the pointer is executed and the result is verified.
POINTERS: POINTERS TO FUNCTIONS, ARRAYS, STRINGS,
POINTERS TO POINTERS, ARRAY OF POINTERS
EX. No: 8e
DEVELOP A PROGRAM USING POINTERS TO COMPUTE THE
SUM, MEAN, AND STANDARD DEVIATION OF ALL ELEMENTS
DATE : STORED IN AN ARRAY OF N REAL NUMBERS.
AIM:
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: For every value of n read the x
Step 4: Initialize sum=0 and i=0
Step 5: For every value of n and i, comuter sum using sum = sum + (*(x+i) – mean) * ( *
( x+i) – mean)
Step 6: Using the sum value computer variance = sum / n and deviation = sqrt (variance )
Step 7: Display mean, variance, deviation
Step 8: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n , i;
float x[20],sum,mean;
float variance , deviation;
printf("Enter the value of n \n");
scanf("%d",&n);
printf("enter %d real values \n",n);
for (i=0;i<n;i++)
{
scanf("%f",(x+i));
}
sum=0;
for(i=0;i<n;i++)
{
sum= sum+*(x+i);
}
printf("sum=%f\n",sum);
mean=sum/n;
sum=0;
for(i=0;i<n;i++)
{
sum=sum+(*(x+i)-mean)*(*(x+i)-mean);
}
variance=sum/n;
deviation=sqrt(variance);
printf("mean(Average)=%f\n",mean);
printf("variance=%f\n",variance);
printf("standard deviation=%f\n",deviation);
}
RESULT
EX. No: 9a
DATE : PRINT THE STUDENT DETAILS USING NESTED STRUCTURES
AIM:
ALGORITHM:
Step 1: Start
Step 4: enter the class name and student details using for loop
Step 6: stop
PROGRAM:
#include <stdio.h>
#include<conio.h>
//student structure declaration
struct student
{
char name[50];
int roll;
};
int main()
{
int i,n;
char temp;
struct class cls;
for(i=0;i<n;i++)
{
printf("Enter student [%d] roll no.: ",i+1);
scanf("%d",&cls.std[i].roll);
printf("Enter student [%d] name: ",i+1);
scanf("%c",&temp); //read last CR (new line) character and ignore
fgets(cls.std[i].name,50,stdin);
}
//print details
printf("\nDetails are:\n");
printf("CLASS NAME: %s\n",cls.className);
for(i=0;i<n;i++)
printf("student[%d]:
%05d\t%s\n",i+1,cls.std[i].roll,cls.std[i].name);
return 0;
RESULT
Thus the C program to print the student details using nested structure is executed
and the result is verified
STRUCTURES: NESTED STRUCTURES, POINTERS TO STRUCTURES,
ARRAYS OF STRUCTURES AND UNIONS
EX. No: 9b
PRINT THE EMPLOYEE DETAILS USING POINTER TO
DATE : STRUCTURES
AIM:
ALGORITHM:
Step 1: Start
Step 7: stop
PROGRAM:
#include <stdio.h>
#include<conio.h>
struct employee
{
char name[100];
int age;
float salary;
char department[50];
};
int main()
{
struct employee employee_one, *ptr;
printf("Enter Name, Age, Salary and Department of Employee\n");
scanf("%s %d %f %s", &employee_one.name, &employee_one.age,
&employee_one.salary, &employee_one.department);
ptr = &employee_one;
printf("\nEmployee Details\n");
printf(" Name : %s\n Age : %d\n Salary = %f\n Dept : %s\n",
ptr->name, ptr->age, ptr->salary, ptr->department);
return 0;
}
RESULT
Thus the C program to print the employee details using pointer to structures
is executed and the result is verified
STRUCTURES: NESTED STRUCTURES, POINTERS TO STRUCTURES,
ARRAYS OF STRUCTURES AND UNIONS
EX. No: 9c
ARRAYS OF STRUCTURES AND UNIONS
DATE :
AIM:
ALGORITHM:
Step 1: Start
Step 6: Stop
ALGORITHM:
UNION AND INTERSECTION OF TWO SORTED ARRAYS
Step 1: Start
Step 4: If arr1[i] is smaller than arr2[j] then print arr1[i] and increment i.
Step 5: If arr1[i] is greater than arr2[j] then print arr2[j] and increment j.
Step 6: If both are same then print any of them and increment both i and j.
#include<stdio.h>
#include<conio.h>
/* Declaration of structure */
struct student
{
char name[30];
int roll;
float marks;
};
int main()
{
/* Declaration of array of structure */
struct student s[3];
int i;
clrscr();
for(i=0;i< 3;i++)
{
printf("Enter name, roll and marks of student:\n");
scanf("%s%d%f",s[i].name, &s[i].roll, &s[i].marks);
}
printf("student marks details are:\n");
for(i=0;i< 3;i++)
{
printf("Name: %s\n",s[i].name);
printf("Roll: %d\n", s[i].roll);
printf("Marks: %f\n\n", s[i].marks);
}
return 0;
}
PROGRAM:
UNION AND INTERSECTION OF TWO SORTED ARRAYS
#include<stdio.h>
#include<conio.h>
{
int arr1[] = {11, 12, 14, 15,16};
int arr2[] = {12, 13, 15, 17};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
printUnion(arr1, arr2, m, n);
getchar();
return 0;
}
RESULT
Thus the C program to using arrays of structures and unions is executed and the
result is verified
FILES: READING AND WRITING, FILE POINTERS, FILE OPERATIONS,
RANDOM ACCESS, PROCESSOR DIRECTIVES
AIM:
ALGORITHM:
Step 1: Start
PROGRAM:
}
printf("File created successfully.");
/*writting into file*/
putc('A',fp);
putc('B',fp);
putc('C',fp);
printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));
fclose(fp);
return 0;
RESULT
AIM:
To write a C program to find the number of characters in the given text files using
file pointers
ALGORITHM:
Step 1: Start
Step 6: Repeat all these steps till all the chatacter from the files has been read.
Step 7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int noc = 0;
fp = fopen("test.txt", "r");
while(1)
{
ch = fgetc(fp);
if(ch == EOF)
{
break;
}
noc++;
}
fclose(fp);
printf("\nNumber of characters in file: %d", noc);
}
RESULT
Thus the C program to find the number of characters in the given text files using
file pointers is executed and the result is verified
FILES: READING AND WRITING, FILE POINTERS, FILE
OPERATIONS, RANDOM ACCESS, PROCESSOR DIRECTIVES
AIM:
ALGORITHM:
Step 1: Begin
Step 9: Declare a integer variable result and initialize it with the output of the ftell()
function.
PROGRAM:
#include <stdio.h>
#include<conio.h>
int findfileSize(char f_n[])
{
FILE* fp = fopen(f_n, "r"); // opening a file in read mode
RESULT
Thus the C program to find size of a file using file operations is executed and
the result is verified.
FILES: READING AND WRITING, FILE POINTERS, FILE
OPERATIONS, RANDOM ACCESS, PROCESSOR DIRECTIVES
AIM:
To write a C program to save alphabet a to z in file and then print the letters using
random access
ALGORITHM:
Step 1: Start
Step 7: open file for reading mode and read all character in the file
#include <stdio.h>
#include<conio.h>
int main(void)
{
// integer variable
int i;
// character variable
char ch;
// file pointer
FILE *fptr;
if (fptr != NULL) {
printf("File created successfully!\n");
}
else {
printf("Failed to create the file.\n");
// exit status for OS that an error occured
return -1;
}
// close connection
fclose(fptr);
// reference
printf("\nReference:\n");
for (i = 0; i < 26; i++)
{
printf("%d ", (i+1));
}
printf("\n");
for (i = 65; i <= 90; i++)
{
// print character
printf("%c", (i));
// manage space
if (i - 65 >= 9)
{
printf(" ");
}
else {
printf(" ");
}
}
printf("\n\n");
fseek(fptr, 0, 0);
ch = getc(fptr);
// rewind
printf("rewind\n");
rewind(fptr);
// close connection
fclose(fptr);
return 0;
}
RESULT
Thus the C program to save alphabet a to z in file and then print the letters using
random access is executed and the result is verified.
FILES: READING AND WRITING, FILE POINTERS, FILE
OPERATIONS, RANDOM ACCESS, PROCESSOR DIRECTIVES
AIM:
ALGORITHM:
Step 1 : Start
Step 2 : define marks
Step 3 : #if macro_condition
statements
#endif macro_condition
Statements
#If condition
statements
#elif condition
Statement
#else
statements
#endif
PROGRAM:
#include <stdio.h>
#include<conio.h>
#define MARKS 90
int main()
{
#ifdef MARKS
printf("MARKS macro has been defined \n");
#endif
#if MARKS >90
printf("Student has scored GRADE A");
#elif MARKS >60
printf("Student has scored GRADE B");
#else
printf("Student has scored GRADE C");
#endif
return 0;
}
RESULT
Thus the C program to check student scored Grade using preprocessor directive
is executed and the result is verified.