0% found this document useful (0 votes)
46 views56 pages

C Basic Programs by Sanaa Salam

The document is an index listing 25 experiments with programming concepts and problems. Each entry includes the date, name of the program or problem, page number, and signature of the staff in charge. Topics covered include calculations, control structures, arrays, matrices, strings, file handling and more. The index provides an overview of a series of programming experiments intended to teach various computational concepts and algorithms.

Uploaded by

Sanaa Salam
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)
46 views56 pages

C Basic Programs by Sanaa Salam

The document is an index listing 25 experiments with programming concepts and problems. Each entry includes the date, name of the program or problem, page number, and signature of the staff in charge. Topics covered include calculations, control structures, arrays, matrices, strings, file handling and more. The index provides an overview of a series of programming experiments intended to teach various computational concepts and algorithms.

Uploaded by

Sanaa Salam
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/ 56

INDEX

Sl.No Date Name of the Program Page Signature of


No Staff in Charge
1 13.10.20 Sum and average of three numbers 1
2 20.10.20 Largest among three numbers (using nested if) 3

3 23.10.20 Electricity charge calculation using if-else ladder 5


4 27.10.20 Simulate a simple calculator (using switch) 8
5 30.10.20 Factorial of a number using while loop 10
6 03.11.20 Perfect number or not 12
7 06.11.20 Fibonacci series (​do...while loop) 14
8 10.11.20 First 'n' prime numbers (for loop) 16
9 13.11.20 Sum of the digits of a number 18
10 17.11.20 Implement break and continue 20
11 20.11.20 Search a key value in an array 22
12 24.11.20 Array sorting 24
13 26.11.20 Transpose of a matrix 27
14 01.12.20 Product of 2 matrices 29
15 04.12.20 String operations using built in​ functions 32
16 11.12.20 Count the number of words in a text 34
17 18.12.20 Palindrome String or not 36
18 22.12.20 Swapping 2 numbers using Call by value and Call 38
by reference

19 05.01.21 Factorial using Recursion 40


20 08.01.21 Display student details using structure 42

12.01.21 Display employee details using​ union 44

22 19.01.21 Implement​ Command Line Arguments 46


23 22.01.21 Display address and contents of variables using 48
pointers

24 25.01.21 Sum of array elements using​ pointers 51


25 29.01.21 File Program 53
Exp No: ​1 ​Date​:13-10-2020

SUM AND AVERAGE​.

AIM:
Write a program to find the sum and average of three numbers.

PROGRAM​:

#include <stdio.h>
#include <conio.h>

main()
{
int a,b,c,sum,average;

clrscr();

printf("Enter the three numbers:\n");

scanf("%d%d%d%",&a,&b,&c);

sum=a+b+c;

average=(a+b+c)/3;

printf("\nsum=%d\n average=%d",sum,average);

getch();

return 0;
}

1
OUTPUT​:

​Enter the three numbers:


1
2
3
Sum=6
Average=2

​RESULT​:
​The program is compiled. Average of three numbers is shown successfully

2
Exp No​:2 ​ Date​:20-10-20

LARGEST AMONG THREE NUMBERS

AIM​:
Write a program to print the greatest among three numbers.

PROGRAM​:

#include <stdio.h>
#include <conio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter the values of a,b,c");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("\n a is the biggest");
}
else
{
printf("\n c i the biggest");
}
}
else if(b>c)
{
printf("\n b is the biggest");
}
else
{
printf("\n c is biggest");
}
getch();
return 0;
} ​3
OUTPUT​:
Enter the values of a,b,c
3
4
5

c is biggest

RESULT​:
The program compiled. Biggest among three numbers is shown successfully.

4
Exp No:​3 ​Date:​23-10-20
ELECTRICITY CHARGE CALCULATION USING IF ELSE LADDER

AIM:
an electricity power distribution company changes its domestic consumers as
follows:

Consumption unit Rate of charge


0-200 Rs 0.25 per unit

201-400 Rs 100+RS0.65 Per excess of 200

401-600 Rs 230+Rs 0.80 per unit excess of


400 Rs 390+Rs 1.00 per unit excess of
600 & above 600

PROGRAM​:

