KEMBAR78
CP Lab Manual | PDF | Matrix (Mathematics) | Sine
0% found this document useful (0 votes)
119 views85 pages

CP Lab Manual

This document provides instructions and experiments for a Computer Programming lab course. It outlines 13 experiments covering basic C programming concepts like input/output, decision making, loops, arrays, strings, pointers, functions, recursion, structures, unions, file handling and dynamic memory allocation. The first experiment involves writing programs to perform simple arithmetic operations like addition, subtraction, multiplication, division and modulus.

Uploaded by

Praful Talesara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views85 pages

CP Lab Manual

This document provides instructions and experiments for a Computer Programming lab course. It outlines 13 experiments covering basic C programming concepts like input/output, decision making, loops, arrays, strings, pointers, functions, recursion, structures, unions, file handling and dynamic memory allocation. The first experiment involves writing programs to perform simple arithmetic operations like addition, subtraction, multiplication, division and modulus.

Uploaded by

Praful Talesara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 85

CP Lab Manual 1FY3-24 / 2FY3-24

I & II Semester
Common to all branches of UG Engineering & Technology

1FY3-24/ 2FY3-24: Computer Programming Lab

Credit: 1.5 Max. Marks: 75 (IA:45, ETE:30)


0L+0T+3P

S. No. Name Of Experiment CO Mapped


Exp No.1 Simple Programs (Using Preprocessor Directives, Input/Output CO1
Statements, Data Types, Variables)
1 Write a C program to perform simple arithmetic operations. CO1
2. Write a C program to convert the temperature from Fahrenheit to Centigrade CO1
and vice versa.
3. Write a C program to find the area of the following shapes: CO1
CircleSquareTriangle
4. Write a C program to swap two numbers: CO1
With a temporary variableWithout a temporary variable
5. Write a C program to compute Compound Interest.
Exp No.2 Decision Making (if,if-else,nested if-else,else-if ladder, switch-case) CO2
6. Write a C program to find whether the given number is odd or even. CO2
7. Write a C program to check whether the given year is leap year. CO2
8. Write a C program to find the greatest among three numbers. CO2
9. Write a C program to find the grade obtained by a student by reading marks in CO2
5 subjects:

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

12. Write a C program to compute the factorial of a given number. CO2

13. Write a C program to generate the Fibonacci series of n terms. CO2

14. Write a C program to reverse the given 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

24. Write a C program to reverse the ‘n’ numbers in an array. 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

29. Write a C program to perform matrix addition and subtraction. CO3

30. Write a C program to perform matrix multiplication. 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

Exp No.8 Functions CO4


37. WAP to perform menu driven program for arithmetic operations using CO4
functions.
38. WAP to compute the factorial value of a given number using call by value. CO4

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

Exp No.9 Recursion CO4

42. WAP to find factorial of a number using recursion. CO4

ExpNo.10 Structure and Union 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

48. WAP to find the sum of command line arguments. CO5

Software requirements:
 Code::Blocks IDE
 Dev C++ IDE
CP Lab Manual 1FY3-24 / 2FY3-24

EXPERIMENT NO. 1

AIM: Write a C program to perform simple arithmetic operations.

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:

/* C Program to convert temperature from Fahrenheit to Celsius and vice versa.*/


#include <stdio.h>

