CP Lab Manual
CP Lab Manual
I & II Semester
Common to all branches of UG Engineering & Technology
Percentage Grade
>=90 A
>= 80 and <90 B
>=70 and <80 C
>=50 and <70 D
>=36 and < 50 E
<36 F
10. Write a C program to print the week day by reading the day number using CO2
switch case.
Exp No.3 Control Structures (while,do-while,for,break,continue,nested for loops) CO2
CP Lab Manual 1FY3-24 / 2FY3-24
11. Write a C program to raise the maximum to the power of another number. CO2
15. Write a C program to find the sum of individual digits of a given number. CO2
16. Write a C program to check whether the given number is an Armstrong CO2
number.
17. Write a C program to check whether the given number is a prime number. CO2
18. Write a C program to check whether the given number is a palindrome number. CO2
19. Write a C program to generate prime numbers between the given ranges. CO2
20. Write a C program to evaluate the expression for sine series. CO2
21. Write a C program to print and find the sum of the following series: CO2
S=1–3+5–7+9…N
21(a)
S = -2 + 9 – 28 + 65 - ….N
21(b)
22. Write a C program to generate following layout:- CO2
22(a) *
**
***
****
A
22(b)
AB
ABC
AB CD
22(c) 1
1 2
1 2 3
CP Lab Manual 1FY3-24 / 2FY3-24
1 2 3 4
23. Write a menu driven program to perform arithmetic operations using do-while. CO2
Exp No.4 One-D Arrays CO3
25. Write a C program to read ‘n’ numbers and to count the number of positives, CO3
negatives and zeros in it.
26. Write a C program to read ‘n’ numbers and to find the maximum and minimum CO3
value.
27. Write a C program to read ‘n’ numbers and to sort them in ascending / CO3
descending order.
28. Write a C program to read ‘n’ numbers and to search a particular element in CO3
that array.
Exp No.5 Two-D Arrays CO3
31. Write a C program to display the mirror image of the given matrix. CO3
32. Write a C program to sum the row elements (individual rows) in the given CO3
matrix.
Exp No.6 Strings CO3
33. S1,s2,s3 are three string variables. Write a program to read two strings CO3
constants into s1 and s2 and compare whether they are equal or not. If they are
not, join them together. Then copy the contents of s1 to the variable s3. At the
end, the program should print the contents of all the three variables and their
lengths.
34. Write a menu driven C program to simulate the copy, reverse and concat string CO3
functions[don’t use built-in functions]
Exp No.7 Pointers CO5
35. WAP to find sum of even and odd elements in an array using pointers. CO5
36. WAP to search a particular element inside an array using pointers. CO5
CP Lab Manual 1FY3-24 / 2FY3-24
39. WAP to swap two numbers using functions.(call by value and call by reference) CO4
40. Write a C program to check whether the given number is an Armstrong number CO4
or not using function.
41. Write a C program to reverse a number using function. CO4
43. Design a structure student with the elements roll no., m1, m2, m3(three subject CO4
marks), tot, avg and grd.Read the required data for 5 students and compute their
total,percentage and grade and display their result.Also have a function to
update marks for a particular roll no. student.
Percentage Grade
>=90 A
>= 80 and <90 B
>=60 and <80 C
>=50 and <60 D
>=36 and < 50 E
<36 F
44. Design a structure emp with the elements empno, name, address, phone_no. CO4
Make address as a nested structure with door_no, street_name, city, pincode.
Have phone_no as a union with landline or mobile_no. Read 5 employee details
using function. Search for a specific employee based on phone_no. Sort the
employees based on pincode and display their details.
Exp No.11 File Handling CO5
45. Create a menu driven program that takes the user input and store it into the file CO5
and reads it back.
46. Create a program that stores 10 integer numbers in a file named DATA. WAP CO5
to read these numbers and then write all odd numbers to a file to be called ODD
and all even numbers to a file to be called EVEN.
Exp No.12 Dynamic Memory Allocation CO5
CP Lab Manual 1FY3-24 / 2FY3-24
47. WAP to find sum of all the elements of an array whose memory is allocated at CO5
run time.
Exp No.13 Command Line Arguments CO5
Software requirements:
Code::Blocks IDE
Dev C++ IDE
CP Lab Manual 1FY3-24 / 2FY3-24
EXPERIMENT NO. 1
PROGRAM:
/**
* C program to perform all arithmetic operations
*/
#include <stdio.h>
int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div;
/*
* Input two numbers from user
*/
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);
/*
* Perform all arithmetic operations
*/
sum = num1 + num2;
sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
mod = num1 % num2;
/*
* Print result of all arithmetic operations
*/
printf("SUM = %d\n", sum);
printf("DIFFERENCE = %d\n", sub);
printf("PRODUCT = %d\n", mult);
printf("QUOTIENT = %f\n", div);
printf("MODULUS = %d", mod);
return 0;
}
CP Lab Manual 1FY3-24 / 2FY3-24
OUTPUT:
-----------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 2
AIM: Write a C program to convert the temperature from Fahrenheit to
Centigrade and vice versa.
PROGRAM:
int main()
{
float fh,cl;
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
return 0;
}
CP Lab Manual 1FY3-24 / 2FY3-24
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 3
•Circle
•Square
•Triangle
PROGRAM:
Area of Circle:
#include <stdio.h>
#include <math.h>
int main()
{
CP Lab Manual 1FY3-24 / 2FY3-24
float radius, area;
scanf("%f", &radius);
area = 3.14159*radius*radius;
printf("Area of the circle = %.2f\n", area); // printing upto two decimal places
return 0;
}
OUTPUT:
Area of Square:
#include<stdio.h>
int main()
{
int side, area;
return (0);
}
Output:
CP Lab Manual 1FY3-24 / 2FY3-24
Area of Triangle:
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, s, area;
s = (a+b+c)/2; // Semiperimeter
area = sqrt(s*(s-a)*(s-b)*(s-c));
return 0;
}
Output:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 4
CP Lab Manual 1FY3-24 / 2FY3-24
AIM: Write a C program to swap two numbers:
PROGRAM:
#include <stdio.h>
int main()
{
double num1, num2, temp;
printf("Before swapping:\n");
//%.2lf is to have two digits after the decimal point
printf("num1 is: %.2lf and num2 is: %.2lf\n", num1, num2);
// Assigning the value of num1 to the temp variable
temp = num1;
printf("After swapping:\n");
printf("num1 is: %.2lf and num2 is: %.2lf", num1, num2);
return 0;
CP Lab Manual 1FY3-24 / 2FY3-24
}
OUTPUT:
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b); //consider two numbers as 4 and 5
a = a * b; // a = 4 * 5 = 20
b = a / b; // b = 20 / 5 = 4
a = a / b; // a = 20 / 4 = 5
printf("Numbers after swapping: %d %d", a, b);
}
Output:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 5
PROGRAM:
#include<stdio.h>
int main()
CP Lab Manual 1FY3-24 / 2FY3-24
{
int p,r,t,int_amt;
printf("Input principle, Rate of interest & time to find simple interest: \n");
scanf("%d%d%d",&p,&r,&t);
int_amt=(p*r*t)/100;
printf("Simple interest = %d",int_amt);
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 6
AIM: Write a C program to find whether the given number is odd or even.
PROGRAM:
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if (n%2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
CP Lab Manual 1FY3-24 / 2FY3-24
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 7
AIM: Write a C program to check whether the given year is leap year.
PROGRAM:
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 8
PROGRAM:
#include<stdio.h>
int main()
{
int num1,num2,num3;
if((num1>num2)&&(num1>num3))
printf("\n Number1 is greatest");
else if((num2>num3)&&(num2>num1))
printf("\n Number2 is greatest");
else
printf("\n Number3 is greatest");
return 0;
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 9
Percentage Grade
>=90 A
>= 80 and <90 B
>=70 and <80 C
>=50 and <70 D
>=36 and < 50 E
<36 F
PROGRAM:
#include<stdio.h>
int main()
{
int m1, m2, m3, m4, m5;
int avg;
avg = (m1+m2+m3+m4+m5) / 5;
CP Lab Manual 1FY3-24 / 2FY3-24
if(avg<36)
{
printf("Grade F");
}
else if(avg>=36 && avg<50)
{
printf("Grade E");
}
else if(avg>=50 && avg<70)
{
printf("Grade D");
}
else if(avg>=70 && avg<80)
{
printf("Grade C");
}
else if(avg>=80 && avg<90)
{
printf("Grade B");
}
else
{
printf("Grade A+");
}
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 10
AIM: Write a C program to print the week day by reading the day number
using switch case.
CP Lab Manual 1FY3-24 / 2FY3-24
PROGRAM:
/**
* C program to print day of week using switch case
*/
#include <stdio.h>
int main()
{
int week;
switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
}
return 0;
}
CP Lab Manual 1FY3-24 / 2FY3-24
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 11
PROGRAM:
#include <stdio.h>
int main()
{
int base, exp;
long long result = 1;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exp);
while (exp != 0)
{
result *= base;
--exp;
}
printf("Answer = %lld", result);
return 0;
CP Lab Manual 1FY3-24 / 2FY3-24
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 12
PROGRAM:
#include <stdio.h>
int main()
{
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 13
PROGRAM:
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 14
PROGRAM:
#include <stdio.h>
int main()
{
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}
CP Lab Manual 1FY3-24 / 2FY3-24
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 15
AIM: Write a C program to find the sum of individual digits of a given number.
PROGRAM:
#include <stdio.h>
void main()
{
long num, temp, digit, sum = 0;
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 16
PROGRAM:
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 17
PROGRAM:
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 18
PROGRAM:
#include <stdio.h>
int main()
{
int n, reversedN = 0, remainder, originalN;
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 19
AIM: Write a C program to generate prime numbers between the given ranges.
PROGRAM:
#include <stdio.h>
int main()
{
if (low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", low);
++low;
}
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
CP Lab Manual 1FY3-24 / 2FY3-24
EXPERIMENT NO. 20
PROGRAM:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float accuracy, term, denominator, x, sinx, sinval;
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 21
AIM: Write a C program to print and find the sum of the following series:
21 (a). S = 1 – 3 + 5 – 7 + 9 … N
PROGRAM:
#include<stdio.h>
int main()
{
int i,N,res,j;
return 0;
}
OUTPUT:
21 (b). S = -2 + 9 – 28 + 65 - ….N
PROGRAM:
#include<stdio.h>
CP Lab Manual 1FY3-24 / 2FY3-24
int main()
{
int i,N,res,j;
return 0;
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 22
a)
*
**
***
****
PROGRAM:
#include<stdio.h>
int main()
{
int n, i, j;
return 0;
CP Lab Manual 1FY3-24 / 2FY3-24
}
OUTPUT:
b)
A
AB
ABC
AB CD
PROGRAM:
#include<stdio.h>
int main()
{
int n, i, j, k;
char ch;
return 0;
}
OUTPUT:
c)
1
1 2
1 2 3
1 2 3 4
PROGRAM:
#include<stdio.h>
int main()
{
int n, i, j, k;
char ch;
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 23
CP Lab Manual 1FY3-24 / 2FY3-24
AIM: Write a menu driven program to perform arithmetic operations using do-
while.
PROGRAM:
#include<stdio.h>
int main()
{
float num1,num2,ans;
int opt;
//Displaying menu
printf("\n-----Main Menu----\n1.Addition");
printf("\n2.Subtraction\n3.Multiply\n4.Divide\n5.Exit");
printf("\nEnter your choice : ");
scanf("%d",&opt);
switch(opt)
{
case 1:
ans = num1+num2;
printf("\nThe addition of 2 numbers is : %f",ans);
break;
case 2:
ans = num1-num2;
printf("\nThe differnce of 2 numbers is : %f",ans);
break;
case 3:
ans = num1*num2;
printf("\nThe product of 2 numbers is : %f",ans);
break;
case 4:
ans = num1/num2;
printf("\nThe division of 2 numbers is : %f",ans);
break;
case 5:
CP Lab Manual 1FY3-24 / 2FY3-24
break;
default: //error message for wrong choice
printf("\nYou Entered Wrong Choice\n");
break;
}
}while(opt!=5);
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
CP Lab Manual 1FY3-24 / 2FY3-24
EXPERIMENT NO. 24
PROGRAM:
#include<stdio.h>
int main()
{
int a[100], i, j, Size, Temp;
while (i < j)
{
Temp = a[i];
a[i] = a[j];
a[j] = Temp;
i++;
j--;
}
return 0;
}
CP Lab Manual 1FY3-24 / 2FY3-24
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 25
AIM: Write a C program to read ‘n’ numbers and to count the number of
positives, negatives and zeros in it.
PROGRAM:
int main()
{
int limit, num, positive = 0, negative = 0, zero = 0;
while(limit)
{
scanf("%d", &num);
if(num > 0)
{
positive++;
}
CP Lab Manual 1FY3-24 / 2FY3-24
else if(num < 0)
{
negative++;
}
else
{
zero++;
}
limit--;
}
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
CP Lab Manual 1FY3-24 / 2FY3-24
EXPERIMENT NO. 26
AIM: Write a C program to read ‘n’ numbers and to find the maximum and
minimum value.
PROGRAM:
#include <conio.h>
int main()
{
int a[1000],i,n,min,max;
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
return 0;
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 27
AIM: Write a C program to read ‘n’ numbers and to sort them in ascending /
descending order.
PROGRAM:
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 28
PROGRAM:
#include <stdio.h>
int main()
{
CP Lab Manual 1FY3-24 / 2FY3-24
int arr[MAX_SIZE];
int size, i, toSearch, found;
/*
* If element is not found in array
*/
if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array", toSearch);
}
return 0;
CP Lab Manual 1FY3-24 / 2FY3-24
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 29
PROGRAM:
#include<stdio.h>
int main()
{
int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);
/*
printing the second matrix
*/
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", second[c][d]);
}
printf("\n");
}
/*
finding the SUM of the two matrices
and storing in another matrix sum of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];
/*
finding the DIFFERENCE of the two matrices
CP Lab Manual 1FY3-24 / 2FY3-24
and storing in another matrix difference of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 30
PROGRAM:
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
CP Lab Manual 1FY3-24 / 2FY3-24
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");
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
multiply[c][d] = sum;
sum = 0;
}
}
printf("\n");
}
}
CP Lab Manual 1FY3-24 / 2FY3-24
return 0;
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 31
AIM: Write a C program to display the mirror image of the given matrix.
PROGRAM:
#include<stdio.h>
int main()
{
int r,i,j;
printf("Enter number of rows:");
CP Lab Manual 1FY3-24 / 2FY3-24
scanf("%d",&r);
int a[r][r];
printf("Enter %d matrix elements:\n",r*r);
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
scanf("%d",&a[i][j]);
}
printf("Matrix Elements:\n");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\nMirror Image of Matrix:\n");
for(i=0;i<r;i++)
{
printf("\n");
for(j=r-1;j>=0;j--)
{
printf("%d\t",a[i][j]);
}
}
return 0;
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 32
AIM: Write a C program to sum the row elements (individual rows) in the
given matrix.
PROGRAM:
#include <stdio.h>
void main ()
{
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 33
AIM: s1, s2, s3 are three string variables. Write a program to read two strings
constants into s1 and s2 and compare whether they are equal or not. If they are not,
join them together. Then copy the contents of s1 to the variable s3. At the end, the
program should print the contents of all the three variables and their lengths.
PROGRAM:
#include <stdio.h>
#include <string.h>
int main ()
{
CP Lab Manual 1FY3-24 / 2FY3-24
char s1[20], s2[20], s3[40];
if(strcmp(s1, s2) != 0)
{
strcat(s1, s2);
}
strcpy(s3, s1);
printf("\nstring 1: %s",s1);
printf("\nstring 2: %s",s2);
printf("\nstring 3: %s",s3);
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 34
AIM: Write a menu driven C program to simulate the copy, reverse and concat
string functions [don’t use built-in functions]
PROGRAM:
#include<stdio.h>
CP Lab Manual 1FY3-24 / 2FY3-24
#include<conio.h>
int main()
{
char arr[30],s1[10],s2[10],s3[10];
int opt,i=0,j,len=0;
case 3:
{
printf(" Reverse the string ");
CP Lab Manual 1FY3-24 / 2FY3-24
printf("\nEnter the string");
scanf("%s",s1);
while(s1[i]!='\0')
{
len=len+1;
i++ ;
}
for(i=len-1;i>=0;i--)
{
printf("%c",s1[i]);
}
break;
}
case 4:
{
printf(" String copying \n");
printf("Enter 1st string :");
scanf("%s",s1);
printf("Enter 2st string :");
scanf("%s",s2);
while(s2[i]!='\0')
{
s1[i]=s2[i];
i++;
}
s1[i]='\0';
printf("%s",s1);
break;
}
default:
{
printf("Not is valid Option........");
}
}
return 0;
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 35
AIM: WAP to find sum of even and odd elements in an array using pointers.
PROGRAM:
#include<stdio.h>
int main()
{
int array[5];
int i,sum=0;
int *ptr;
for(i=0;i<5;i++)
{
//*ptr refers to the value at address
sum = sum + *ptr;
CP Lab Manual 1FY3-24 / 2FY3-24
ptr++;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 36
PROGRAM:
#include <stdio.h>
#include<malloc.h>
int main(void)
int n,i,pos=-1,*a,element;
a=calloc(sizeof(int),n);
printf("Enter %d Elements:",n);
for(i=0;i<n;i++)
scanf("%d",a+i);
}
CP Lab Manual 1FY3-24 / 2FY3-24
for(i=0;i<n;i++)
if(*(a+i)==element)
{
pos=i;
break;
}
}
printf("%d is in %d",element,pos);
return 0;
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 37
AIM: WAP to perform menu driven program for arithmetic operations using
functions.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
CP Lab Manual 1FY3-24 / 2FY3-24
//function declarations
void display(float n1, float n2, char ch, float result);
void add(float n1, float n2);
void subtract(float n1, float n2);
void multiply(float n1, float n2);
void divide(float n1, float n2);
void rem(float n1, float n2);
void power(float n1, float n2);
//main function
int main()
{
float n1, n2;
int ch;
do{
printf("Enter two numbers: ");
scanf("%f %f", &n1, &n2);
printf("\n*****************");
printf("\n1.Addition");
printf("\n2.Subtraction");
printf("\n3.Multiplication");
printf("\n4.Division");
printf("\n5.Remainder");
printf("\n6.Power (x^y)");
printf("\n7.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
add(n1,n2);
break;
case 2:
subtract(n1,n2);
break;
case 3:
multiply(n1,n2);
break;
case 4:
divide(n1,n2);
break;
case 5:
rem(n1,n2);
break;
case 6:
CP Lab Manual 1FY3-24 / 2FY3-24
power(n1,n2);
break;
case 7:
printf("Thank You.");
exit(0);
default:
printf("Invalid input.");
printf("Please enter correct input.");
}
printf("\n**********************************\n");
}while(1);
return 0;
}
//function for displaying the result
void display(float n1, float n2, char ch, float result)
{
printf("%.2f %c %.2f = %.2f\n", n1, ch, n2, result);
}
//function for addition of two numbers
void add(float n1, float n2)
{
float result = n1 + n2;
display(n1, n2, '+', result);
}
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 38
AIM: WAP to compute the factorial value of a given number using call by
value.
PROGRAM:
#include<stdio.h>
long factorial(int);
main()
{
int number;
long fact = 1;
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
return ( result );
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 39
AIM: WAP to swap two numbers using functions.(call by value and call by reference)
PROGRAM:
Call By Value:
#include<stdio.h>
#include <stdio.h>
temp = num1;
num1 = num2;
num2 = temp;
return 0;
}
Call by reference:
#include <stdio.h>
/**
* *num1 - pointer variable to accept memory address
* *num2 - pointer variable to accept memory address
*/
void swap(int * num1, int * num2)
{
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
int main()
{
int n1, n2;
/*
* &n1 - & evaluate memory address of n1
* &n2 - & evaluate memory address of n2
CP Lab Manual 1FY3-24 / 2FY3-24
*/
swap(&n1, &n2);
return 0;
OUTPUT:
Call by Value:
Call by reference:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 40
AIM: Write a C program to check whether the given number is an Armstrong
number or not using function.
PROGRAM:
#include <math.h>
#include <stdio.h>
int main() {
int n, flag;
printf("Enter a positive integer: ");
scanf("%d", &n);
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 41
AIM: Write a C program to reverse a number using function.
PROGRAM:
#include<stdio.h>
int findReverse(int n)
{
int sum=0;
while (n!=0)
{
sum = sum*10 + n%10;
n /= 10;
}
return sum;
}
int main()
{
int number, reverse;
reverse = findReverse(number);
return 0;
}
CP Lab Manual 1FY3-24 / 2FY3-24
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 42
PROGRAM:
#include<stdio.h>
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 43
AIM: Design a structure student with the elements roll no., m1, m2, m3(three
subject marks), tot, avg and grd.Read the required data for 5 students and
compute their total,percentage and grade and display their result.Also have a
function to update marks for a particular roll no. student.
Percentage Grade
>=90 A
>= 80 and <90 B
>=60 and <80 C
>=50 and <60 D
>=36 and < 50 E
<36 F
PROGRAM:
#include<stdio.h>
int main()
{
struct student
{
int rollno;
char name[20];
int m1,m2,m3;
float percent;
CP Lab Manual 1FY3-24 / 2FY3-24
};
struct student s[20],t;
int i,j,n;
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
CP Lab Manual 1FY3-24 / 2FY3-24
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 44
AIM: Design a structure emp with the elements empno, name, address,
phone_no. Make address as a nested structure with door_no, street_name, city,
pincode. Have phone_no as a union with landline or mobile_no. Read 5
employee details using function. Search for a specific employee based on
phone_no. Sort the employees based on pincode and display their details.
PROGRAM:
#include<stdio.h>
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
void main()
{
int i;
struct Employee Emp[ 3 ]; //Statement 1
for(i=0;i<3;i++)
{
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 45
AIM: Create a menu driven program that takes the user input and store it into
the file and reads it back.
PROGRAM:
#include<stdlib.h>
#include <stdio.h>
int main()
{
fclose(fp);
return 0;
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 46
AIM: Create a program that stores 10 integer numbers in a file named DATA.
WAP to read these numbers and then write all odd numbers to a file to be
called ODD and all even numbers to a file to be called EVEN.
PROGRAM:
#include <stdio.h>
int main()
{
FILE *f1, *f2, *f3;
int number, i;
printf("Contents of DATA file\n\n");
f1 = fopen("DATA", "w"); /* Create DATA file */
for(i = 1; i <= 30; 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");
/* Read from DATA file */
while((number = getw(f1)) != EOF)
{
if(number %2 == 0)
putw(number, f3); /* Write to EVEN file */
else
putw(number, f2); /* Write to ODD file */
}
fclose(f1);
fclose(f2);
fclose(f3);
f2 = fopen("ODD","r");
f3 = fopen("EVEN", "r");
printf("\n\nContents of ODD file\n\n");
while((number = getw(f2)) != EOF)
CP Lab Manual 1FY3-24 / 2FY3-24
printf("%4d", number);
printf("\n\nContents of EVEN file\n\n");
while((number = getw(f3)) != EOF)
printf("%4d", number);
fclose(f2);
fclose(f3);
return 0;
}
OUTPUT:
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 47
AIM: WAP to find sum of all the elements of an array whose memory is
allocated at run time.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
CP Lab Manual 1FY3-24 / 2FY3-24
int main()
{
int *ptr; //declaration of integer pointer
int limit; //to store array limit
int i; //loop counter
int sum; //to store sum of all elements
//free memory
free(ptr); //hey, don't forget to free dynamically allocated memory.
return 0;
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24
------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 48
PROGRAM:
#include <stdio.h>
int count, s = 0;
void sum(int *, int *);
OUTPUT:
$ cc arg4.c
$ a.out 1 2 3 4
sum is 10
----------------------------------------------------------------------------------------------------