#include <stdio.h>
#include <conio.h>
main()
{
int a;float amount; ​5
printf("Enter the consumption unit:");
scanf("%d",&a);
if(a>=0 && a<=200)
amount=a*0.25;
else if(a>200 && a<400)
amount=100+((a-200)*0.65);
else if (a>400 && a<=600)
amount=230+((a-400)*0.80);
else if (a>600)
amount=390+((a-600)*1.00);
printf("amount to be paid=%f",amount);
getch();
return 0;
}

OUTPUT​:
Enter the consumption unit:200
amount to be paid=100.000000

RESULT​:
Program compiled. Amount to be paid is shown successfully.

7
Exp No:​4 ​ Date:​27-10-20

SIMULATE A SIMPLE CALCULATOR USING SWITCH

AIM​:

calculator WAP to simulate a simple calculator.

PROGRAM​:
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,result;
char op;clrscr();
printf("Enter an expression:\n");
scanf("%d%c%d",&a,&op,&b);
switch(op)
{
case'+':
result=a+b;
break;
case'-':
result=a-b;
break;
case'*':
result=a*b;
break;
case'/':
result=a/b;
break;
}
printf("result=%d\n",result);
getch();
return 0;}

​OUTPUT :
Enter an expression:
56+4
result=60

RESULT​:
Program compiled. Operations are done in the simple calculator successfully.

9
Exp No:​5​ Date:​30-10-20
FACTORIAL OF A NUMBER USING WHILE LOOP

AIM:

write a program in c to print the factorial of a number

PROGRAM​:

#include <stdio.h>
#include <conio.h>

main()
{
int i,n,f;
clrscr();
f=1;
i=1;
printf("enter the value of n:\n");
scanf("%d",&n);
while(i<=n)
{
f=f*i;
i=i+1;
}
printf("factorial=%d\n",f);
getch();
return 0;
}

10
OUTPUT​:

enter the value of n:


3
factorial=6

RESULT​:
The program was compiled and the factorial of number is shown successfully.

11
Exp No:​6​ Date​:3-11-20
PERFECT NUMBER OR NOT

AIM:
Write a program in c to check whether a number is perfect or not

PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
int i,n,sum=0;
clrscr();
printf("enter a number:\n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
printf("\n%d is a perfect number",n);
else
printf("\n%d is not a perfect number",n);
getch();
return 0;
}

12
OUTPUT​:
enter a number:
6

6 is a perfect number

​RESULT​:
The program was compiled .The entered number is successfully showed whether perfect or not

13
Exp No:​7​ Date​:6-11-20

FIBONACCI SERIES

AIM​:
Write a program in c to generate fibonacci series(do...while loop)

PROGRAM​:
#include <stdio.h>
#include <conio.h>
main()
{
int n,i,t1,t2,nextterm;
i=1;
t1=0;
t2=1;
nextterm=0;
clrscr();
printf("enter the nubmer of terms:\n");
scanf("%d",&n);
printf("fibonacci series:\n");
do
{
i++;
printf("%d\n",nextterm);
nextterm=t1+t2;
t1=t2;
t2=nextterm;
}
while(i<=n);
getch();
return 0;
}

14
OUTPUT​:
enter the number of terms:
10
fibonacci series:
0
1
2
3
5
8
13
21
34
55

RESULT​:
The program was compiled. Fibonacci series is shown successfully.

15
Exp No:8 Date:​10-11-20

FIRST ‘n' PRIME NUMBERS


AIM:
write a program to print the prime numbers from 1 to n

PROGRAM​:
#include <stdio.h>
#include <conio.h>
main()
{
int i,j,n,flag;
clrscr();
printf("enter the value of n:");
scanf("%d",&n);
printf("all prime numbers between 1 and n are:\n");
for(i=2;i<=n;i++)
{
flag=0;
for(j=1;j<=n;j++)
{
if(i%j==0)
flag++;
}
if(flag==2)
printf("%d\n",i);
}
getch();
return 0;
}

16
OUTPUT​:
enter the value of n:20
all prime numbers between 1 and n are:
2
3
5
7
11
13
17
19

RESULT​:
The program was compiled and the prime numbers from 1 to n was printed successfully.

17
Exp No:​9​ Date​:9-11-20

SUM OF THE DIGITS OF A NUMBER


AIM​:
Write a program in c to print the sum of the digits of a number.

PROGRAM
#include <stdio.h>
#include <conio.h>
main()
{
int n,sum,m;
clrscr();
sum=0;
printf("enter a number:\n");
scanf("%d",&n);
while(n!=0)
{
m=n%10;
n=n/10;
sum=sum+m;
}
printf("sum of digits=%d\n",sum);
getch();
return 0;
}