int main()
{

float fh,cl;

printf("\nEnter temperature in Fahrenheit: ");


scanf("%f",&fh);

cl= (fh - 32) / 1.8;


printf("Temperature in Celsius: %.2f",cl);

printf("\nEnter temperature in Celsius: ");


scanf("%f",&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

AIM: Write a C program to find the area of the following shapes:

•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;

printf("Enter the radius of a circle\n");

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;

printf("\nEnter the Length of Side : ");


scanf("%d", &side);

area = side * side;


printf("\nArea of Square : %d", 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;

printf("Enter sides of a triangle\n");


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

s = (a+b+c)/2; // Semiperimeter

area = sqrt(s*(s-a)*(s-b)*(s-c));

printf("Area of the triangle = %.2lf\n", area);

return 0;
}

Output:

------------------------------------------------------------------------------------------------------

EXPERIMENT NO. 4
CP Lab Manual 1FY3-24 / 2FY3-24
AIM: Write a C program to swap two numbers:

•With a temporary variable


•Without a temporary variable

PROGRAM:

With a temporary variable

#include <stdio.h>
int main()
{
double num1, num2, temp;

//Storing first number entered by user in num1


printf("Enter First Number: ");
scanf("%lf", &num1);

//Storing second number entered by user in num2


printf("Enter Second Number: ");
scanf("%lf",&num2);

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;

// Assigning the value of second number num2 to num1


num1 = num2;

/* Assigning the value of temp variable(which is the


* original value of num1) to the num2
*/
num2 = temp;

printf("After swapping:\n");
printf("num1 is: %.2lf and num2 is: %.2lf", num1, num2);

return 0;
CP Lab Manual 1FY3-24 / 2FY3-24
}
OUTPUT:

Without a temporary variable

#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

AIM: Write a C program to compute Simple Interest.

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);

// leap year if perfectly visible by 400


if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
// not a leap year if visible by 100
// but not divisible by 400
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
// all other years are not leap year
else
{
CP Lab Manual 1FY3-24 / 2FY3-24
printf("%d is not a leap year.", year);
}
return 0;
}
OUTPUT:

------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 8

AIM: Write a C program to find the greatest among three numbers.

PROGRAM:

#include<stdio.h>
int main()
{
int num1,num2,num3;

//Ask user to input any three integer numbers


printf("\nEnter value of num1, num2 and num3:");
//Store input values in variables for comparsion
scanf("%d %d %d",&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

AIM: Write a C program to find the grade obtained by a student by reading


marks in 5 subjects:

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;

printf("Enter your marks of 5 Subjects ");


scanf("%d %d %d %d %d",&m1, &m2, &m3, &m4, &m5);

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;

/* Input week number from user */


printf("Enter week number(1-7): ");
scanf("%d", &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

AIM: Write a C program to raise the maximum to the power of another


number.

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

AIM: Write a C program to compute the factorial of a given number.

PROGRAM:

#include <stdio.h>
int main()
{
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);

// shows error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i)
{
fact *= i;
}
CP Lab Manual 1FY3-24 / 2FY3-24
printf("Factorial of %d = %llu", n, fact);
}

return 0;
}

OUTPUT:

------------------------------------------------------------------------------------------------------

EXPERIMENT NO. 13

AIM: Write a C program to generate the Fibonacci series of n terms.

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: ");

for (i = 1; i <= n; ++i)


{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
CP Lab Manual 1FY3-24 / 2FY3-24
return 0;
}

OUTPUT:

------------------------------------------------------------------------------------------------------

EXPERIMENT NO. 14

AIM: Write a C program to reverse the given number.

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;

printf("Enter the number \n");


scanf("%ld", &num);
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
CP Lab Manual 1FY3-24 / 2FY3-24
num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Sum of the digits %ld = %ld\n", temp, sum);
}

OUTPUT:

------------------------------------------------------------------------------------------------------

EXPERIMENT NO. 16

AIM: Write a C program to check whether the given number is an Armstrong


number.

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;

result += remainder * remainder * remainder;


CP Lab Manual 1FY3-24 / 2FY3-24
// removing last digit from the orignal number
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

AIM: Write a C program to check whether the given number is a prime


number.

PROGRAM:

#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

for (i = 2; i <= n / 2; ++i) {

// condition for non-prime


if (n % i == 0) {
flag = 1;
break;
}
CP Lab Manual 1FY3-24 / 2FY3-24
}

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

AIM: Write a C program to check whether the given number is a palindrome


number.

PROGRAM:

