SPC Manual Updated Record
SPC Manual Updated Record
SCHOOL OF COMPUTING
REGULATION 2024
LAB MANUAL
a) ARMSTRONG NUMBER 21
b) FIBONACCI SERIES 22
c) TO CHECK THE GIVEN NUMBER IS PRIME NUMBER OR NOT 24
5. a) TO PRINT THE SQUARE OF THE ARRAY ELEMENTS 27
b) INSERTING AN ELEMENT IN AN ONE DIMENSIONAL ARRAY 30
c) DELETING AN ELEMENT IN AN ARRAY 32
6. TWO DIMENSIONAL ARRAY 36
7. STRINGS AND STRING HANDLING FUNCTIONS 40
a) STRING COPY 41
b) STRING CONCATENATION 42
c) STRING COMPARISON 43
8. a) FUNCTIONS CALL BY VALUE AND CALL BY REFERENCE 44
Start
Stop
PROGRAM :
#include<stdio.h>
void main()
{
int a, b,
sum;
clrscr();
printf(“\nEnter two numbers :”);
scanf(“%d%d”, &a, &b);
sum = a+b;
printf(“\nThe sum is %d”, sum);
getch();
}
RESULT :
Thus, the C program for calculating the sum of two numbers was written, executed and verified
successfully.
Start
Stop
PROGRAM :
#include<stdio.h>
void main()
{
float a, b,
prod; clrscr();
printf(“\nEnter two numbers :”);
scanf(“%f%f”, &a, &b);
prod = a*b;
printf(“\nThe product is %f ”, prod);
getch();
}
RESULT :Thus, the C program for calculating the product of two floating point numbers was written,
executed and verified successfully.
Start
Declaring a variable ch
Stop
PROGRAM :
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf(“\nEnter a Character
:”); scanf(“%c”, &ch);
printf(“\nThe ASCII value of %c is %d”, ch,ch);
getch();
}
RESULT :
Thus, the C program for printing the ASCII value of a character was written, executed and verified
successfully.
Algorithm:
STEP 1: Start
STEP 2: Define pi = 3.14.Get the value of radius of the circle.
STEP 3: Using formulae 3.14*r*r and 2*3.14*r the area and the circumference are found. STEP 4:
Print the value of area and circumference.
STEP 5: Stop.
Start
Circumference= 2*PI
Program:
#include<stdio.h>
#include<conio.h>
#define PI 3.14
int main()
{
float r, area, circum;
clrscr();
printf("\n Enter the radius of circle:");
scanf("%f",&r);
area=PI*r*r;
circum=2*PI*r;
printf("\nArea of Circle: %f \n Circumference of a Circle: %f\
n",area,circum); getch();
return 0;
}
Output:
Enter the radius of circle: 2.34
Area of Circle: 17.193384
Circumference of a Circle: 14.695200
Result:
Thus, the C program for finding area and circumference of a circle was written, executed and verified
successfully.
Department of CSE, BIHER 4
Ex.No. 2 OPERATORS AND EXPRESSION IN C
Write a C program to show the results using printf() statement for the operations
given below from 1 to 6 in a single program.
a) logical operator
(a==b) && (c<b)
!(a!=b)|| (c<b)
b) Relational operaor
x>=y
c) Arithmetic Operator
d) Assignment operator
e) Increment and Decrement operator
f) Bitwise operator
Aim: To Write a C program to show the results using printf() statement for the logical, relational,
arithmetic, assignment, increment and decrement, bitwise operator.
Algorithm:
Step 1: Start
Step 2: Declare variables of different type as needed.
Step 3: Get the input values for the variables to demonstrate operations.
Step 4: Perform addition, subtraction, multiplication, division and modulus operations for arithmetic
operators.
Step 5: Comapare variables using >,<, >=,<=, ==, and != to demonstrate relational operators.
Step 6: Use Logical && , ||, ! to demonstrate logical operators.
Step 7: Use Assignment operator == for performing assignment operation.
Step 8: Use ++,-- operators for performing pre increment, post increment, pre decrement and post
decrement operation.
Step 9: Use Bitwise AND, OR, XOR, NOT, left shift, right shift for performing bitwise operations.
Step 10: Print the result of each operation.
Step 11: Stop
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int a = 5, b = 10, c =
8; int x = 15, y = 20;
int num = 6;
int p = 6, q =
3; int result =
0; clrscr();
// Logical operations
printf("Logical Operations:\n");
printf("(a == b) && (c < b) = %d\n", (a == b) && (c < b));
printf("!(a != b) || (c < b) = %d\n", !(a != b) || (c < b));
Output :
Logical Operations:
(a == b) && (c < b) = 0
!(a != b) || (c < b) = 1
Relational Operations:
x >= y = 0 x < y = 1
Arithmetic Operations:
a + b = 15
b-c=2
a * c = 40
b/a=2b%a=0
Assignment Operations:
Department of CSE, BIHER 6
result = a + b; result =
15
RESULT:
Thus, the above C program for various operator functions was written, executed and verified
successfully.
Write a C program to find the biggest of three numbers using ladder if else
statement.
AIM: To Write a C program to find the biggest of three numbers using ladder if else statement.
ALGORITHM:
Step 1:Start
Step 2:Declare three variables with name a, b, c, largest Step
3:Display “Enter three numbers:”
PROGRAM:
#include<stdio.h>
#include<conio.h> int
main()
{
int a,b,c;
clrscr();
printf("\nEnter a,b, and c\t");
scanf(“%d%d%d”, &a,&b,&c);
if((a>b)&&(a>c))
printf(“ The number a=%d is big”,a);
else if((b>a)&&(b>c))
printf(“ The number b=%d is big”,b);
else
printf(“ The number c=%d is big”,c);
getch();
return 0;
}
RESULT:
Thus, the C program for finding biggest of three numbers using ladder if else statement was written,
executed and verified successfully.
Write a C program to check whether a number is odd or even using 2 simple if statements.
AIM:
To write a C program to check whether the given number is odd or even.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include
<conio.h> int
main()
{
int num;
clrscr();
printf("\nEnter a number:\t");
scanf(“%d”, &num); if(num
%2==0)
{
printf(“ \nThe number is even”);
}
If(num%2!=0)
{
printf(“ \nThe number is odd”);
}
getch();
return 0;
}
OUTPUT:
Enter a number: 4
The number is even
RESULT:
Thus the C Program to check whether the given number is odd or even, was written, executed and verified
successfully.
RESULT:
Thus the C program to check the entered value as character or not was written, executed and verified
successfully.
AIM:
To write a C program to implement simple calculator using switch case statement.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <conio.h>
int main()
{
int opt,a,b,c;
clrscr();
printf("1. Addition \n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Modulus \n");
printf("EXIT \n"); printf("\
tEnter an option\t");
scanf(“%d”, &opt);
printf("\nEnter a and b values:\t");
scanf(“%d%d”, &a,&b);
switch(opt)
{
case 1: c=a+b;
printf(“\nAddition a and b is %d”,c);
break;
case 2: c=a-b;
printf(“\nSubtraction of a and b is %d”,c);
break;
case 3: c=a*b;
printf(“\nMultiplication of a and b is %d”,c);
break;
Department of CSE, BIHER 13
case 4: c=a/b;
printf(“\nDivision of a and b is %d”,c);
break;
case 5: c=a%b;
printf(“\nRemainder of a and b is %d”,c);
break;
default: printf(“\nWrong option No operation You exit”);
}
getch();
return 0;
}
OUTPUT:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
EXIT
enter an option 2
enter a and b 11 9
Subtraction of a and b is 2
RESULT:
Thus the C program using switch-case statements for mathematical calculations was written, executed and
verified successfully.
AIM:
To write a C program to find whether the number is Armstrong number or not using while looping
statement.
ALGORITHM:
STEP 1 : Start.
STEP 2: Declare n,n1,digit,sum=0.
STEP 3: Get the required positive number and store in n.
STEP 4: Use while loop until original number is not zero.
digit=n%10;
sum=sum+digit*digit*digit;
n=n /10
STEP 5: Check if digit and n are equal
STEP 6: Print the appropriate output
STEP 7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int n,n1,digit,sum=0;
clrscr();
printf("Enter any positive number");
scanf("%d",&n);
n1=n;
while(n>0)
{
digit=n%10;
sum=sum+digit*digit*digit;
n=n/10;
}
if (n1==sum)
printf("\nThe given number is an Armstrong No.%d",n1);
else
printf("\nThe given number is NOT an Armstrong No.%d",n1);
getch();
}
Output:
Enter any positive number 153
The given number is an Armstrong No.153
Result:
Thus, the above C program was written, executed and verified successfully.
Write a C program to print the Fibonacci series of n number of terms using do … while statement.
AIM:
To write a C program to print the Fibonacci series of n number of terms using do …while statement.
ALGORITHM:
Step 1:Start
Step 2:Declare variables i, a,b , show
Step 3:Initialize the variables, a=0, b=1, and show =0
Step 4:Enter the number of terms of Fibonacci series to be printed
Step 5:Print First two terms of series
Step 6:Use loop for the following steps
show=a+b
a=b
b=show
increase value of i each time by 1
print the value of show
Step 7:End
PROGRAM:
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, count = 1;
printf("\nEnter the number of terms: ");
scanf("%d", &n);
if (n <= 0)
{
printf("\nPlease enter a positive number.\n");
return 1;
}
printf("\nFibonacci Series: ");
do
{
if (count == 1)
{
printf("%d", first);
}
else if (count == 2)
{
printf(", %d", second);
}
else
{
next = first +
second; printf(",
%d", next); first =
second; second =
OUTPUT:
Enter the number of terms: 7
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8
RESULT:
Thus, the above C program was written, executed and verified successfully.
Write a C program to find the number is prime or not using for loop.
AIM:
To write a C program to find the number is prime or not using for loop.
ALGORITHM:
STEP 1: Start
STEP 2: Take n as input.
STEP 3: Initialize a variable flag to 0.
STEP 4: Iterate a “for” loop from 2 to num/2.
STEP 5: If n is divisible by loop iterator, then increment flag.
STEP 6: If the flag is equal to 0,
Return “Num IS PRIME”.
Else,
Return “Num IS NOT PRIME”.
STEP 7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,flag=0;
clrscr();
printf("Enter any number");
scanf("%d",&n);
for (i=2;i<=n/2;i++)
{
if (n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number",n);
else
printf("%d is NOT a prime number",n);
getch();
}
OUTPUT:
Enter any number 37
37 is a prime number
RESULT:
Thus, the above C program was written, executed and verified successfully.
Write a C program to read a one dimensional array and square its elements and print the array with squared
values.
AIM:
To write a C program to print the squared values of a one dimensional array.
ALGORITHM:
PROGRAM:
#include<stdio.h>
void main()
{
clrscr();
int a[5],i;
printf(“\nEnter the values to store in an array:”);
for(i=1;i<=5;i++)
scanf(“%d”, &a[i]);
printf(“\nThe squared values of the given array:”);
for(i=1;i<=5;i++)
printf(“%d”,a[i]*a[i]);
getch();
}
OUTPUT:
RESULT:
Thus the C program to print squared values of the given array was written, executed and verified successfully.
AIM:
To write a C program to insert an element in an 1 dimensional array.
ALGORITHM:
Step 1: Start
Step2. Get the element value which needs to be inserted.
Step3. Get the position value.
Step4. Check whether the position value is valid or not.
Step5. If it is valid,Shift all the elements from the last index to position index by 1 position to
the right. insert the new element in arr[position]
Step 6: Otherwise, Print array Is out of boundary.
Step 7:Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{ int i,a[30],element,n,pos;
clrscr();
printf(“\nEnter number of elements in an array\n”);
scanf(“%d”,&n);
if(n<30)
{
printf("---------- INPUT------------\n\n");
printf("\nEnter the Array Elements....\n");
for (i=0;i<n;i++)
{ scanf("%d",&a[i]);
}
printf(“\nEnter the position and element to be inserted in an array\n”);
scanf(“%d%d”,&pos,&element);
for(i=n;i>=pos;i--)
{ a[i]=a[i-1];
} a[pos]=element;
printf("----------- OUTPUT----------\n\n");
printf("\nThe Array Elements after inserting an element are...\n");
for(i=0;i<=n;i++)
{
printf("%d\n",a[i]);
}
} else
{
18
printf(“\nArray is out of boundary”);
}
getch();
}
RESULT:
Thus, the above C program to insert an element in an 1 dimensional array was written, executed and verified
successfully.
AIM:
To Write a program to delete an element from one dimensional array.
ALGORITHM:
Step 1: Start
Step 2:Find the given element in the given array and note the index.
Step 3:If the element found,Shift all the elements from index + 1 by 1 position to the left.Reduce
the array size by 1.
Step 4: Otherwise, print "Array is out of boundary".
Step 5:Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[30],n,pos;
clrscr();
printf(“\nEnter number of elements in an array\n”);
scanf(“%d”,&n);
if(n<30)
{
printf("---------- INPUT------------\n\n");
printf("\nEnter the Array Elements....\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
printf(“\nEnter the position to be deleted in an array\n”);
scanf(“%d”,&pos);
for(i=pos;i<=n;i++)
{
a[i]=a[i+1];
}
printf("----------- OUTPUT----------\n\n");
printf("\nThe Array Elements after deleting an element are...\n");
for(i=0;i<n-1;i++)
{
printf("%d\n",a[i]);
}
}
else
{
printf(“\nArray is out of boundary”);
}
getch();
}
RESULT:
Thus, the above C program to delete an element from one dimensional array was written, executed
and verified successfully.
AIM:
To write a C program to do the addition of two matrices.
ALGORITHM:
Step 1: Start.
Step 2: Declare matrix matrx A[r1][c1] and matrix matrx B[r2][c2] and matrix c[row][col], row= no. of
rows, col= no. of columns.
Step 3: Read row, col, mat1[][] and mat2[][].
Step 4: Declare variable i=0, j=0.
Step 5: Repeat until i < row
Repeat until j < col
c[i][j]=a[i][j] + b[i][j]
Set j=j+1
Set i=i+1
Step 6: c[i][j] is the required matrix after addition.
Step 7: Stop.
PROGRAM:
#include<stdio.h>
main()
{
int a[10][10],b[10][10],c[10][10],I,j,r1,c1,r2,c2;
printf(“\nEnter the row and column of the matrx A\t”); scanf(“%d
%d”,&r1,&c1);
printf(“\nEnter the row and column of the matrx B\t”); scanf(“%d
%d”,&r2,&c2);
if (r1==r2 &&c1==c2)
{
printf(“\nEnter the elements of A”)
for (i=0;i<r1;i++)
for (j=0;j<c1;j++)
{
scanf(“%d”,&a[i][j]);
}
printf(“\nEnter the elements of B”)
for (i=0;i<r2;i++)
for (j=0;j<c2;j++)
scanf(“%d”,&b[i][j]);
for (i=0;i<r2;i++)
{
for (j=0;j<c2;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf(“d\t”,c[i][j]);
} printf(“\n”);
}
else
printf(“\nAddition is not possible”);
}
RESULT:
Thus, the above C program was executed successfully.
STRING COPY
AIM:
To write a C program to copy one string to the other using built-in library function strcpy().
ALGORITHM:
Step 1:Start
Step 2: Declare two character arrays: str1[30], str2[30].
Step 3: Read the input string into str1 using gets().
Step 4: Copy the string from str1 to str2 using strcpy(str2, str1).
Step 5: Print "String1 = <str1> and String2 = <str2>".
Step 6: End
PROGRAM:
#include<stdio.h>
#incluce<conio.h>
#incluce<string.h>
void main()
{
char str1[30],str2[30];
clrscr();
printf(“\nEnter a string :\n”);
gets(str1);
strcpy(str2,str1);
printf(“\nString1 = %s and string 2=%s\n”,str1, str2);
getch();
}
OUTPUT:
enter a string : Good Habit
String1 = Good Habit and string 2= Good Habit
RESULT:
Thus, the above C program to copy one string to the other string was written,executed and verified
successfully.
Write a C program to concatenate one string with the other string using Library functions.
AIM:
To write a C program to concatenate one string with the other string using Library functions.
ALGORITHM:
Step 1 :Start
Step 2: Declare two character arrays: str1[30], str2[30].
Step 3: Read the input string into str1 using gets().
Step 4: Read the input string into str2 using gets().
Step 5:Print "Before concatenation: String1 = <str1>, String2 = <str2>".
Step 6:Concatenate str2 to str1 using strcat(str1, str2).
Step 7: Print "After concatenation: String1 = <str1>, String2 = <str2>".
Step 8: Stop
PROGRAM:
#include<stdio.h>
#incluce<conio.h>
#incluce<string.h>
void main()
{
char str1[30],str2[30];
clrscr();
printf(“\nEnter first string :\n”);
gets(str1);
printf(“\nEnter second string :\n”);
gets(str2);
printf(“\nBefore concatenation \n”);
printf(“\nString1 = %s and string 2=%s\n”,str1, str2);
strcat(str1,str2);
printf(“\nAfter concatenation \n”);
printf(“\nString1 = %s and string 2=%s\n”,str1, str2);
getch();
}
OUTPUT:
RESULT:
Thus, the above C program to concatenate one string with the other string was written, executed and
verified successfully.
AIM:
To write a C program to compare two strings using Library functions.
ALGORITHM:
Step 1:Start
Step 2: Declare two character arrays: str1[30], str2[30].
Step 3: Read the input string into str1 using gets().
Step 4: Read the input string into str2 using gets().
Step 5: Print "Before concatenation: String1 = <str1>, String2 = <str2>".
Step 6:Concatenate str2 to str1 using strcat(str1, str2).
Step 7: Print "After concatenation: String1 = <str1>, String2 = <str2>".
Step 8: End
PROGRAM:
#include<stdio.h>
#incluce<conio.h>
#incluce<string.h>
void main()
{
char str1[30],str2[30];
clrscr();
printf(“\nEnter first string :\n”);
gets(str1);
printf(“\nEnter second string :\n”);
gets(str2);
if(strcmp(str1,str2)==0)
{
printf(“\nStrings are same”);
} else
{
printf(“\nStrings are not same”);
}
getch();
}
OUTPUT:
Enter first string : Chain
Enter second string : Chain
Strings are same
RESULT:
Thus, the above C program to compare two strings was written,executed and verified successfully.
AIM:
To Write a C program to swap two numbers by pass by value in functions.
ALGORITHM:
Step 1 : Start
Step 2 : Declare variables a and b
Step 3 : Input values for a and b.
Step 4 : Print "Before swapping: a = <value>, b = <value>".
Step 5 : Call the function swap(a, b). Inside swap function:
i. Declare a temporary variable temp.
ii. temp = a.
iii. a = b.
iv. b = temp.
v. Print "Inside swap function: a = <value>, b = <value>".
Step 6: Print "After swapping in main: a = <value>, b =
<value>". Step 7: End
PROGRAM:
#include <stdio.h>
#include<conio.h>
void swap (int a, int
b); int main()
{
int a,b;
clrscr();
printf(“\nEnter the values of a and b”);
scanf(“%d%d”,&a,&b); printf(“\
nBefore swapping \n”); printf(“a = %d
\t b= %d\n”,a,b); swap(a,b);
printf(“\nAfter swapping \n”);
printf(“a = %d \t b= %d\n”,a,b);
getch();
return 0;
}
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
RESULT:
Thus, the above C program was executed successfully.
AIM:
To pass one dimensional array to a function in order to implement the pass by reference.
ALGORITHM:
Start
1. Declare an array a[30] and integers n, i.
2. Read n.
3. Print "Enter the array values".
4. For i = 0 to n-1:
Read a[i].
5. Call update(a, n). Function update(a[], n)
6. For i = 0 to n-1:
a[i] = a[i] *
a[i].
7. Return to main.
8. Print "After updating the array a in subfunction…".
9. For i = 0 to n-
1: Print a[i].
End
PROGRAM:
#include <stdio.h>
#include<conio.h>
void update (int [],int
); int main()
{
int a[30],i,n;
clrscr();
printf(“\nEnter the values of n \n”);
scanf(“%d”,&n);
printf(“\nEnter the array values \n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“\nPassing 1 D array to the function update\n”);
update(a, n);// Passing the address reference to the function
printf(“\nAfter updating the array a in subfunction… \n”);
for(i=0;i<n;i++)
{
printf(“a[%d] = %d\n”,i,a[i]);
}
getch();
return 0;
}
OUTPUT:
Enter the values of n 5
Enter the array values 10 20 11 12 13
Passing 1 D array to the function update
After updating the array a in subfunction…
a[0]=100
RESULT:
Thus, the above C program was executed successfully.
AIM:
To do the sum of integers using pointer variable implemented in C program.
ALGORITHM:
Start
1. Declare variables: n, i, sum = 0, *ptr
2. Print "Enter number of elements:"
3. Read n
4. Allocate memory for n integers: ptr = (int*) calloc(n, sizeof(int))
5. If ptr == NULL:
a. Print "Error! memory not allocated."
b. Exit the program
6. Print "Enter elements:"
7. For i = 0 to n - 1:
a. Read element into *(ptr + i)
b. Add *(ptr + i) to sum
8. Print "Sum = ", sum
9. Free allocated memory: free(ptr)
10. End
PROGRAM:
#include<stdio.h>
void main()
{
int *p, sum, I;
Int x[5] = {10, 20, 30, 40, 50);
i=0;
p=x; //initialization of integer pointer p to the base address of an array x
printf(“\nElement Value Address\n\n”);
while(i<5)
{
printtf(“ x[%d]\t%d\t%u\n”, i, *p, p);
sum = sum + *p;
i++;
p++;
}
printf(“ \nSum =%d\n”, sum); printf(“\
n &x[0] = %u\n”, x[0]); printf(“\n p =
%u \n”,p);
}
RESULT:
Thus, the above C program was executed successfully.
AIM: To implement the array of pointer concept using character array in C program.
ALGORITHM:
Start
1. Declare an array of pointers: name[3]
a. Assign "New Zealand" to name[0]
b. Assign "Australia" to name[1]
c. Assign "India" to name[2]
2. Print "Using array of pointers"
3. For i = 0 to 2:
a. Print the string stored at *(name + i)
4. End
PROGRAM:
#include <stdio.h>
void main()
{
char *name[3] = { “New Zealand”, “Australia” ,“India” };
printf(“\nUsing array of poitners\n”);
for(i=0; i<3;i++)
{
printf(“%s\n”, *(name+i));
}
}
OUTPUT:
New Zealand
Australia
India
RESULT:
Thus, the above C program was executed successfully.
AIM:
To Write a C program to calculate the sum of n numbers entered by the user.
ALGORITHM:
1. Print a prompt: "Enter number of elements".
2. Read the integer value n from the user.
3. Use calloc to allocate memory for n
integers. ptr = (int*) calloc(n,
sizeof(int));
4. Check if memory allocation was successful:
If ptr == NULL, print an error message: "Error! memory not allocated."
Exit the program if memory allocation fails.
5. Print a prompt: "Enter elements".
6. Loop through i from 0 to n - 1:
Read each integer value from the user and store it in ptr[i] using pointer arithmetic:
scanf("%d", ptr + i);.
7. Initialize a variable sum to 0.
8. Loop through i from 0 to n - 1:
Add the value stored at ptr[i] to sum: sum += *(ptr + i);.
Print the sum using: printf("Sum = %d", sum);.
8. Use the free function to release the dynamically allocated memory: free(ptr);.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("\nEnter number of elements: ");
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int));
if(ptr == NULL)
{
printf("\nError! memory not allocated.");
exit(0);
}
printf("\nEnter elements: ");
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("\nSum =%d", sum);
free(ptr);
return 0;
}
RESULT:
Thus, the above C program was executed successfully.
AIM: To write algorithm and C program for getting the book details using C structure.
ALGORITHM:
Start
1. Define a structure `book` with fields:
- `name` (character array)
- `price` (float)
- `pages` (integer)
2. Declare variables `b1`, `b2`, `b3` of type `struct book`
3. Print "Enter names, prices & number of pages of 3 books"
4. For each book:
a. Prompt user to input name, price, and pages
b. Read the inputs using `scanf`
5. Print "And this is what you entered:"
6. For each book:
a. Print name, price, and pages using `printf`
End
PROGRAM:
# include <stdio.h>
struct book
{
char name[30];
float price ;
int pages ;
};
int main( )
{
struct book b1, b2, b3 ;
printf ( "\nEnter names, prices & no. of pages of 3 books\n" )
; scanf ( "\n%s%f %d", b1.name, &b1.price, &b1.pages ) ;
scanf ( "\n%s %f %d", b2.name, &b2.price, &b2.pages ) ;
scanf ( "\n%s %f %d", b3.name, &b3.price, &b3.pages ) ;
printf ( "\nAnd this is what you entered\n" ) ;
printf ( "\n%s %f %d\n", b1.name, b1.price, b1.pages )
; printf ( "\n%s %f %d\n", b2.name, b2.price, b2.pages
) ; printf ( "\n%s %f %d\n", b3.name, b3.price,
b3.pages ) ; return 0 ;
}
RESULT:
Thus, the above C program was executed successfully.