18
OUTPUT​
enter a number:
45
sum of digits=9

RESULT​:
The program was compiled. The sum of digits of the number is shown successfully.

19
Exp No:​10​ Date​:17-11-20

IMPLEMENT BREAK AND CONTINUE

AIM:
Write a program to implement break and continue.

PROGRAM​:
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int count,negative;
double n,sqroot;
clrscr();
printf("\nenter 999 to stop\n");
count=0;
negative=0;
while(count<=100)
{
printf("\nenter a number\n");
scanf("%lf",&n);
if(n==999)
break;
if(n<0)
{
printf("number is negative\n");
negative++;
continue;
}
sqroot=sqrt(n);
printf("n=%lf\n square root of %lf=%lf\n\n",n,n,sqroot);
count++;
}
printf("\n\n\nnubmer ofsquareroots found=%d\n",count);
printf("\n\nno.of Negative numbers found=%d\n",negative);
printf("END OF DATA\n");

getch();
return 0;
}
20
OUTPUT​:

enter 999 to stop

enter a number
25
n=25.000000
square root of 5.000000=0.000000

enter a number
-25

number is negative

enter a number
999

nubmer ofsquareroots found=1

no.of Negative numbers found=1


END OF DATA

RESULT​:

The programs were compiled. Break and continue statements were implemented
successfully.

21
Exp No​:11 ​Date​:20-11-2020

SEARCH A KEY VALUE IN AN ARRAY

AIM​:
Write a program to searching of a key value in an array

PROGRAM​:-
#include <stdio.h>
#include <conio.h>
#define SIZE 100
int main()
{
int arr[SIZE];
int size,i,n,found;
clrscr();
printf("enter the size of array:");
scanf("%d",&size);
printf("enter elements in array:");
for (i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
printf("\nenter element to search:");
scanf("%d",&n);
found=0;
for(i=0;i<size;i++)
{
if(arr[i]==n)
{
found=1;
break;
}
}
if(found==1)
{
printf("\n%d is found at position %d",n,i+1);
}
else
{
printf("\n%d is not found in the array",n);
}
getch();
return 0;}

22
OUTPUT​:-
enter the size of array:5
enter elements in array:10
15
20
25
30

enter element to search:25

25 is found at position 4

RESULT​:-
The program was compiled .the position of searched element is shown
successfully.

23
Exp No​:12 ​Date​:24-11-2020

SORTING AN ARRAY

AIM​:
Write a program to sort an array

PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,temp,n,arr[30];
clrscr();
crintf(“enter the size of array:\n”);
scanf(“%d”,&n);
printf(“enter the elements of array:”);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;} ​24
}
}
printf(“the numbers arranged in ascending order is given below:-\n”);
for(i=0;i<n;i++)
printf(“%d\n”,arr[i]);
getch();
}

25
OUTPUT​:

Enter the size of array:


8
Enter the numbers:12
122
2
111
3
5
56
43
The numbers arranged in ascending order is given below:-
2
3
5
12
43
56
111
122

RESULT​:
The program was compiled and the elements of array is sorted in ascending order and shown
successfully.

26
Exp No:​13 ​Date:​26-11- 2020

TRANSPOSE 0F A MATRIX
AIM​:
Write a program in c to find the transpose of a matrix.
PROGRAM​:
#include <stdio.h>
#include <conio.h>
#define SIZE 3
main()
{
int mat[3][3],i,j;
clrscr();
printf("enter the elements of matrix:\n");
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("transpose of the the matrix:\n");
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
printf("%d",mat[j][i]);
}
printf("\n");}
getch();
return 0;} ​27
OUTPUT

enter the elements of matrix:


123
456
789
transpose of the the matrix:
147
258
369

RESULT​:
The program is compiled and transpose of the matrix is got successfully.

28
Exp no​:14 ​Date:​01-12-2021

MATRIX MULTIPLICATION
AIM​:
Write a program in C to find the product of two matrices