#include <stdio.h>
int main()
{
int n, reversedN = 0, remainder, originalN;
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;

// reversed integer is stored in reversedN


while (n != 0)
CP Lab Manual 1FY3-24 / 2FY3-24
{
remainder = n % 10;
reversedN = reversedN * 10 + remainder;
n /= 10;
}

// palindrome if orignalN and reversedN are equal


if (originalN == reversedN)
printf("%d is a palindrome.", originalN);
else
printf("%d is not a palindrome.", originalN);

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()
{

int low, high, i, flag;


printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are: ", low, high);
CP Lab Manual 1FY3-24 / 2FY3-24
// iteration until low is not equal to high
while (low < high)
{
flag = 0;

// ignore numbers less than 2


if (low <= 1)
{
++low;
continue;
}

// if low is a non-prime number, flag will be 1


for (i = 2; i <= low / 2; ++i)
{

if (low % i == 0)
{
flag = 1;
break;
}
}

if (flag == 0)
printf("%d ", low);

// to check prime for the next number


// increase low by 1

++low;
}

return 0;
}

OUTPUT:

------------------------------------------------------------------------------------------------------
CP Lab Manual 1FY3-24 / 2FY3-24
EXPERIMENT NO. 20

AIM: Write a C program to evaluate the expression for sine series.

PROGRAM:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void main()
{
int n, x1;
float accuracy, term, denominator, x, sinx, sinval;

printf("Enter the value of x (in degrees) \n");


scanf("%f", &x);
x1 = x;
/* Converting degrees to radians */
x = x * (3.142 / 180.0);
sinval = sin(x);
printf("Enter the accuracy for the result \n");
scanf("%f", &accuracy);
term = x;
sinx = term;
n = 1;
do
{
denominator = 2 * n * (2 * n + 1);
term = -term * x * x / denominator;
sinx = sinx + term;
n = n + 1;
} while (accuracy <= fabs(sinval - sinx));
printf("Sum of the sine series = %f \n", sinx);
printf("Using Library function sin(%d) = %f\n", x1, sin(x));
}

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;

/*read value of N*/


printf("Enter the value of N: ");
scanf("%d",&N);

/*set res by 0*/


res=0;

/*calculate sum of the series*/


for(j=1, i=1;i<=N;i++)
{
if(i % 2 != 0)
{
if(j % 2 == 0)
{
printf(" - %d",i);
res = res - i;
CP Lab Manual 1FY3-24 / 2FY3-24
}
else
{
printf(" + %d",i);
res = res + i;
}
j++;
}
}

/*print the res*/

printf(" ==> %d\n",res);

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;

/*read value of N*/


printf("Enter the value of N: ");
scanf("%d",&N);

/*set res by 0*/


res=0;

/*calculate sum of the series*/


for(i=1;i<=N;i++)
{
if(i % 2 != 0)
{
printf(" - %d",(i*i*i + 1));
res = res - (i*i*i + 1);
}
else
{
printf(" + %d",(i*i*i + 1));
res = res + (i*i*i + 1);
}

/*print the res*/

printf(" ==> %d\n",res);

return 0;
}

OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24

------------------------------------------------------------------------------------------------------

EXPERIMENT NO. 22

AIM: Write a C program to generate following layout:-

a)
*
**
***
****

PROGRAM:

#include<stdio.h>

