Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

EX .

NO:1A C PROGRAM USING I/O STATEMENTS

DATE:

AIM:

ALGORITHM:

STEP 1: Start

STEP 2: Declare and initialize the variables for a,b, c.

STEP 3: Display addition of numbers


STEP 4: End

PROGRAM:

#include<stdio.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:
EX .NO:1B PROGRAM TO PERFORM THE CALCULATOR OPERATIONS,

DATE: NAMELY, ADDITION, SUBTRACTION, MULTIPLICATION,


DIVISION AND SQUARE OF A NUMBER AND EXPRESSIONS

AIM:

ALGORITHM:

STEP 1 : Start the program

STEP 2 : Declare the functions

STEP 3: Get the input values

STEP 4: Perform the operations using functions

STEP5: Print the results

STEP 6: Stop the program

PROGRAM:

#include<stdio.h>
int add(int n1,int n2);
int subtract(int n1, int n2);
int multiply(int n1,int n2);
int divide(int n1,int n2);
int square(int n1);
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));
return 0;
}
int add(int n1,int n2)
{
int result;
result=n1+n2;
return result;
}
int subtract(int n1,int n2)
{
int result;
result = n1 - n2;
return result;
}
int multiply(int n1,int n2)
{
int result;
result=n1*n2;
return result;
}
int divide(int n1,int n2)
{
int result;
result = n1 / n2;
return result;
}
int square(int n1)
{
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:
EX .NO:2A C PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS LEAP

DATE: YEAR OR NOT USING IF-ELSE.

AIM:

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 go to step 7.
STEP 6: Print the result as “It is not a leap year”
STEP 7: Stop the program.

PROGRAM:

#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return 0;
}

OUTPUT:
Enter a year: 2004 2004 is a leap year

RESULT:
EX .NO: 2B
C PROGRAM TO PRINT MULTIPLICATION TABLE USING GOTO
DARE STATEMENT

AIM:

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>
int 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<=16)
goto table;
}
OUTPUT:
Enter the number whose table you want to print?5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11 = 55
5 x 12 = 60
5 x 13 = 65
5 x 14 = 70
5 x 15 = 75
5 x 16 = 80

RESULT:
EX NO:2C C PROGRAM TO CHECK WHETHER THE ENTERED CHARACTER IS
VOWEL OR NOT (USE SWITCH CASE)
DATE:

AIM:

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>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
//condition to check character is alphabet or
if((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 a VOWEL.\n",ch);

break;
default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}
return 0;
}

OUTPUT:

Enter a character: 1
1 is not an alphabet.

RESULT:
EX NO: 2D C PROGRAM USING BREAK STATEMENT

DATE:

AIM:

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>
int main ()
{
int a =10;
while( a <20)
{
printf("value of a: %d\n", a);
a++;
if( a >15)
{
break;
}
}

return 0;
}

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:
EX NO: 2E C PROGRAM USING CONTINUE STATEMENT

DATE:

AIM:

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);
return 0;
}
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:
EX NO: 3A C PROGRAM USING FOR LOOP

DATE:

AIM:

ALGORITHM:

STEP 1: Start
STEP 2: Declare and initialize the local variables

STEP 3: Then declare execution of for loops

STEP 4: Print the result

STEP 5: 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 :
EX NO: 3B C PROGRAM USING WHILE LOOP

DATE:

AIM:

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:
EX NO: 3C C PROGRAM USING DO-WHILE LOOP

DATE:

AIM:

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:

.
EX NO: 4A C PROGRAM FOR ACCESS ARRAY ELEMENTS
USING ONE DIMENSIONAL ARRAY
DATE:

AIM:

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:
EX.NO: 4B C PROGRAM FOR TWO DIMENSIONAL ARRAY

DATE:

AIM:

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:
EX.NO:4C C PROGRAM FOR MULTI DIMENSIONAL ARRAY

DATE:

AIM:

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:
EX.NO: 4D C PROGRAM TO DISPLAY ARRAY ELEMENTS USING
TRAVERSAL
DATE:

AIM:

ALGORITHM:
STEP 01: Start
STEP 02: Declare and initialize array.
STEP 03: Declare the size of array using size of(array)

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:
EX.NO: 5A C PROGRAM TO FIND STRING LENGTH

DATE:

AIM:

ALGORITHM:

STEP 1: Start the program


STEP 2: Get the string
STEP 3: Find the length of string
STEP 4: Display length of string
STEP 5: 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:
EX.NO:5B
PROGRAMTOCOMPARE THE TWO STRINGS USING STRING
DATE: FUNCTION

AIM:

ALGORITHM:

STEP 1: Start the program


STEP2: Get the string1 &
string2
STEP 3: Compare both strings
STEP4: If value is 0 then print string is same.
STEP5: Else print string is not same

PROGRAM
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
int value; // declaration of integer variable
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
// comparing both the strings using strcmp() function
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("strings are not same");
return 0;
}

OUTPUT:
Enter the first string: cse
Enter the second string:cse strings are same