PROGRAM​:
#include <stdio.h>
#include <conio.h>
int main()
{
int m,n,p,q,i,j,k,sum=0;
int first[10][10],second[10][10],multiply[10][10];
clrscr();
printf("enter the number of rows and columns of first matrix:\n");
scanf("%d%d",&m,&n);
printf("enter the elements of first matrix:\n");
for(i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&first[i][j]);
printf("enter the number of rows and columns of second matrix:\n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("multiplication not possible");
else
{
printf("enter the elements of second matrix:\n");
}
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&second[i][j]);
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
sum=sum+first[i][k]*second[k][j];
} ​29
multiply[i][j]=sum;
}
}
printf("product of the matrices:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",multiply[i][j]);
printf("\n");}

}
getch();
return 0;
}

30
OUTPUT​:
enter the number of rows and columns of first matrix:
2
2
enter the elements of first matrix:
1
2
3
4
enter the number of rows and columns of second matrix:
2
2
enter the elements of second matrix:
5
6
7
8
product of the matrices:
14
16
28
32

RESULT:
​The program is compiled and multiplication of two matrices is done successfully.

31
Exp no​:15 ​Date​:04-12-2020

​STRING OPERATIONS USING BUILT IN FUNCTIONS

AIM​:
write a program to perform all string operations using built in functions in c

PROGRAM​:
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char str1[50],str2[50];
clrscr();
puts("enter str1:\n");
gets(str1);
puts("\nenter str2:\n");
gets(str2);
strcat(str1,str2);
printf("\ncombined string:| %s |\n",str1);
if(strcmp(str1,str2)==0)
printf("\nentered strings are equal\n");
else
printf("\nentered strings are not equal\n");
strcpy(str1,str2);
printf("\nstr1 is:%s",str1);
strrev(str1);
printf("\nreverse of str1 is:%s\n",str1);
strlen(str1);
printf("the length of str1 is:%d",strlen(str1));
getch();
return 0;
}

32
OUTPUT
enter str1:

THE EARTH

enter str2:

IS BEAUTIFUL

combined string:| THE EARTH IS BEAUTIFUL |

entered strings are not equal

str1 is:IS BEAUTIFUL


reverse of str1 is:LUFITUAEB SI
the length of str1 is:12

RESULT​:
The program compiled successfully,using built in funtionsall the string operations are
performed

33
Exp no​:16 ​Date​:11-12-2020

COUNT THE NO.OF WORDS IN A TEXT

AIM​:
WAP to count the no.of words in a text

PROGRAM :
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char s[200];
int i,count=0;
clrscr();
printf("enter the string:\n");
scanf("%[^\n]s",s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]==' '&& s[i+1]!=' ')
count++;
}
printf("no.of words in given string are %d\n",count);
getch();
}

34
OUTPUT​:
enter the string:
welcome to c programming
no.of words in given string are 4

RESULT​:
The program was compiled.number of words in the entered text is shown successfully

35
Exp no​:17 ​Date:​18-12-2020

PALINDROME STRING OR NOT

AIM:
WAP to check whether a given string is palindrome or not

#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char string[100],temp,x[100];
int i,j;
printf("enter a string:\n");
scanf("%s",string);
strcpy(x,string);
i=0;
j=strlen(string)-1;
while(i<j)
{
temp=string[i];
string[i]=string[j];
string[j]=temp;
i++;j--;
clrscr();
}
printf("reverse of the string is: %s\n",string);
if(strcmp(x,string)==0)
printf("%s is a palindrome string",x);
else
printf("%s is not a palindrome string",x);
getch();
return 0;
}

36
OUTPUT:
enter a string:
malayalam
reverse of the string is: malayalam
malayalam is a palindrome string

RESULT:
The program was compiled successfully

37
Exp no​:18 ​Date​:22-12-2020

SWAPPING TWO VALUES USING CALL BY VALUE AND CALL BY


REFERENCE

AIM​:
swap two numbers using call by value & and call by reference

PROGRAM​:
#include <stdio.h>
#include <conio.h>
void swap1(int,int);
void swap2(int*,int*);
void main()
{
int a,b;
int c,d;
clrscr();
printf("Enter the value of two integers");
scanf("%d%d",&a,&b);
printf("a=%d,b=%d before calling function\n",a,b);
swap1(a,b);
printf("a=%d,b=%d after calling the function\n",a,b);

printf("\nenter the value of two integers\n");


scanf("%d%d",&c,&d);
printf("a=%d,b=%d before calling function\n",c,d);
swap2(&c,&d);
printf("a=%d,b=%d after calling the function\n",c,d);
getch();
}
void swap1(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("a=%d,b=%d in function\n",a,b);
}
void swap2(int *c,int *d)
{
int temp;
temp=*c;
*c=*d;
*d=temp;}
38
OUTPUT​:
Enter the value of two integers
2
3
a=2,b=3 before calling function
a=3,b=2 in function
a=2,b=3 after calling the function