int main()
{
int n, i, j;

printf("Enter Number of Rows: ");


scanf("%d",&n);

for(i=1; i<=n; i++)


{
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}

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;

printf("Enter Number of Rows: ");


scanf("%d",&n);

for(i=1; i<=n; i++)


{
ch = 'A';

for(k=4; k>=i; k--)


{
printf(" ");
}
CP Lab Manual 1FY3-24 / 2FY3-24
for(j=1; j<=i; j++)
{
printf("%c",ch);
ch++;
}
printf("\n");
}

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;

printf("Enter Number of Rows: ");


scanf("%d",&n);
CP Lab Manual 1FY3-24 / 2FY3-24

for(i=1; i<=n; i++)


{
for(k=4; k>=i; k--)
{
printf(" ");
}

for(j=1; j<=i; j++)


{
printf("%d ",j);
}
printf("\n");
}

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;

//taking user input


do
{
printf("\nEnter the First Number : ");
scanf("%f",&num1);
printf("\nEnter the Second Number : ");
scanf("%f",&num2);

//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

AIM: Write a C program to reverse the ‘n’ numbers in an array.

PROGRAM:

#include<stdio.h>

int main()
{
int a[100], i, j, Size, Temp;

printf("\nPlease Enter the size of an array: ");


scanf("%d",&Size);

//Inserting elements into the array


for (i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}

j = i - 1; // Assigning j to Last array element


i = 0; // Assigning i to first array element

while (i < j)
{
Temp = a[i];
a[i] = a[j];
a[j] = Temp;
i++;
j--;
}

printf("\nResult of an Reverse array is: ");


for (i = 0; i < Size; i++)
{
printf("%d \t", a[i]);
}

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:

#include < stdio.h >

int main()
{
int limit, num, positive = 0, negative = 0, zero = 0;

printf("Enter the limit\n");


scanf("%d", &limit);

printf("Enter %d numbers\n", limit);

while(limit)
{
scanf("%d", &num);

if(num > 0)
{
positive++;
}
CP Lab Manual 1FY3-24 / 2FY3-24
else if(num < 0)
{
negative++;
}
else
{
zero++;
}

limit--;
}

printf("\nPositive Numbers: %d\n", positive);


printf("Negative Numbers: %d\n", negative);
printf("Number of zero: %d\n", zero);

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;

printf("Enter size of the array : ");


scanf("%d",&n);

printf("Enter elements in array : ");


for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

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;

printf("Enter the value of num \n");


scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
CP Lab Manual 1FY3-24 / 2FY3-24
}

OUTPUT:

------------------------------------------------------------------------------------------------------

EXPERIMENT NO. 28

AIM: Write a C program to read ‘n’ numbers and to search a particular


element in that array.

PROGRAM:

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()
{
CP Lab Manual 1FY3-24 / 2FY3-24
int arr[MAX_SIZE];
int size, i, toSearch, found;

/* Input size of array */


printf("Enter size of array: ");
scanf("%d", &size);

/* Input elements of array */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nEnter element to search: ");


scanf("%d", &toSearch);

/* Assume that element does not exists in array */


found = 0;

for(i=0; i<size; i++)


{
/*
* If element is found in array then raise found flag
* and terminate from loop.
*/
if(arr[i] == toSearch)
{
found = 1;
break;
}
}

/*
* 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

AIM: Write a C program to perform matrix addition and subtraction.

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);

printf("\nEnter the %d elements of the first matrix \n\n", m*n);


for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &first[c][d]);

printf("\nEnter the %d elements of the second matrix \n\n", m*n);


for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &second[c][d]);
CP Lab Manual 1FY3-24 / 2FY3-24
/*
printing the first matrix
*/
printf("\n\nThe first 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", first[c][d]);
}
printf("\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];

// printing the elements of the sum matrix


printf("\n\nThe sum of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}

/*
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];

// printing the elements of the diff matrix


printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", diff[c][d]);
}
printf("\n");
}

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24

------------------------------------------------------------------------------------------------------

EXPERIMENT NO. 30

AIM: Write a C program to perform matrix multiplication.

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");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

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");

for ( c = 0 ; c < p ; c++ )


for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of entered matrices:-\n");

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);

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 ()
{

static int array[10][10];


int i, j, m, n, sum = 0;

printf("Enter the order of the matrix\n");


scanf("%d %d", &m, &n);

printf("Enter the co-efficients of the matrix\n");


for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
CP Lab Manual 1FY3-24 / 2FY3-24
{
scanf("%d", &array[i][j]);
}
}

for (i = 0; i < m; ++i)


{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}

printf("Sum of the %d row is = %d\n", i, sum);


sum = 0;

sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}

printf("Sum of the %d column is = %d\n", j, sum);


sum = 0;

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];

printf("\nEnter String 1 : ");


scanf("%s",s1);

printf("\nEnter String 2 : ");


scanf("%s",s2);

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;

printf("Enter any option\n\n");


printf("1: Find out length of the string\n");
printf("2: Concatenate of the two string\n");
printf("3:Reverse of the string\n");
printf("4:Copy of the string\n");
printf("Enter the choice\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
{
printf("Enter any string \n");
scanf("%s",arr);
for(i=0;arr[i]!='\0';i++);
printf("The length of the string %d",i);
break;
}
case 2:
{
printf(" String Concatenation \n");
printf("\nEnter the First string:\n");
scanf("%s", s1);
printf("\nEnter Second string");
scanf("%s",s2);
for(i=0;s1[i]!='\0';i++)
{
s3[i]=s1[i];
}
s3[i]='\0';
for(j=0;j<=i;j++)
{
s3[i+j]=s2[j];
}
printf("The Concatenated string is %s",s3);
break;
}

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;

printf("\nEnter array elements (5 integer values):");


for(i=0;i<5;i++)
scanf("%d",&array[i]);

/* array is equal to base address


* array = &array[0] */
ptr = array;

for(i=0;i<5;i++)
{
//*ptr refers to the value at address
sum = sum + *ptr;
CP Lab Manual 1FY3-24 / 2FY3-24
ptr++;
}

printf("\nThe sum is: %d",sum);


}

OUTPUT:

------------------------------------------------------------------------------------------------------

EXPERIMENT NO. 36

AIM: WAP to search a particular element inside an array using pointers.

PROGRAM:

#include <stdio.h>
#include<malloc.h>
int main(void)

int n,i,pos=-1,*a,element;

printf("Enter size of array:");


scanf("%d",&n);

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

printf("Enter a element to find:");


scanf("%d",&element);

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

//function for subtraction of two numbers


void subtract(float n1, float n2)
{
float result = n1 - n2;
display(n1, n2, '-', result);
}
//function for multiplication of two numbers
void multiply(float n1, float n2)
{
float result = n1 * n2;
display(n1, n2, '*', result);
}

//function for division of two numbers


void divide(float n1, float n2)
{
float result = n1 / n2;
display(n1, n2, '/', result);
}

//function for calculating remainder


CP Lab Manual 1FY3-24 / 2FY3-24
void rem(float n1, float n2)
{
//Modulus operator only works on int data type
//Floating numbers are converted to int number
int num1 = n1;
int num2 = n2;
int result = num1%num2;
printf("%d %% %d = %d\n", num1, num2, result);
}
//function for calculating power
void power(float n1, float n2)
{
if(n2<0) printf("Second number should be +ve.");
else
{
float result=1.0;
for(int i=1; i<=n2; i++)
{
result *= n1;
}
display(n1, n2, '^', result);
}
}
OUTPUT:
CP Lab Manual 1FY3-24 / 2FY3-24

------------------------------------------------------------------------------------------------------

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;

printf("Enter a number to calculate it's factorial\n");


scanf("%d",&number);

printf("%d! = %ld\n", number, factorial(number));

return 0;
}

long factorial(int n)
{
int c;
long result = 1;

for( c = 1 ; c <= n ; c++ )


result = result*c;

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>

/* Swap function definition */


void swap(int num1, int num2)
{
int temp;

printf("In Function values before swapping: %d %d\n", num1, num2);

temp = num1;
num1 = num2;
num2 = temp;

printf("In Function values after swapping: %d %d\n\n", num1, num2);


}

/* main() function definition */


int main()
{
int n1, n2;

/* Input two integers from user */


printf("Enter two numbers: ");
scanf("%d%d", &n1, &n2);
CP Lab Manual 1FY3-24 / 2FY3-24

/* Print value of n1 and n2 in before swapping */


printf("In Main values before swapping: %d %d\n\n", n1, n2);

/* Function call to swap n1 and n2 */


swap(n1, n2);
printf("In Main values after swapping: %d %d", n1, n2);

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;

printf("In Function values before swapping: %d %d\n", *num1, *num2);

temp = *num1;
*num1 = *num2;
*num2 = temp;

printf("In Function values after swapping: %d %d\n\n", *num1, *num2);


}

/* main() function declaration */

int main()
{
int n1, n2;

printf("Enter two numbers: ");


scanf("%d%d", &n1, &n2);

printf("In Main values before swapping: %d %d\n\n", n1, n2);

/*
* &n1 - & evaluate memory address of n1
* &n2 - & evaluate memory address of n2
CP Lab Manual 1FY3-24 / 2FY3-24
*/
swap(&n1, &n2);

printf("In Main values after swapping: %d %d", 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 checkArmstrongNumber(int n);

int main() {
int n, flag;
printf("Enter a positive integer: ");
scanf("%d", &n);

// check Armstrong number


flag = checkArmstrongNumber(n);
if (flag == 1)
printf("%d is an Armstrong number.", n);
else
printf("%d is not an Armstrong number.", n);
return 0;
}

// function to check Armstrong number


int checkArmstrongNumber(int num) {
int originalNum, remainder, n = 0, flag;
double result = 0.0;

// store the number of digits of num in n


for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}

for (originalNum = num; originalNum != 0; originalNum /= 10) {


remainder = originalNum % 10;

// store the sum of the power of individual digits in result


result += pow(remainder, n);
}

// condition for Armstrong number


if (round(result) == num)
flag = 1;
CP Lab Manual 1FY3-24 / 2FY3-24
else
flag = 0;
return flag;
}
OUTPUT:

------------------------------------------------------------------------------------------------------
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;

printf("Enter a positive interger: ");


scanf("%d", &number);

reverse = findReverse(number);

printf("The reverse of %d is: %d", number, reverse);

return 0;
}
CP Lab Manual 1FY3-24 / 2FY3-24

OUTPUT:

------------------------------------------------------------------------------------------------------

EXPERIMENT NO. 42

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

PROGRAM:

#include<stdio.h>

long int multiplyNumbers(int n);

int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n)


{
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

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;

printf("\n enter the limit");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the roll no\n");
scanf("%d",&s[i].rollno);
printf("\n enter the name \n");
scanf("%s",s[i].name);
printf("\n enter the mark=");
scanf("%d",&s[i].m1);
printf("\n enter the mark=");
scanf("%d",&s[i].m2);
printf("\n enter the mark=");
scanf("%d",&s[i].m3);
s[i].percent=(s[i].m1+s[i].m2+s[i].m3)/3;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(s[i].percent<s[j].percent)
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
printf("\n display in desending order\n");
for(i=0;i<n;i++)
{
printf("\n rollno=%d",s[i].rollno);
printf("\n name=%s",s[i].name);
printf("\n mark1=%d",s[i].m1);
printf("\n mark2=%d",s[i].m2);
printf("\n mark3=%d",s[i].m3);
printf("\n percent=%f",s[i].percent);
}
return 0;
}

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++)
{

printf("\nEnter details of %d Employee",i+1);

printf("\n\tEnter Employee Id : ");


scanf("%d",&Emp[i].Id);

printf("\n\tEnter Employee Name : ");


scanf("%s",&Emp[i].Name);

printf("\n\tEnter Employee Age : ");


scanf("%d",&Emp[i].Age);

printf("\n\tEnter Employee Salary : ");


scanf("%ld",&Emp[i].Salary);
}
CP Lab Manual 1FY3-24 / 2FY3-24
printf("\nDetails of Employees");
for(i=0;i<3;i++)
printf("\n%d\t%s\t%d\t%ld",Emp[i].Id,Emp[i].Name,Emp[i].Age,Emp[i].Salary);

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()
{

FILE *fp; /* file pointer*/


char fName[20];

printf("\nEnter file name to create :");


scanf("%s",fName);

/*creating (open) a file*/


fp=fopen(fName,"w");
/*check file created or not*/
if(fp==NULL)
{
printf("File does not created!!!");
exit(0); /*exit from program*/
}

printf("File created successfully.");


/*writting into file*/
putc('A',fp);
putc('B',fp);
putc('C',fp);

printf("\nData written successfully.");


fclose(fp);

/*again open file to read data*/


fp=fopen(fName,"r");
if(fp==NULL)
{
printf("\nCan't open file!!!");
exit(0);
}
CP Lab Manual 1FY3-24 / 2FY3-24

printf("Contents of file is :\n");


printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));

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

printf("Enter limit of the array: ");


scanf("%d",&limit);

//declare memory dynamically


ptr=(int*)malloc(limit*sizeof(int));

//read array elements


for(i=0;i<limit;i++)
{
printf("Enter element %02d: ",i+1);
scanf("%d",(ptr+i));
}

//print array elements


printf("\nEntered array elements are:\n");
for(i=0;i<limit;i++)
{
printf("%d\n",*(ptr+i));
}

//calculate sum of all elements


sum=0; //assign 0 to replace garbage value
for(i=0;i<limit;i++)
{
sum+=*(ptr+i);
}
printf("Sum of array elements is: %d\n",sum);

//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

AIM: WAP to find the sum of command line arguments.

PROGRAM:

#include <stdio.h>

int count, s = 0;
void sum(int *, int *);

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


{
int i, ar[argc];
count = argc;
CP Lab Manual 1FY3-24 / 2FY3-24
for (i = 1; i < argc; i++)
{
ar[i - 1] = atoi(argv[i]);
}
sum(ar, ar + 1);
printf("%d", s);
}

/* computes sum of two numbers recursively */


void sum(int *a, int * b)
{
if (count == 1)
return;
s = s + *a + *b;
count -= 2;
sum(a + 2, b + 2);
}

OUTPUT:

$ cc arg4.c
$ a.out 1 2 3 4
sum is 10

----------------------------------------------------------------------------------------------------

You might also like