RESULT:
EX .NO: 5C PROGRAM TO COPY CONTENT OF ONE STRING TO ANOTHER
STRING USING STRING FUNCTION.
DATE:

AIM:

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.
STEP 6: 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:
EX.NO: 5D PROGRAM TO PERFORM STRING CONCATENATION

DATE

AIM:

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:
#include<stdio.h>
#include <string.h>
int main(){
char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
char ch2[10]={'c', ‘s’,’e’,'\0'};
strcat(ch,ch2);
printf("Value of first string is: %s",ch);
return 0;
}

OUTPUT:
Value of first string is hello cse

RESULT:
EX.NO:5E PROGRAM TO REVERSE THE STRING USING STRING OPERATION

DATE:

AIM:

ALGORITHM:

STEP 1: Start the program


STEP2: Get the string
STEP 3:Reverse the string using strrev function.
STEP4:Display
STEP5:Stop.

PROGRAM:

#include<stdio.h>
#include <string.h>
int main()
{
char str[20];
printf("Enter string: ");
gets(str);
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
return 0;
}

OUTPUT:

Enter string : mookambigai


String is: mookambigai
Reverse String is: iagibmakoom

RESULT:
EX.NO: 6A PROGRAM TO SWAP TWO VALUES BY USING PASS BY VALUE

AIM:

ALGORITHM:

STEP 1: Take two numbers as input.

STEP 2: 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:
#include<stdio.h>
int main()
{
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);

// value of first is assigned to temp


temp = first;

// value of second is assigned to first


first = second;

// value of temp (initial value of first) is assigned to second


second = temp;

// %.2lf displays number up to 2 decimal points


printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}
OUTPUT:
Enter first number: 20
Enter second number: 10
After swapping, first number = 10.00
After swapping, second number = 20.00

RESULT:
EX.NO:6
PROGRAM TO SWAP TWO VALUES BY USING PASS BY REFERENCE
DATE:
AIM:

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 *);
void main( )
{
int n1,n2;
printf("Enter the two numbers to be swapped\n");
scanf("%d%d",&n1,&n2);
swap(&n1,&n2);
}
void swap(int *n1,int *n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
printf("\n After swapping are n1=%d n2=%d",*n1,*n2);
}

OUTPUT:

Enter the two numbers to be swapped


3 17
After swapping are n1=17 n2=3

RESULT:
EX.NO: 7 PROGRAM TO FIND THE NTH TERM OF FIBONACCI SERIES USING
RECURSION
DATE:

AIM:

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:
EX.NO:8A
C PROGRAM FOR POINTERS TO
DATE: FUNCTION
AIM:

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the function with an int parameter.
STEP 3: Declare fun_ptr is a pointer to function fun()
STEP 4: Invoking fun() using fun_ptr
STEP 5: Stop

PROGRAM:

#include <stdio.h>
void fun(int a)
{
printf("Value of a is %d\n", a);
}
int main()
{
void (*fun_ptr)(int) = &fun;
(*fun_ptr)(10);
return 0;

OUTPUT:

Value of a is 10

RESULT:
EX.NO: 8B
C PROGRAM FOR STRING POINTER
DATE:

AIM:

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);
return 0;
}

OUTPUT:

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

RESULT:
EX.NO: 8C C PROGRAM FOR POINTERS TO
POINTERS
DATE:

AIM:

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:

.
EX.NO: 8D C PROGRAM FOR ARRAY OF POINTER
DATE:

AIM:

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 =0x7ffcd716b18
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:
EX.NO: 9A
C PROGRAM FOR NESTED STRUCTURES
DATE:

AIM:

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 student_college_detail
{
int college_id;
char college_name[50];
};
struct student_detail
{
int id;
char name[20];
float percentage;
struct student_college_detail clg_data;
}
stu_data;
int main()
{
struct student_detail stu_data = {1, "Raju", 90.5, 71145,
"Anna University"};
printf(" Id is: %d \n", stu_data.id);
printf(" Name is: %s \n", stu_data.name);
printf(" Percentage is: %f \n\n", stu_data.percentage);
printf(" College Id is: %d \n", stu_data.clg_data.college_id);
printf(" College Name is: %s \n", stu_data.clg_data.college_name);
return 0;
}
OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 90.500000

College Id is: 71145


College Name is: Anna University

RESULT:
EX.NO: 9B

DATE: C PROGRAM FOR POINTERS TO STRUCTURE

AIM:

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:
EX.NO: 9C C PROGRAM FOR STRUCTURES AND UNION

DATE:

AIM:

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:
EX.NO:10A

DATE: C PROGRAM FOR WRITING IN A FILE

AIM:

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:
EX.NO: 10B
C PROGRAM FOR FILE OPERATIONS READ A FILE
DATE:

AIM:

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:
EX.NO:10C C PROGRAM TO DEMONSTRATE RANDOM

DATE: ACCESS FILE


AIM:

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:
EX .NO:10D C PROGRAM TO DEMONSTRATE FILE PREPROCESSOR
DIRECTIVES
DATE:

AIM:

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 5: 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:

You might also like