enter the value of two integers


2
3
a=2,b=3 before calling function
a=3,b=2 after calling the function

RESULT:

The program is compiled .the numbers are swapped using call by value and call by reference
successfully.

39
Exp No:​:19 ​Date​:05-01-2020

FACTORIAL USING RECURSION

AIM​:​WAP to find the factorial of a number using recursion.

PROGRAM​:
#include <stdio.h>
#include <conio.h>
int factorial(int);
int main()
{
int num,fact;
clrscr();
printf("enter any integer number:");
scanf("%d",&num);
fact=factorial(num);
printf("factorial of %d is:%d",num,fact);
getch();
return 0;
}
int factorial(int n)
{
if(n==0)
return(1);
else
return(n*factorial(n-1));
}

40
OUTPUT​:
enter any integer number:5
factorial of 5 is:120

RESULT​:
The Program is compiled. Factorial of number is shown successfully.

41
Exp No:​20 ​Date:​08-01-2020

DISPLAY STUDENT DETAILS USING STRUCTURE

AIM:
WAP to find average marks and display student details using structure.

PROGRAM​:-
#include <stdio.h>
#include <conio.h>
struct student
{
int rno,marks[5],total;
float avg;
char name[10];
}s[10];
void main()
{
int i,j,n;
clrscr();
printf("Enter the no.of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the data of student %d\n",i+1);
printf("\nEnter the name:\n");
scanf("%s",s[i].name);
printf("enter the roll number:\n");
scanf("%d",&s[i].rno);
s[i].total=0;
for(j=0;j<5;j++)
{
printf("enter the marks of the subject%d:",j+1);
scanf("%d",&s[i].marks[j]);
s[i].total=s[i].total+s[i].marks[j];
}
s[i].avg=(s[i].total)/5;
}
for(i=0;i<n;i++)
{
printf("\nTHE DETAILS ARE :\n\nrollno:%d\nname:%s\naverage:%f\n
",s[i].rno,s[i].name,s[i].avg);
}
getch();
return 0;
}
42
OUTPUT​:

Enter the number of students:2

enter data of student 1

Enter the name:


RAM
enter the roll number:
1
enter the marks of the subject1:44
enter the marks of the subject2:45
enter the marks of the subject3:46
enter the marks of the subject4:47
enter the marks of the
subject5:48

Enter the data of student 2

Enter the name:


RAJU
enter the roll number:
2
enter the marks of the subject1:42
enter the marks of the subject2:43
enter the marks of the subject3:44
enter the marks of the subject4:45
enter the marks of the subject5:46

THE DETAILS ARE :

rollno:1
name:RAM
average:46.000000

THE DETAILS ARE :

rollno:2
name:RAJU
average:44.000000

RESULT:
The program was compiled and thestudent details are shown successfully.

​43
Exp No​:21 ​Date​:12-01-2021

​DISPLAY EMPLOYEE DETAILS USING UNION

AIM:
WAP to display employee details using union.

PROGRAM​:

#include <stdio.h>
#include <conio.h>
union employee
{
int code;
char name[10];
int bpay;
}u[5];
void main()
{
int i,n;
clrscr();
printf("enter the number of employees:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the details of employee %d:\n",i+1);
printf("\nenter the name:\n");
scanf("%s",u[i].name);
printf("\nname:%s\n",u[i].name);
printf("\nenter the code:\n");
scanf("%d",&u[i].code);
printf("\ncode:%d\n",u[i].code);
printf("\nenter the amount of bpay:\n");
scanf("%d",&u[i].bpay);
printf("\nbpay:%d",u[i].bpay);
}
getch();
return 0;
}

44
OUTPUT​:

enter the code:


1

code:1

enter the amount of bpay:


200

bpay:200
enter the details of employee 2:

enter the name:


RAMU

name:RAMU

enter the code:


2

code:2

enter the amount of bpay:


300

bpay:300

RESULT​:
The program is compiled and details of employee printed succesfully.

45
Exp​ No:22 Date:19-01-2021

IMPLEMENT COMMAND LINE ARGUMENTS

