C Lab
C Lab
No:1 a
PROGRAM USING I/O STATEMENTS AND OPERATORS
Date:
AIM
ALGORITHM
Step 1: Start
Step 2: Assign the values for Input and output process.
Step 3: Read the Input variable for finding the even and odd number of given upper
bound. Step 4: Using I/O statements and Operations for computational processing. Step 5:
Display the output of the calculations. Step 6: Stop
PROGRAM
#include <stdio.h>
void main ()
{ int s1 =
0; int s2 =
0; int ub;
int absDiff;
int n = 1;
clrscr();
printf("Enter the upper bound: ");
scanf("%d", &ub);
// compute the process
while(n <= ub)
{
if (n % 2 == 0)
{
S2 += n;
}
else
{
S1 += n;
}
++n;
}
if (s1> s2)
{
absDiff = s1– s2;
}
else
{ absDiff = s2–
s1;
}
RESULT:
Thus the C Program using I/O statements and operators was executed and the
output was obtained.
Ex .No:1 b
PROGRAM USING EXPRESSIONS
Date:
AIM
To write a C Program to evaluate an expressions.
ALGORITHM
PROGRAM:
#include<stdio.h>
#include<conio
.h> void main
() { int
a,b,c,d,e,f,g ;
clrscr(); printf
("\nEnter a
value:");
scanf("%d",&a);
printf("\nEnter b
value:");
scanf("%d",&b);
printf("\nEnter c
value:");
scanf("%d",&c);
printf("\nEnter d
value:");
scanf("%d",&d); printf
("\nEnter the e value:");
scanf("%d",&e); printf
("\nEnter the f value:");
scanf("%d",&f);
g=a*b/(b+c*b/d+a)
+e*(e/f);
printf ("\nthe total value of expression g is %d”, g);
getch(); }
OUTPUT:
RESULT:
AIM
To write a C Program to perform decision making constructs using if else Condition.
ALGORITHM
PROGRAM:
#include<stdio.h>
void main()
{ int
grade;
clrscr();
printf((“Enter your mark:”);
scanf("%d",&grade); if
(grade <= 10)
{
printf("YOU DID NOT STUDY.\n"); printf("YOU
FAILED ! \n");
}
else
{
if (grade < 60)
{
printf("You failed \n"); printf("Study
harder\n");
}
Else {
if (grade >= 60) printf("YOU
PASSED ! \n");
}}
getch()
;
}
OUTPUT:
Enter your mark:10 YOU
DID NOT STUDY. YOU
FAILED!
Enter your mark:30
You failed
Study harder
RESULT:
Thus the C Program to perform decision making constructs using if else Condition
executed successfully.
AIM
To write a C Program to perform decision making constructs using switch case statement.
ALGORITHM
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ()
{ char
myinput;
int i;
clrscr();
printf("Which option will you choose:\n");
printf("a) Program 1 \n"); printf("b)
Program 2 \n"); scanf("%c", &myinput);
switch (myinput)
{ case
'a':
printf("Run program 1\n");
printf("Enter the i value:");
scanf("%d”, &i); if (i== 5)
{ printf("OK\n");
} else { printf("Not
OK\n");
} break; case 'b': { printf("Run
program 2\n"); printf("Please
Wait\n"); break;
}
default:
printf("I
nvalid
choice\
n");
break; }
getch();
}
OUTPUT:
Which option will you choose: a)
Program 1
b) Program 2 a
Run program 1
Enter the i value:5
OK
Which option will you choose: a)
Program 1
b) Program 2 a
Run program 1
Enter the i value:4
Not OK
RESULT:
Thus the C Program to perform decision making constructs using switch case statement
executed successfully.
AIM
To write a C Program to perform decision making constructs using continue statement.
ALGORITHM:
getch();
}
OUTPUT:
Please Enter any integer:5 1
Even
3
Even
5
RESULT:
Thus the C Program to perform decision making constructs using continue statement
executed successfully.
AIM
To write a C Program to perform decision making constructs using Goto statement.
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{ int
totmark;
clrscr();
printf(“\n Enter the your mark:”);
scanf(“%d”,&totmark);
If(totmark>=50)
{ goto
pass; }
else{ Got
o
fail;
}
pass:
printf(“\n congratulation! You made it\n”); fail:
printf(“\n Better luck next time\n”); getch();
}
OUTPUT:
LOOPING STATEMENTS
AIM
To write a C Program to perform Looping statement using forloop condition.
ALGORITHM
PROGRAM:
#include <stdio.h>
void main ()
{ int n,
n1;
clrscr();
Printf (“Enter the n value:”);
Scanf(“%d”,&n);
Printf (“Enter the n1 value:”);
Scanf(“%d”, &n1); for (int
a=1; a<n;a++)
{ for (int b=1; b<n1;
b++)
{ if(a==b) printf("%d,
%d\n",a,b);
}
}
getch();
}
PROGRAM 2:
/* For Loop in C Programming Example */
#include <stdio.h>
Voidmain () {
int i, number, total= 0;
clrscr();
printf ("\n Please Enter any integer\n");
scanf ("%d", &number); for (i=1; i<=
number; it+) total = total + i;
printf("\nSum of n natural numbers is: %d\n", total); getch
();
}
OUTPUT:
Please Enter any integer 20
Sum of n natural numbers is: 210
PROGRAM 3:
//Program for Nested for loop
#include <stdio.h>
#include<conio.h
> void main ()
{
int i, j;
clrscr();
for (i=9; i<=10; i++)
{ for (j=1; j<=10; j+
+)
{ printf ("%d * %d = %d\n”, i, j,
i*j);
}
} getch
();
}
OUTPUT:
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
RESULT:
Thus the C Program to perform Looping statement using for loop condition was executed
successfully.
LOOPING STATEMENTS
AIM
To write a C Program to perform Looping statement using while loop.
ALGORITHM
PROGRAM 1:
#include <stdio.h>
#include<conio.h>
void main()
{ int i,n,in;
i=1;
clrscr();
printf("Enter a value:");
scanf("%d",&n);
while(i<=n)
{ printf("\n");
in=i;
while(in<=n
)
{ printf("%d",in);
in=in+1; } i=i+1;
}
getch();
}
OUTPUT:
Enter the value:5
PROGRAM 2:
//program to find the factorial of numbers
#include <stdio.h> void
main()
{ int n,i,f;
f=i=1;
clrscr();
printf("Enter a Number to Find Factorial: ");
scanf("%d",&n);
while(i<=n)
{ f*=i;
i++; }
printf("The Factorial of %d is : %d",n,f);
getch(); }
OUTPUT:
Enter a Number to Find Factorial: 5 The
Factorial of 5 is: 120
PROGRAM 3:
//program to find the Fibonacci of numbers
#include<stdio.h> void
main ()
{ int count, n, t1=0, t2=1, Temp=0;
clrscr();
printf("Enter The Number of Terms :\n"); scanf("%d",&n);
printf("\nFibonacci Series : %d , %d , ", t1, t2);
count=2; while
(count<n)
{
Temp=t1+t2; t1=t2;
t2=Temp; ++count;
printf("%d ,
",Temp);
}
getch();
}
OUTPUT:
Enter the Number of Terms:
10
Fibonacci Series: 0 ,1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34.
RESULT:
Thus the C Program to perform Looping statement using while loop was executed
successfully.
LOOPING STATEMENTS
Ex.No:3.c
Date: (DO-WHILE LOOP)
AIM
To write a C Program to perform Looping statement using do-while loop.
ALGORITHM
PROGRAM 1:
//Program to calculate the interest
#include <stdio.h>
void main ()
{ float
a,b,c; char
ch='y'; do
{
printf("enter the principle:"); scanf("%f",&a);
printf("enter the Rate:"); scanf("%f",&b);
printf("enter the Tax:"); scanf("%f",&c);
printf("interest rate=%.5f",(a*b*c)/100);
printf("\n\n calculate interest again? ('y' for yes, 'n' for no): “); scanf("%c”,
&ch); } while(ch=='y');
}
PROGRAM 2:
#include <stdio.h>
#include <conio.h> void
main()
{ int i,
num;
clrscr();
printf("Enter a number: \n");
scanf("%d",&num); i=1; do
{
printf("%d\n", i); i+
+
} while (i<num);
getch();
}
PROGRAM 3:
#include <stdio.h>
#include <conio.h> void
main ()
{
int i;
clrscr();
printf("Even number upto 20 \n");
i=0; do { i=i+2; printf("%d\n",
i); }while (i<= 20); getch();
}
OUTPUT:
Even number up to 20 2
4
6
8
10
12
14
16
18
20
22
RESULT:
Thus the C Program to perform Looping statement using do-while loop was executed
successfully.
AIM
To implement a C Program to perform Array using 1-Dimensional array.
ALGORITHM
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ()
{ int sub,marks,n,i,sum=0, tmp=0, arr[10],Percentage;
printf("\nEnter number of subject : \n");
scanf("%d", &n);
tmp=n*100;
printf("\nEnter The Marks: \n"); for
(i=0; i<n;i++)
{ scanf("%d",
&arr[i]);
} for (i=0;
i<n;i++)
{
sum=sum+arr[i];
}
Percentage = (sum * 100) / tmp;
printf("\nPercentage Of Student : %d\n", Percentage);
getch();
}
OUTPUT:
RESULT:
Thus the C Program to perform Array using 1-Dimensional array was executed
successfully.
AIM
To implement a C Program to perform Array using 2-Dimensional array.
ALGORITHM
Step1:Start the program Step2:Assign
the variables.
Step3:Get the value of two matrices using 2-dimensional array. Step4:Adding two matrices
using 2-dimensional array and display the output. Step5:Stop the program.
PROGRAM 1:
#include <stdio.h>
void main ()
{ int
arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{ printf("\
n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}
OUTPUT:
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34
Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78
56 10 30
34 21 34
45 56 78
RESULT:
Thus the C Program to perform Array using 2-Dimensional array was executed
successfully.
AIM:
To implement a C Program to perform Array using multi-Dimensional array.
ALGORITHM:
Step1:Start the program
Step2:Assign the variables of test.
Step3:Print the value and store the input using multi- dimensional array.
Step4:Perform the for-loop asper the condition and display the values. Step5:Stop
the program.
PROGRAM:
#include <stdio.h>
int main() {
int grades[5][4];
return 0;
}
OUTPUT:
Enter grades for student 1:
Subject 1: 80
Subject 2: 75
Subject 3: 90
Subject 4: 85
Enter grades for student 2:
Subject 1: 70
Subject 2: 85
Subject 3: 80
Subject 4: 75
Enter grades for student 3:
Subject 1: 90
Subject 2: 80
Subject 3: 85
Subject 4: 95
Enter grades for student 4:
Subject 1: 75
Subject 2: 90
Subject 3: 75
Subject 4: 80
Enter grades for student 5:
Subject 1: 85
Subject 2: 70
Subject 3: 80
Subject 4: 90
Average grade for each student:
Student 1: 82.50
Student 2: 77.50
Student 3: 87.50
Student 4: 80.00
Student 5: 81.25
Average grade for each subject:
Subject 1: 80.00
Subject 2: 80.00
Subject 3: 82.00
Subject 4: 85.00
RESULT:
AIM:
To implement a C Program to perform Traversal using array.
ALGORITHM:
Step1:Start the program
Step2:Assign the variables list and i variable for loop.
Step3:Get the value of array elements and perform the loop asper the loop condition.
Step4:Display the values(i.e., list) of traversal array.
Step5:Stop the program.
PROGRAM:
// C program to traverse the array
#include <stdio.h>
// Function to traverse and print the array
void printArray(int* arr, int n)
{ int
i;
printf("Array: ");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
OUTPUT:
Array: 2 -1 5 6 0 -3
RESULT:
Thus the C Program to perform Traversal using array was executed successfully.
AIM:
To implement a C program using strings and its operations.
ALGORITHMS:
PROGRAM 1:
//Program to count characters in given string using strlen function.
#include<stdio.h>
#include<string.h>
void main ()
{
char str [20] ="AHMEDABAD";
int i, cnt=0, len=0;
len=strlen(str);
printf(“Length of the string is %d”,len) for
(i=0; i < len; i++)
{ if(str[i] == 'A')
cnt++; } printf("Total A in given
string : %d",cnt); getch();
}
OUTPUT:
Length of the string is 9
Total A in given string: 3
PROGRAM 2:
//String Processing - Concatenation using strcat function.
#include<stdio.h>
#include<string.h>
int main()
{ char str1[10]="12345", str2[10]="abcd";
strcat(str1,str2);
puts(str1);
return 0;
}
OUTPUT:
12345abcd
PROGRAM 3:
//Count occurrences of alphabets in the given paragraph.
//This program will count only A to Z alphabets.
#include<stdio.h>
#include<string.h>
void main()
{ int i, counter[26]={}, ch; char
str[100]; puts("Enter your
paragraph:"); gets(str);
OUTPUT:
Enter your paragraph:
HELLO STUDENT HOW ARE YOU
Character and its frequency are as under: A = 1
D=1
E =3
H=2
L=2
N=1
O=3
R=1
S =1
T =2
U=2
W=1
Y=1
PROGRAM 4:
#include <stdio.h>
#include
<string.h> void
main ()
{ char str1[10] = "12345";
char str2[10] = "World";
strcpy(str1, str2, 4);
str1[4]
='\0';
printf("strcpy( str1, str2, 3) = %s\n", str1 ); getch();
}
OUTPUT:
strncpy (str1, str2, 3) = Worl
PROGRAM 5:
//C program to print password as star ( ***** )
#include<stdio.h>
#include<string.h>
void main()
{ int i;
char ch1, password[10];
printf("Enter your
password:"); for(i=0; i<10; i+
+)
{ ch1=getch();
password[i]=ch1
;
PROGRAM 6:
// Use of built-in C string library function.
#include<stdio.h>
#include<string.h>
void main()
{ int len;
char str1[10], str2[10];
puts("Enter String 1:");
gets(str1); puts("Enter
String 2:");
gets(str2);
//Function to find length of given string 1. len =
strlen(str1); printf("\nLength of String 1 is %d.\n",
len); //Function to compare two strings
if(strcmp(str1,str2)==0) printf("\nString 1 and String
2 are same.\n"); else printf("\nString 1 and String 2
are not same.\n");
//Function to copy string 1 into string 2 strcpy(str2,str1); //this
will copy str1 into str2 printf("\nString 2 after execution of strcpy
= %s", str2); getch();
}
OUTPUT:
Enter String 1:
abc Enter
String 2:
xyz
Length of String 1 is 3.
String 1 and String 2 are not same. String 2 after
execution of strcpy = abc
PROGRAM 7:
// C Program to check given string is Palindrome or not.
#include <stdio.h>
#include <string.h>
void main()
{ char a[10], b[10]; printf("Enter
your string:\n"); gets(a);
strcpy(b,a);
strrev(b); if (strcmp(a,b) == 0) printf("Entered
string is a palindrome.\n"); else printf("Entered
string is not a palindrome.\n");
getch();
}
OUTPUT:
Enter your string:
ABA
Entered string is a palindrome.
RESULT :
Thus the C program to perform various string operation was executed and obtained output.
Ex .No:6.a C PROGRAM TO FIND THE GIVEN NUMBER AS AN ARMSTRONG
NUMBER OR NOT USING USER DEFINED FUNCTION
Date:
AIM:
ALGORITHM:
Step 1: start
Step 2:read n
Step 3:assign sum 0,I m n,count =0
Step 4:if m>0 repeat
Step 4.1:m m/10
Step 4.2:count++
Step 4.3:until the condition fail
Step5: if I>0 repeat step 4 until condition fail
Step 5.1:rem I%10
Step 5.2:sum sum+pow(rem,count)
Step 5.3:I I/10
Step 6:if n=sum print Armstrong otherwise print not armstrong Step 7:stop
PROGRAM:
#include<stdio.h>
// declare
variables int
lastDigit = 0;
int power = 0;
int sum = 0;
while(n!=0) {
int main()
{ int
number;
if(isArmstrong(number) == 0) printf("\n
%d is an Armstrong number.\n",
number); else
printf("%d is not an Armstrong number.", number);
return 0;
}
OUTPUT:
Enter number:153
153 is an Armstrong number
RESULT:
Thus the C Program to find whether a given number in Armstrong or not was executed and
the output was obtained.
Ex .No:6.b C PROGRAM TO FIND MAXIMUM OF GIVEN THREE NUMBERS
USING FUNCTION WITH PARAMETER
Date:
AIM:
To find maximum of three number using sub function with three parameters
ALGORITHM:
main program
Step 1: start
Step 2: call subprogram as num1=input();num2=input();num3=input()
Step 3: call sub program as largest=large(num1,num2,num3)
Step 4: call subprogram as display(largest)
Step 5: stop
Sub program: large(float a, float b, float c)
Step 1: if a> b and a>c ; return a
Step 2: if b>a and b>c ; return b;
Step 3: other wise return c to main program
Sub program: input()
Step 1: read n
Step 2: return n
Sub program: display(float max)
Step 1: print max
PROGRAM:
#include<stdio.h>
// main
function
int main()
{ float num1, num2, num3, largest;
num1 = input();
num2 = input(); num3
=
input();
return 0;
}
AIM:
To write a C program to swap to values using pass by value and pass by reference with
functions
ALGORITHM:
a)
main program
Step 1: start
Step 2: read a, b as integer value Step 3: call
sub program as swap(a,b) Step 4: stop
Sub program: swap(int a, int b)
Step 1: assign temp to a Step
2: assign a to b Step 3:
assign b to temp Step 4:
display a and b
b)
main program
Step 1: start
Step 2: read a, b as integer value
Step 3: call sub program as swap(&a,&b)
Step 4: stop
Sub program: swap(int *a, int *b)
Step 1: assign temp to *a Step 2:
assign *a to *b
Step 3: assign *b to temp
Step 4: display a and b
PROGRAM:
b)
RESULT:
Thus the C Program to perform swap operation using pass by value and pass by reference
was executed and the output was obtained.
Ex .No:6.d C PROGRAM TO FIND THE SQUARE AND CUBE OF A NUMBER
USING A FUNCTION WITH RETURNVALUE
Date:
AIM:
ALGORITHM:
main program
Step 1: start
Step 2: read a as integer value
Step 3: call sub program as s = square(a)
Step 4: display s
Step 5: call sub program as c = cube(a) Step 6:
display c
#include <stdio.h>
double cube(int);
double
square(int); int
main()
{ int num;
double
c,s;
printf("Enter any number: ");
scanf("%d", &num); c =
cube(num);
square(num);
return 0;
}
OUTPUT:
Enter any number:5
Cube of 5is 125
Square of 5 is 25
RESULT:
Thus the C Program to compute square and cube of a number using function was executed
and the output was obtained.
Ex .No:6.e C PROGRAM TO FIND THE SUM OF ‘N’ NATURAL NUMBER USING
FUNCTION
Date:
AIM:
ALGORITHM:
main program
Step 1: start
Step 2: read range as integer value
Step 3: call sub program as result = sum(range)
Step 4: display result
Step 5 : Stop
int sum(int n)
{ int i, add = 0;
for( i=1; i<=n; i++)
{ add +=
i; } return
add;
}
int main()
{ int range, result; printf("Upto which number you
want to find sum:
"); scanf("%d", &range);
result = sum(range);
printf("\n1+2+3+...+%d+%d = %d",range-1, range, result); }
OUTPUT:
Upto which number you want to find
sum:10 1+2+3+...+9+10= 55
RESULT:
Thus the C Program to compute sum of n natural numbers was executed and the output
was obtained.
Ex .No:6.f C PROGRAM TO FIND THE MEAN OF ‘N’ NATURAL NUMBERS
USING FUNCTION
Date:
AIM:
ALGORITHM:
main program
Step 1: start
Step 2: read range as integer value
Step 3: call sub program as result = sum(range)
Step 4: display result
Step 5: Compute mean as mean = result/range
Step 6: display mean
Step 7 : Stop
PROGRAM:
#include<stdio.h
int main()
{ int range, result,mean; printf("Upto which number
you want to find sum:
"); scanf("%d", &range);
result = sum(range);
printf("\n1+2+3+...+%d+%d = %d",range-1, range, result);
mean=result/range;
printf("\nMean = %d",mean, result); }
OUTPUT:
Upto which number you want to find
sum:10 1+2+3+...+9+10= 55
Mean=5.5
RESULT:
Thus the C Program to compute mean of n natural numbers was executed and the output was
obtained.
Ex .No:7.a C PROGRAM TO FIND THE FACTORIAL OF GIVEN NUMBER
USING RECURSIVE FUNCTION
Date:
AIM:
To write a C program to find the factorial of a given number using recursive function.
ALGORITHM:
main program
Step 1: start Step
2: read n
Step 3: call sub program as f=fact(n)
Step 4: print f value Step 5:
stop
Sub program:
Step 1: initialize the f
Step 2: if n= = 0 or n == 1 return 1 to main program if not goto step 3 Step 3: return
n*fact(n-1) to main program
PROGRAM:
#include<stdio.> int
factorial(int n)
{ if(n!=0) return n*factorial(n-1); // general
case
else return 1; // base
case
}
int main()
{ int num, result; printf("Enter a
positive number: ");
scanf("%d",&num); result=
factorial(num); //function call
printf("\nResult = %d\n",result);
return 0;
}
OUTPUT:
Enter a positive number: 5
Result=120
RESULT:
Thus the C Program to compute factorial of a given number using recursive funcion was
executed and the output was obtained.
Ex .No:8.a
C PROGRAM TO DISPLAY ADDRESS AND VALUE OF VARIABLE
Date:
USING POINTER
AIM:
ALGORITHM:
Step 1: Start
Step 2 : Declare a variablecand a pointer variablepcof type integer. Step
3: Display value of c and address of c using vaiable c and reference
operator &. Step 4: Assign c to pc;
Step 5: Display value of c and address of c using pointer vaiable
pc. Step 6: Change value of c using pointer variable pc. Step 7:
Repeat step 3 and step 5 Step 8 : Stop.
PROGRAM:
*pc = 2; printf("Address of c:
%p\n", &c); printf("Value of c:
%d\n\n", c); return 0;
}
OUTPUT:
Address of c: FFF8
Value of c:22
Address of pointer pc:
FFF01 Content of pointer
pc:22 Address of pointer pc:
FFF01 Content of pointer
pc:11
Address of c: FFF8
Value of c:2
RESULT:
Thus the C program to display address and value of a variable was executed successfully and
obtained output.
Ex .No:8.b C PROGRAM TO PRINT STRING USING
POINTERS
Date:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare a character array and a character pointer
variable
Step 3: Read a string into character array..
Step 4: Assign pointer variable to character array.
Step 5: Repeat step 6 until array element ‘/0’ is reached
Step 6: Display string value by reading each element of the array using pointer
variable Step 7 : Stop
PROGRAM:
while(*ptr!='\0') printf("%c",*ptr+
+);
return 0;
}
OUTPUT:
RESULT:
Thus the C program to display string value using pointer was executed successfully and obtained
output.
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare an integer array ”a” and a integer pointer variable “p”
Step 3: Assign values to integer array. Step 4: Assign pointer variable
to integer
array. Step 5: Initialize i to 0
Step 5: Repeat step 6 until i reaches size of the integer array “a” Step
6: Display each element of the array using pointer variable “p”
Step 7 : Stop
PROGRAM:
#include<stdio.h
> void main()
{ int
a[5]={5,4,6,8,9};
int *p=&a[0];
int i; clrscr();
for(i=0;i<5;i++)
printf("%d\
t",*(p+i)); getch();
}
OUTPUT:
5 4 6 8 9
RESULT:
Thus the C program to display array elements using pointers executed successfully and obtained
output.
Ex .No:9.a
Date: C PROGRAM TO DISPLAY BOOK DETAILS USING STRUCTURE
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare structure "book" having data member as book_name, book_id, book_price, author.
Step 3: Declare book of size 3
Step 4: Read Book details – use loop construct that iterates thrice.
Step 5: Display Book details – use . operator to access structure members. – use loop another
loop construct that iterates thrice.
Step 6: Stop
PROGRAM:
#include<stdio.h>
struct book
{ char book_name[20];
int bookid; float
book_price; char
author[15];
};
int main()
{ struct book b[3];
int i;
//clrscr();
for(i=0;
i<3; i++)
{ printf("Enter details of book #%d\n",
i+1); printf("Enter book id: ");
scanf("%d", &b[i].bookid);
printf("Enter book name: ");
scanf("%s", b[i].book_name);
printf("Enter book author: ");
scanf("%s", b[i].author);
printf("Enter book price: ");
scanf("%f", &b[i].book_price);
}
OUTPUT:
Enter details of book #1
Enter book id: 101
Enter book name: LetUsC Enter
book author:
Kanetkar Enter book price:
350.20 Enter details of
book #2 Enter book id: 102
Enter book name: ANSI_C
Enter book author:
Balgurusamy Enter book price:
300.78
Enter details of book #3
Enter book id: 103
Enter book name: OperatingSystem
Enter book author: Godbole
Enter book price: 295.67
Book 1.............
Book Id: 101
Book Name: LetUsC
Book Author: Kanetkar
Book price: 350.200012
Book 2.............
Book Id: 102
Book Name: ANSI_C Book
Author:
Balgurusamy Book price:
300.779999
Book 3.............
Book Id: 103 Book
Name:
OperatingSystem Book
Author: Godbole
Book price: 295.670013
RESULT:
Thus the C program to display book details using structure executed successfully and obtained
output.
ALGORITHM:
Main program:
Step 1: Start
Step 2: Declare structure "date" having data member as day, month,
year Step 4: Read Date details. Step 5: Call subprogram as
display(&d) Step 6: Stop
PROGRAM:
#include<stdio.h>
OUTPUT:
Enter the day , month and year :
17
JUL
Y
2001
DAY: 17
MONTH :
JULY YEAR :
2001
RESULT:
Thus the C program to display date using structures and pointers executed successfully and
obtained output.
Ex .No:9.c
C PROGRAM TO DISPLAY JOB DETAILS USING UNION AND
Date: STRUCTURE
AIM:
ALGORITHM:
Main program:
Step 1: Start
Step 2:Declare structure "sjob" having data member as name, salary, workerno
Step 3: Declare structure "sjob" having data member as name, salary, workerno
Step 4: Display size information of uJob and sJob, Step
5: Assign values to uJob members.
Step 6: Assign values to sJob members.
Step 7: Display salary details – use . operator to access union member and structure members
Step 8: Stop
PROGRAM:
char
name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{ char
name[32];
float salary; int
workerNo;
} sJob;
int main()
{ printf("size of union = %d bytes",
sizeof(uJob));
strcpy(uJob.name,"Akash");
uJob.salary=11000.5; uJob.wokerNo=101;
printf("\nsize of structure = %d bytes",
sizeof(sJob));
strcpy(sJob.name,"Akash");
sJob.salary=11000.5;
sJob.wokerNo=101; printf("\nSalary in
Union =
%f",uJob.salary); prinf("\nSalary in Strucure
= %f",sJob.salary); return
0;
}
OUTPUT:
RESULT:
Thus the C program to display job details using structure and union executed successfully
and obtained output.
Ex .No:10.a
C PROGRAM TO PERFORM WRITE OPERATION ON A FILE
Date:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare FILE pointer
Step 3: Open file program.txt in write mode
Step 4: Check if file pointer returns NULL
Step 5: If Step 4 returns NULL - display ERROR Else goto Step 6
Step 6: Read a number through console
Step 7: Write that number to file through file pointer
Step 8: Stop
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{ int num; FILE *fptr; fptr =
fopen("program.txt","w");
if(fptr == NULL)
{ printf("Error!");
exit(1); }
printf("Enter num:
");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
OUTPUT:
Enter num:3456
Program.txt
3456
RESULT:
Thus the C program to write operation on file executed successfully and obtained output.
Ex .No:10.b
C PROGRAM TO PERFORM READ OPERATION ON A FILE
Date:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare FILE pointer
Step 3: Open file program.txt in read mode
Step 4: Check if file pointer returns NULL
Step 5: If Step 4 returns NULL - display ERROR Else goto Step 6
Step 6: Read a number from file as num
Step 7: Display num
Step 8: Stop
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{ int num;
FILE
*fptr;
if ((fptr = fopen("program.txt","r")) ==
NULL){ printf("Error! opening file");
fscanf(fptr,"%d", &num);
return 0;
}
OUTPUT:
RESULT:
Thus the C program to read operation on file executed successfully and obtained output.
Ex .No:10.c
Date: C PROGRAM TO PERFORM RANDOM ACCESS OVER A FILE
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare a file pointer
Step 3: Open file.txt in Write Append mode (w+)
Step 4: Write string to file
Step 5: Locate position 7 in the file using fseek function
Step 6:Stop
PROGRAM:
#include<stdio.h>
int main()
{
FILE *fp; fp =
fopen("jtp.txt","r");
if(!fp)
{
printf("Error: File cannot be opened\n");
return 0;
}
//Move 3 bytes forward, so if we print all the way through, we won't see the first 6
bytes.
fseek(fp, 3, 0);
char ch;
while(fread(&ch,sizeof(ch),1,fp)==1)
{
//Here, we go through the entire file and print everything up until the end.
printf("%c",ch);
}
fclose(fp);
return 0;
}
OUTPUT:
is best
RESULT:
Thus the C program to random access on file executed successfully and obtained output.
Ex .No:10.d
C PROGRAM TO FIND AREA OF A CIRCLE USING MACRO
Date:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Define a MACRO PI as 3.1415
Step 3: Declare variable radius and area
Step 4: Read radius
Step 5: Compute area as PI * radius * radius
Step 6: Display area Step
7: Stop
PROGRAM:
#include <stdio.h>
#define PI 3.1415
int main()
{ float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
printf("\nArea=%.2f",area)
; return 0;
}
OUTPUT:
RESULT:
Thus the C program to compute area of a circle using preprocessor directive MACRO was
executed successfully and obtained output.