AIM​:
WAP to implement command line arguments.

PROGRAM​ :

#include <stdio.h>

int main( int argc, char*argv[])

if(argc==2)

printf("argument supplied is %s\n", argv[1]);

else if(argc>2)

printf("So many arguments supplied.\n");

else

printf("one argument expected ");

46
OUTPUT​ :

one argument expected.

RESULT​ :
program to implement command line arguments done successfully.

47
Exp No:23 Date:22-01-2021
DISPLAY ADDRESS AND CONTENTS OF VARIABLES USING
POINTERS

AIM​:
WAPto display address and contents of different variables(data types )using pointers.

PROGRAM​:

#include <stdio.h>
#include <conio.h>
void main()
{
int i=3;
int *j;
float k=2.5;
float *f;
char ch='a';
char *b;
double p=2.3005;
double *q;
clrscr();
j=&i;
f=&k;
b=&ch;
q=&p;
printf("i=%d\n",i);
printf("&i=%u\n",&i);
printf("j=%u\n",j);
printf("&j=%u\n",&j);
printf("*j=%d\n",*j);
printf("\nk=%f\n",k);
printf("&k=%u\n",&k);
printf("f=%u\n",f);
printf("&f=%u\n",&f);
printf("*f=%f\n",*f);
printf("\nch=%c\n",ch);
printf("&ch=%u\n",&ch);
printf("b=%u\n",b); ​48
printf("&b=%u\n",&b);
printf("*b=%c\n",*b);
printf("\np=%lf\n",p);
printf("&p=%u\n",&p);
printf("q=%u\n",q);
printf("&q=%u\n",&q);
printf("*q=%lf\n",*q);
getch();}

49
OUTPUT​:

i=3
&i=65524
j=65524
&j=65522
*j=3

k=2.500000
&k=65518
f=65518
&f=65516
*f=2.500000

ch=a
&ch=65515
b=65515
&b=65512
*b=a

p=2.300500
&p=65504
q=65504
&q=65502
*q=2.300500

RESULT​:
The program is compiled.the contents and address of different variables of different data
types using pointers displayed succesfully.

50
Exp No:24 Date:25-01-2021

SUM OF ARRAY ELEMENTS USING POINTERS


AIM:
WAPto compute the sum of all elements in an array using pointers.

PROGRAM​:

#include <stdio.h>
#include <conio.h>
main()
{
int a[10],i,sum=0,*p;
clrscr();
printf("enter 10 elements:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
p=a;
for(i=0;i<10;i++)
{
sum=sum+*p;
p++;
}
printf("the sum is %d",sum);
getch();
return 0;
}

51
OUTPUT​:

enter 10 elements:10
20
30
40
50
60
70
80
90
100
the sum is 550

RESULT​:
The program is compiled and sum of the elements of the array got succesfully using
pointers.

52
Exp No:​25 ​Date:​29-01-2021

FILE PROGRAM
AIM:
A file named DATA contains a series of integer numbers. Write a program to
read these numbers and then write all “odd” numbers to a filecalledODD and
all “even” numbers to a file called EVEN.

PROGRAM​:
#include <stdio.h>
#include <conio.h>
main()
{
FILE *f1,*f2,*f3;
int number,i;
clrscr();
printf("contents of DATA ​file:\n​");
f1=fopen("DATA","w");
for(i=1;i<=10;i++)
{
scanf("%d",&number);
if(number==-1)
break;
putw(number,f1);
}
fclose(f1);
f1=fopen("DATA","r");
f2=fopen("ODD","w");
f3=fopen("EVEN","w");
while((number=getw(f1))!=EOF)
{
if((number%2)==0)
putw(number,f3);
else
putw(number,f2);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD","r");
f3=fopen("EVEN","r");
printf("\ncontents of ODD files\n");
while((number=getw(f2))!=EOF)
printf("%4d",number);
printf("\ncontents of EVEN ​file:\n​"); ​53
while((number=getw(f3))!=EOF)
printf("%4d",number);
fclose(f2);
fclose(f3);
getch();
return 0;
}

54
OUTPUT​:
contents of DATA file:
111
222
333
444
555
666
777
888
999
000

contents of ODD files


111 333 555 777 999
contents of EVEN file:
222 444 666 888 0

RESULT​:
The program is compiled and contents of ODD files and EVEN files got successfully.

55

You might also like