72
1 PROGRAMMING FOR PROBLEM SOLVING LAB MANUAL DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

1

PROGRAMMING FOR PROBLEM SOLVING

LAB MANUAL

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Page 2: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

2

Year : 2018 - 2019

Subject Code : CS106ES & CS206ES

Regulations : R18

Class & Branch : I B.Tech - I Semester

CE, ME & ECE

&

I B.Tech - II Semester

CSE & EEE

Prepared By

Mr. Vishnu Prasad Goranthala Mr. Ranjith Kumar Badugu Assistant Professor, CSE Assistant Professor, CSE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Page 3: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

3

INDEX

S.No. Name of the program Page

No.

1 Write the program for the simple, compound interest. 07

2

Write a program to find the max and min from the three numbers.

08

3

Write a simple program that prints the results of all the operators

available in C (including pre/ post increment, bitwise and/or/not, etc.).

Read required operand values from standard input.

09

4

Write program that declares Class awarded for a given percentage of

marks, where mark <40%= Failed, 40% to <60% = Second class,

60% to <70%=First class, >= 70% = Distinction. Read percentage from

standard input.

13

5

Write a program that prints a multiplication table for a given number.

14

6

Write a C program to read 3 numbers and print only the middle number

among them.

15

7

Write a C program to check whether given letter is alphabet or not if so

check it is vowel or not.

16

8

Write a C program, which takes two integer operands and one operator

from the user, performs the operation and then prints the result.

(Consider the operators +,-,*, /, % and use Switch Statement)

17

9

Write a C program to find the roots of quadratic equation.

20

10 Write a C program to determine if the given number is a prime number

or not. 21

11

Write a C program to find the factorial of a positive integer. 22

12

Write a C program to check whether given number is Armstrong or

Not.

23

Page 4: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

4

13

Write a C program to find the sum of individual digits of a positive

integer and test given number is palindrome.

24

14

Write a C program to generate all the prime numbers between

1 and ‘n’, where ‘n’ is a value supplies by the user.

25

15

Write a C program to find the minimum, maximum and average in an

array of integers.

26

16

Write a C program to perform the addition of two matrices. 27

17

Write a C program to perform the multiplication of two matrices after

verifying necessary conditions.

29

18

Write a C program to find the factorial of a given integer using

recursion technique.

31

19

Write a C program to generate first ‘n’ Fibonacci sequence using

recursion technique.

32

20

Write a C program to find x ^ n using recursion technique.

33

21

Write a C program to find the GCD (greatest common divisor) of two

given integers using recursion technique. 34

22

Write a C program to print the following format:

1

2 3

4 5 6

7 8 9 10

35

23

Write a program for reading elements using pointer into array and

display the values using array.

36

Page 5: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

5

24

Write a program for display values reverse order from array using

pointer.

37

25

Write a program through pointer variable to sum of n elements from

array.

38

26

Write a C program to create a file and store some data into it and

display the content of that file to standard output device.

39

27

Write a C program which copies one file to another, replacing all

lowercase characters with their uppercase equivalents.

40

28

Write a C program to merge two files into a third file. 42

29

Write a C program to create a binary file and store 10 numbers into it

and display the content of that file to standard output device.

44

30

Write a C program that uses non recursive function to search for a Key

value in a given list of integers using linear search method

45

31

Write a C program that uses non recursive function to search for a Key

value in a given sorted list of integers using binary search method.

46

32

Write a C program that implements the Bubble sort method to sort a

given list of integers in ascending order.

48

33

Write a C program that sorts the given array of integers using selection

sort in descending order

50

34

Write a C program that sorts the given array of integers using insertion

sort in ascending order

52

Page 6: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

6

Beyond the Syllabus

01

Write a C program that uses to perform the following:

a) Create a singly linked list of integers.

b) Display the contents of the above list.

66

02 Write a C Program to Implement a Stack using Linked List

68

35

Write a C program that sorts a given array of names

54

36

Write a C program to determine if the given string is a palindrome or

not (Spelled same in both directions with or without a meaning like

madam, civic, noon, abcba, etc.)

55

37

Write a C program to count the lines, words and characters in a given

text.

56

38

Write a C program to illustrate the following:

1. Simple structure concept.

2. Enumeration concept.

3. Typedef concept.

57

39

Write a C program to construct a pyramid of numbers as follows:

60

40

Write a C program to illustrate Macro’s concept (#define, #undef)

63

Page 7: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

7

1 Q: Write the program for the simple, compound interest

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

float simpleInterest(int p ,int t , float r);

float compoundInterest(int p, int t , float r);

int p,t;

float r,s,c;

clrscr();

printf("Enter P,T,R :\n");

scanf("%d%d%f",&p,&t,&r);

s=simpleInterest(p,t,r);

c=compoundInterest(p,t,r);

printf("Simple interest = %9.2f\n",s);

printf("Compound interest = %9.2f\n",c);

getch();

}

float simpleInterest(int p,int t , float r)

{

return((p*t*r)/100.0);

}

float compoundInterest(int p ,int t, float r)

{

return(p*pow((1+r/100),t)-p);

}

Output:

Page 8: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

8

2 Q: Write a program to find the max and min from the three numbers.

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

clrscr();

printf("\n Please enter the value of A: ");

scanf("%d",&a);

printf("\n Please enter the value of B: ");

scanf("%d",&b);

printf("\n Please enter the value of C: ");

scanf("%d",&c);

if (a<b && a<c)

printf("\n %d is Minimum which is value of A",a);

else if(b<a && b<c)

printf("\n %d is Minimum which is value of B",b);

else

printf("\n %d is Minimum which is value of C",c);

if (a>b && a>c)

printf("\n\n %d is Maximum which is value of A",a);

else if(b>a && b>c)

printf("\n\n %d is Maximum which is value of B",b);

else

printf("\n\n %d is Minimum which is value of C",c);

getch();

}

Output:

Page 9: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

9

3Q: usage of Operators:

// C Program to demonstrate the working of arithmetic operators

#include <stdio.h>

void main()

{

int a = 9,b = 4, c;

c = a+b;

printf("a+b = %d \n",c);

c = a-b;

printf("a-b = %d \n",c);

c = a*b;

printf("a*b = %d \n",c);

c=a/b;

printf("a/b = %d \n",c);

c=a%b;

printf("Remainder when a divided by b = %d \n",c);

}

Output:

a+b = 13

a-b = 5

a*b = 36

a/b = 2

Remainder when a divided by b=1

Page 10: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

10

// C Program to demonstrate the working of increment and decrement operators

#include <stdio.h>

Void main()

{

int a = 10, b = 100;

float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);

printf("--b = %d \n", --b);

printf("++c = %f \n", ++c);

printf("--d = %f \n", --d);

}

Output:

++a = 11

--b = 99

++c = 11.500000

++d = 99.500000

Page 11: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

11

// C Program to demonstrate the working of assignment operators

#include <stdio.h>

Void main()

{

int a = 5, c;

c = a;

printf("c = %d \n", c);

c += a; // c = c+a

printf("c = %d \n", c);

c -= a; // c = c-a

printf("c = %d \n", c);

c *= a; // c = c*a

printf("c = %d \n", c);

c /= a; // c = c/a

printf("c = %d \n", c);

c %= a; // c = c%a

printf("c = %d \n", c);

}

Output:

c = 5

c = 10

c = 5

c = 25

c = 5

c = 0

Page 12: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

12

// C Program to demonstrate the working of Relational operators

#include <stdio.h>

Void main()

{

int a = 5, b = 5, c = 10;

printf("%d == %d = %d \n", a, b, a == b); // true

printf("%d == %d = %d \n", a, c, a == c); // false

printf("%d > %d = %d \n", a, b, a > b); //false

printf("%d > %d = %d \n", a, c, a > c); //false

printf("%d < %d = %d \n", a, b, a < b); //false

printf("%d < %d = %d \n", a, c, a < c); //true

printf("%d != %d = %d \n", a, b, a != b); //false

printf("%d != %d = %d \n", a, c, a != c); //true

printf("%d >= %d = %d \n", a, b, a >= b); //true

printf("%d >= %d = %d \n", a, c, a >= c); //false

printf("%d <= %d = %d \n", a, b, a <= b); //true

printf("%d <= %d = %d \n", a, c, a <= c); //true

}

Output:

5 == 5 = 1

5 == 10 = 0

5 > 5 = 0

5 > 10 = 0

5 < 5 = 0

5 < 10 = 1

5 != 5 = 0

5 != 10 = 1

5 >= 5 = 1

5 >= 10 = 0

5 <= 5 = 1

5 <= 10 = 1

Page 13: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

13

4 Q: Write program that declares Class awarded

for a given percentage of marks, where mark

<40%= Failed, 40% to <60% = Second class,

60% to <70%=First class, >= 70% = Distinction.

Read percentage from standard input.

#include<stdio.h>

#include<conio.h>

void main()

{

float avg;

clrscr();

printf("Enter average percentage of marks:\n");

scanf("%f",&avg);

if(avg<40)

{

printf("you just failed!\n");

}

else if(avg<60)

{

printf("Second class!\n");

}

else if(avg<70)

{

printf("First class!\n");

}

else

{

printf("Distinction!\n");

}

getch();

}

Output:

Run1:

Run2:

Run3:

Run4:

Page 14: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

14

5 Q: Write a program that prints a multiplication table for a given number.

#include <stdio.h>

#include<conio.h>

void main()

{

int n, i=1;

clrscr();

printf("Enter a number: ");

scanf("%d",&n);

mtable:

{

printf("%d * %d = %d \n", n, i,

n*i);

i++;

if(i<=10)

{

goto mtable;

}

}

getch();

}

Output:

Page 15: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

15

6Q: Write a C program to read 3 numbers and print only the middle number

among them.

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

clrscr();

printf("Enter 3 numbers:\n");

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

if((a==b)&&(b==c))

{

printf("all inserted numbers are equal!\n");

}

else

if(((a==b)&&(b!=c))||((b==c)&&(b!=a))||((a==c)

&&(c!=b)))

{

printf("may be ab or ac or bc are equal!\n");

}

else if(((a>b)&&(a<c))||((a<b)&&(a>c)))

{

printf("%d is a middle one!\n",a);

}

else if(((b>c)&&(b<a))||((b<c)&&(b>a)))

{

printf("%d is middle one!\n",b);

}

else

{

printf("%d is middle one!\n",c);

}

getch();

}

Output:

Run 1:

Run2:

Page 16: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

16

7 Q: Write a C program to check whether given letter is alphabet or not if so check

it is vowel or not.

#include <stdio.h>

void main()

{

char ch;

clrscr();

printf("Enter a character:\n");

scanf("%c",&ch);

if( (ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))

{

switch(ch)

{

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':

case 'A':

case 'E':

case 'I':

case 'O':

case 'U':

printf("%c is a vowel!\n",ch);

break;

default:

printf("%c is a consonant!\n",ch);

}

}

else

{

printf(" inserted leter may be not alphabet!\n");

}

getch();

}

Output:

Run1:

Run2:

Run3:

Run 4:

Page 17: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

17

8Q:Write a c program to illustrate menu based format using switch case statement.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int add (int, int);

int sub (int, int);

int mul (int, int);

int divi (int, int);

int main()

{

int i,x,y,c;

printf("Enter two numbers : ");

scanf ("%d %d",&x,&y);

printf("1. addition\n");

printf("2. subtraction\n");

printf("3. multiplication\n");

printf("4. division\n");

printf("5. exit\n\n");

printf("Which action you want to perform : ");

scanf ("%d",&i);

switch(i)

{

case 1:

c=add(x , y);

printf("Answer = %d\n",c);

break;

case 2:

c=sub(x , y);

printf("Answer = %d\n",c);

break;

case 3:

c=mul(x , y);

printf("Answer = %d\n",c);

break;

case 4:

c=divi(x,y);

printf("Answer = %d\n",c);

Page 18: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

18

break;

case 5 :

exit(0);

break;

default :

printf("Enter valid number\n");

}

}

int add(int x, int y)

{

int c;

c = (x+y);

return c ;

}

int sub(int x, int y)

{

int c;

c = (x-y);

printf("Answer = %d\n",c);

}

int mul(int x, int y)

{

int c;

c = (x*y);

printf("Answer = %d\n",c);

}

int divi(int x, int y)

{

int c;

c = (x/y);

printf("Answer = %d\n",c);

}

Output:

Page 19: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

19

The standard form of a quadratic equation is:

ax2 + bx + c = 0, where

a, b and c are real numbers and

a ≠ 0

The term b2-4ac is known as the discriminant of a quadratic equation. The

discriminant tells the nature of the roots.

If discriminant is greater than 0, the roots are real and different.

If discriminant is equal to 0, the roots are real and equal.

If discriminant is less than 0, the roots are complex and different.

9Q: Write a C program to find the roots of quadratic equation.

Page 20: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

20

#include <stdio.h>

#include <math.h>

#include<conio.h>

void main()

{

double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;

clrscr();

printf("Enter coefficients a, b and c: ");

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

discriminant = b*b-4*a*c;

// condition for real and different roots

if (discriminant > 0)

{

// sqrt() function returns square root

root1 = (-b+sqrt(discriminant))/(2*a);

root2 = (-b-sqrt(discriminant))/(2*a);

printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);

}

//condition for real and equal roots

else if (discriminant == 0)

{

root1 = root2 = -b/(2*a);

printf("root1 = root2 = %.2lf;", root1);

}

// if roots are not real

else

{

realPart = -b/(2*a);

imaginaryPart = sqrt(-discriminant)/(2*a);

printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imaginaryPart,

realPart, imaginaryPart);

}

getch();

}

Output:

Page 21: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

21

10 Q: Write a C program for checking whether a given number is prime or not.

#include<conio.h>

#include<stdio.h>

void main()

{

int n,fcount=0,i;

clrscr();

printf("Enter the number:\n");

scanf("%d",&n);

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

{

if(n%i==0)

{

fcount=fcount+1;

}

}

if(fcount==2)

{

printf("%d is a prime number!\n",n);

}

else

{

printf("%d is not a prime number!\n",n);

}

getch();

}

Output:

Run 1:

Enter the number:

4

4 is not a prime number!

Run 2:

Enter the number:

17

17 is a prime number!

Page 22: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

22

11 Q: Write a C program to find the factorial of a positive integer.

#include <stdio.h>

void main()

{

int n, i;

long int factorial = 1;

clrscr();

printf("Enter an integer: ");

scanf("%d",&n);

// show 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++)

{

factorial =factorial*i;

}

printf("Factorial of %d = %ld", n, factorial);

}

getch();

}

Output:

Run1:

Run 2:

Page 23: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

23

12 Q: A program for checking whether given number is Armstrong or not.

#include<stdio.h>

#include<conio.h>

main()

{

int n,r,sum=0,temp;

clrscr();

printf("enter the number=");

scanf("%d",&n);

temp=n;

while(n>0)

{

r=n%10;

sum=sum+(r*r*r);

n=n/10;

}

if(temp==sum)

{

printf("Armstrong number ");

}

else

{

printf("not Armstrong number");

}

getch();

}

Output:

Run1:

enter the number=153

Armstrong number

Run 2:

enter the number=5

not Armstrong number

Page 24: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

24

13 Q:A program for checking whether the given number is palindrome or not.

#include<stdio.h>

#include<conio.h>

main()

{

int n,r,sum=0,temp;

clrscr();

printf("enter the number=");

scanf("%d",&n);

temp=n;

while(n>0)

{

r=n%10;

sum=(sum*10)+r;

n=n/10;

}

if(temp==sum)

printf("palindrome number ");

else

printf("not palindrome");

getch();

}

Output:

Run 1:

enter the number=151

palindrome number

run 2:

enter the number=5621

not palindrome number

Page 25: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

25

14 Q:Write a C program to generate all the prime numbers between 1 and ‘n’,

where ‘n’ is a value supplies by the user.

#include<stdio.h>

#include<conio.h>

void main()

{

int n, i, j, fcount=0;

clrscr();

printf("Prime no.series:\n");

printf("Enter the range:\n");

scanf("%d", &n);

printf("The prime numbers between 1 to %d\n",n);

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

{

fcount = 0;

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

{

if(i % j == 0)

{

fcount++;

}

if(fcount == 2)

{

printf("%5d", i);

}

}

}

getch();

}

Output:

Prime no. series:

Enter the range:

10

The prime numbers between 1 to 10

2 3 5 7

Page 26: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

26

15 Q: write a c program to find maximum and minimum element in array

#include <stdio.h>

#include <conio.h>

void main()

{

int arr[50];

int i, max, min, size,sum=0;

float avg=0;

clrscr();

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

scanf("%d", &size);

printf("Enter elements in the array: ");

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

{

scanf("%d", &arr[i]);

sum=sum+arr[i];

}

max = arr[0]; OUTPUT:

min = arr[0];

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

{

if(arr[i] > max)

{

max = arr[i];

}

if(arr[i] < min)

{

min = arr[i];

}

}

avg=sum/size;

printf("Maximum element = %d\n", max);

printf("Minimum element = %d\n", min);

printf("Average of inserted array elements=%f\n",avg);

getch();

}

Page 27: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

27

16 Q: Write a C program to perform the addition of two matrices.

#include <stdio.h>

#include <conio.h>

void main()

{

int i,j,r1,c1,r2,c2, first[10][10], second[10][10], sum[10][10];

clrscr();

printf("Enter the order of matrices for 2 matrices:\n");

scanf("%d%d%d%d", &r1,&c1,&r2,&c2);

if((r1==r2)&&(c1==c2))

{

printf("Enter the %d X %d elements of first matrix:\n",r1,c1);

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

{

for (j = 0; j < c1; j++)

{

scanf("%d", &first[i][j]);

}

}

printf("Enter the %d X %d elements of second matrix:\n",r2,c2);

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

{

for (j = 0 ; j < c2; j++)

{

scanf("%d", &second[i][j]);

}

}

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

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

{

for (j = 0 ; j < c1; j++)

{

sum[i][j] = first[i][j] + second[i][j];

printf("%d\t", sum[i][j]);

}

printf("\n");

}

}

Page 28: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

28

else

{

printf("Summation of two matrices not possible!\n");

}

getch();

}

OUTPUT:

RUN 1:

RUN 2:

Page 29: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

29

17Q: Write a C Program for Matrix Multiplication:

#include <stdio.h>

#include<conio.h>

void main()

{

int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;

clrscr();

printf("Enter rows and column for first matrix: ");

scanf("%d %d", &r1, &c1);

printf("Enter rows and column for second matrix: ");

scanf("%d %d",&r2, &c2);

// Column of first matrix should be equal to column of second matrix and

while (c1 != r2)

{

printf("Error! column of first matrix not equal to row of second.\n\n");

printf("Enter rows and column for first matrix: ");

scanf("%d %d", &r1, &c1);

printf("Enter rows and column for second matrix: ");

scanf("%d %d",&r2, &c2);

}

// Storing elements of first matrix.

printf("\nEnter elements of matrix 1:\n");

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

{

for(j=0; j<c1; ++j)

{

printf("Enter elements a%d%d: ",i+1, j+1);

scanf("%d", &a[i][j]);

}

}

Page 30: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

30

// Storing elements of second matrix.

printf("\nEnter elements of matrix 2:\n");

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

{

for(j=0; j<c2; ++j)

{

printf("Enter elements b%d%d: ",i+1, j+1);

scanf("%d",&b[i][j]);}

}

// logic for multiplication

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

{

for(j=0; j<c2; ++j)

{

result[i][j] = 0;

// Multiplying matrices a and b and

// storing result in result matrix

for(k=0; k<c1; ++k) OUTPUT:

{

result[i][j]+=a[i][k]*b[k][j];

}

}

}

// Displaying the result

printf("\nOutput Matrix:\n"); Output:

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

{

for(j=0; j<c2; ++j)

{

printf("%d ", result[i][j]);

if(j == c2-1)

printf("\n\n");

}

}

getch(); }

Page 31: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

31

18 Q: A program for finding the factorial value to given number using recursion.

#include<stdio.h>

#include<conio.h>

long factorial(int n) ;

void main()

{

int number;

long int fact;

clrscr();

printf("Enter a number: ");

scanf("%d", &number);

fact = factorial(number);

printf("Factorial of %d is %ld\n", number, fact);

getch();

}

long factorial(int n)

{

if (n == 0)

{

return 1;

}

else

{

return(n * factorial(n-1));

}

}

Output:

Enter a number: 6

Factorial of 5 is: 720

Page 32: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

32

19 Q: Write a program to generate Fibonacci series up to given range.

#include<stdio.h>

int Fibonacci(int n);

void main()

{

int n, i;

clrscr();

printf("Enter the range for fibonacci series:\n");

scanf("%d",&n);

printf("Fibonacci series\n");

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

{

printf("%d\n", Fibonacci(i));

}

getch();

}

int Fibonacci(int n)

{

if ( n == 0 )

{

return 0;

}

else if ( n == 1 )

{

return 1;

}

else

{

return ( Fibonacci(n-1) + Fibonacci(n-2) );

}

}

Output:

Page 33: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

33

20 Q: Write a C program to compute the value of X ^ N given X and N as

inputs

#include <stdio.h>

#include <math.h>

long int power(int x, int n);

void main()

{

long int x, n, xpown;

printf("Enter the values of X and N \n");

scanf("%ld %ld", &x, &n);

xpown = power(x, n);

printf("X to the power N = %ld\n", xpown);

}

/* Recursive function to computer the X to power N */

long int power(int x, int n)

{

if (n == 1)

{

return(x);

}

else if (n % 2 == 0)

/* if n is even */

{

return (pow(power(x, n/2), 2));

}

else

/* if n is odd */

{

return (x * power(x, n - 1));

}

}

Output:

Page 34: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

34

21 Q: Write a C program to find the GCD (greatest common divisor) of two given

integers using recursion technique.

#include <stdio.h>

#include <Conio.h>

int gcd(int n1, int n2);

void main()

{

int x1, x2;

clrscr();

printf("Enter any two positive numbers to find GCD: ");

scanf("%d%d", &x1, &x2);

printf("GCD of %d and %d = %d", x1, x2, gcd(x1,x2));

getch();

}

/* Recursive approach of euclidean algorithm to find GCD of two numbers */

int gcd(int n1, int n2)

{

if(n2!= 0)

{

return (gcd(n2, n1%n2));

}

else

{

return (n1);

}

}

Output:

Page 35: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

35

22 Q: A C program to display the following format:

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

#include<Stdio.h>

#include<conio.h>

void main()

{

int i,j,k,n;

clrscr();

printf("Enter the range:\n");

scanf("%d",&n);

printf("number format is:\n");

k=1;

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

{

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

{

printf("%5d",k);

k++;

}

printf("\n");

}

getch();

}

Output:

Enter the range:

5

number format is:

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

Page 36: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

36

23 Q: Write a program for reading elements using pointer into array and display

the values using array.

#include<stdio.h>

#include<conio.h>

void main()

{

int a[50],*p,i,n;

clrscr();

p=a;

printf("Enter size of array:\n");

scanf("%d",&n);

printf("Enter %d elements into array:\n",n);

/* storing the elements into array through pointer variable*/

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

{

scanf("%d",p+i);

}

printf("Array elelments are:\n");

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

{

printf("%5d",a[i]);

}

getch();

}

OUTPUT:

Page 37: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

37

24 Q: Write a program for display values reverse order from array using pointer.

#include<stdio.h>

#include<conio.h>

#define MAX 30

void main()

{

int size, i, arr[MAX];

int *ptr;

clrscr();

ptr = &arr[0];

printf("\nEnter the size of array : ");

scanf("%d", &size);

printf("\nEnter %d integers into array: ", size);

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

{

scanf("%d", ptr);

ptr++;

}

ptr = &arr[size - 1];

printf("\nElements of array in reverse order are :");

for (i = size - 1; i >= 0; i--)

{

printf("\nElement %d is %d ", i, *ptr);

ptr--;

}

getch();

}

Output:

Page 38: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

38

25 Q: Write a program through pointer variable to sum of n elements from array

#include<stdio.h>

#include<conio.h>

#define MAX 50

void main()

{

int size,sum=0,i,arr[MAX];

int *ptr;

clrscr();

ptr = &arr[0];

printf("\nEnter the size of array : ");

scanf("%d", &size);

printf("\nEnter %d integers into array: ", size);

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

{

scanf("%d", ptr);

ptr++;

}

ptr = &arr[0];

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

{

sum=sum+*(ptr);

ptr++;

}

printf("\nusing pointers,sum of array elements is %d",sum);

getch();

}

Output:

Page 39: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

39

26 Q: Write a C program to create a file and store some data into it and display the

content of that file to standard output device.

#include<conio.h>

#include<stdio.h>

void main()

{

FILE *fp1;

char c;

clrscr();

fp1=fopen("example1.txt","w");

printf("Enter the data to a file:\n");

while((c=getchar())!=EOF)

{

putc(c,fp1);

}

fclose(fp1);

printf("file created sucessfully!\n");

printf("Data from a file:\n");

fp1=fopen("example1.txt","r");

while((c=getc(fp1))!=EOF)

{

printf("%c",c);

}

fclose(fp1);

getch();

}

Output:

Enter the data to a file:

VISHNU PRASAD

^Z

file created sucessfully!

Data from a file:

VISHNU PRASAD

Page 40: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

40

27 Q: A C program which copies one file to another, replacing all lowercase

characters with their upper case equalent.

#include<conio.h>

#include<stdio.h>

void main()

{

FILE *fp1,*fp2;

char c;

clrscr();

fp1=fopen("example1.txt","w");

printf("Enter the data to a file:\n");

while((c=getchar())!=EOF)

{

putc(c,fp1);

}

fclose(fp1);

printf("file created sucessfully!\n");

/* logic for converting lowercase letters into uppercase*/

fp1=fopen("example1.txt","r");

fp2=fopen("example2.txt","w");

while((c=getc(fp1))!=EOF)

{

if((c>='a')&&(c<='z'))

{

putc(c-32,fp2);

}

else

{

putc(c,fp2);

}

}

fclose(fp1);

fclose(fp2);

fp2=fopen("example2.txt","r");

printf("\nAfter convertion of all lowercase letters into upper case while \n") ;

printf("coping into another file:\n");

printf("Data from a file:\n");

Page 41: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

41

while((c=getc(fp2))!=EOF)

{

printf("%c",c);

}

fclose(fp2);

getch();

}

Output:

Enter the data to a file:

vISHNU PRAsad

^Z

file created sucessfully!

After convertion of all lowercase letters into upper case while

coping into another file:

Data from a file:

VISHNU PRASAD

Page 42: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

42

28 Q: A C program to merge two files into a third file.

#include<conio.h>

#include<stdio.h>

void main()

{

FILE *fp1,*fp2,*fp3;

char c,x;

clrscr();

fp1=fopen("example1.txt","w");

printf("Enter the data to file 1:\n");

while((c=getchar())!=EOF)

{

putc(c,fp1);

}

fclose(fp1);

printf("file 1 created sucessfully!\n");

fp2=fopen("example2.txt","w");

printf("Enter the data to file 2:\n");

while((c=getchar())!=EOF)

{

putc(c,fp2);

}

fclose(fp2);

printf("file 2 created successfully!\n");

fp2=fopen("example2.txt","r") ;

fp1=fopen("example1.txt","r");

fp3=fopen("example3.txt","w");

while((c=getc(fp1))!=EOF)

{

putc(c,fp3);

}

fclose(fp1);

while((c=getc(fp2))!=EOF)

{

putc(c,fp3);

}

Page 43: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

43

fclose(fp2);

fclose(fp3);

printf("merging of 2 files into 3rd file completed!\n");

getch();

}

Output:

Enter the data to file 1:

Vishnu Prasad

^Z

file 1 created sucessfully!

Enter the data to file 2:

Goranthala

^Z

file 2 created successfully!

merging of files completed!

Page 44: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

44

29 Q: Write a C program to create a binary file and store 10 numbers into it and

display the content of that file to standard output device.

#include<conio.h>

#include<stdio.h>

void main()

{

FILE *fp1;

int i,n,num;

clrscr();

fp1=fopen("example1.txt","wb");

printf("Enter the range:\n");

scanf("%d",&n);

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

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

{

scanf("%d",&num);

putw(num,fp1);

}

fclose(fp1);

printf("file 1 created sucessfully!\n");

fp1=fopen("example1.txt","rb");

printf("Data from the file :\n");

while((num=getw(fp1))!=EOF)

{

printf("%5d",num);

}

fclose(fp1);

getch();

}

Output:

Page 45: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

45

30 Q: Write a program for implement linear or sequential search technique.

#include<stdio.h>

#include<conio.h>

void main()

{

int i,flag=0,n,sk,a[50];

clrscr();

printf("enter the range:\n");

scanf("%d",&n);

printf("enter %d elements:\n",n);

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

{

scanf("%d",&a[i]);

}

printf("enter a searchkey:\n");

scanf("%d",&sk);

/*sequential search*/

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

{

if(sk==a[i])

{

flag=1;

printf("%d element found at position

%d!\n",sk,i+1);

break;

}

}

if(flag==0)

{

printf("%d element not found in the

list !\n",sk);

}

getch();

}

Output:

RUN 1:

RUN 2:

Page 46: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

46

31Q: Write a program to implement binary search technique.

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,flag=0,n,sk,a[50],temp,mid,first,last;

clrscr();

printf("enter the range :\n");

scanf("%d",&n);

printf("enter %d elements :\n",n);

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

{

scanf("%d",&a[i]);

}

/*logic for sorting using bubble sort technique*/

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

{

for(j=n-1;j>i;j--)

{

if(a[j]<a[j-1])

{

temp=a[j];

a[j]=a[j-1];

a[j-1]=temp;

}

}

}

printf("After sorting:\n");

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

{

printf("%5d",a[i]);

}

printf("\nEnter a search key:\n");

scanf("%d",&sk);

first=0;

last=n-1;

while(first<=last)

{

mid=(first+last)/2;

if(a[mid]>sk)

Page 47: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

47

{

last=mid-1;

}

else if(a[mid]<sk)

{

first=mid+1;

}

else

{

printf("%d found at location %d\n",sk,mid+1);

flag=1;

break;

}

}

if (flag==0)

{

printf("%d element not found in the list!\n",sk);

}

getch();

}

Output:

Run1: Run 2:

Page 48: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

48

32 Q: Write a C program that implements the Bubble sort method to sort a given

list of integers in ascending order.

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,n,a[50],temp,smallest;

clrscr();

printf("enter the range :\n");

scanf("%d",&n);

printf("enter %d elements: \n",n) ;

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

{

scanf("%d",&a[i]);

}

printf("Before sorting:\n");

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

{

printf("%5d",a[i]);

}

/*logic for sorting using bubble sort technique*/

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

{

for(j=n-1;j>i;j--)

{

if(a[j]<a[j-1])

{

temp=a[j];

a[j]=a[j-1];

a[j-1]=temp;

}

}

}

printf("\nAfter sorting using Bubble sort technique:\n");

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

{

printf("%5d",a[i]);

}

getch();

}

Page 49: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

49

OUTPUT:

Page 50: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

50

33Q: Write a program for implement selection sort technique.

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,n,a[50],temp,smallest;

clrscr();

printf("enter the range :\n");

scanf("%d",&n);

printf("enter %d elements: \n",n) ;

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

{

scanf("%d",&a[i]);

}

printf("Before sorting:\n");

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

{

printf("%5d",a[i]);

}

/*logic for selection sort*/

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

{

smallest=i;

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

{

if(a[smallest]>a[j])

{

smallest=j;

}

}

temp=a[i];

a[i]=a[smallest];

a[smallest]=temp;

}

Page 51: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

51

printf("\nAfter sorting using selection sort technique:\n");

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

{

printf("%5d",a[i]);

}

getch();

}

Output:

Page 52: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

52

34Q: Write a program implement insertion sort technique.

#include<stdio.h>

#include<conio.h>

void insertionsort(int list[],int last);

void main()

{

int array[50],i,n;

clrscr();

printf("Enter the range:\n");

scanf("%d",&n);

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

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

{

scanf("%d",&array[i]);

}

printf("Inserted array elements before sorting are:\n");

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

{

printf("%5d",array[i]);

}

insertionsort(array,n-1);

printf("\nafter insertion sorting array elements are:\n");

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

{

printf("%5d",array[i]);

}

getch();

}

Page 53: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

53

void insertionsort(int list[],int last)

{

int i;

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

{

int located=0;

int j;

int temp=list[i];

for(j=i-1;j>=0&&!located;)

{

if(temp<list[j])

{

list[j+1]=list[j];

j--;

}

else

{

located=1;

}

}

list[j+1]=temp;

}

}

Output:

Page 54: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

54

35 Q: C program to display names of students in ascending order using

arrays.

#include<conio.h>

#include<stdio.h>

void main()

{

char string[10][15],temp[15];

int i=0,j=0,n;

clrscr();

printf("Enter how many names do you want:\n");

scanf("%d",&n);

printf("Enter %d names:\n",n);

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

{

scanf("%s",string[i]);

}

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

{

for(j=1;j<=n-1;j++)

{

if(strcmp(string[j-1],string[j])>0)

{

strcpy(temp,string[j]);

strcpy(string[j],string[j-1]);

strcpy(string[j-1],temp);

}

}

}

printf("Sorted names:\n");

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

{ OUTPUT:

printf("%s\n",string[i]);

getch();

}

Page 55: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

55

36 Q: Write a C program to determine if the given string is a palindrome or not

(Spelled same in both directions with or without a meaning like madam, civic,

noon, abcba, etc.)

#include <stdio.h>

#include <string.h>

void main()

{

char str1[20], org[20];

clrscr();

printf("Enter a string to check if it is a palindrome\n");

scanf("%s",&str1);

strcpy(org,str1); // Copying input string

strrev(str1); // Reversing the string

if (strcmp(org,str1) == 0) // Comparing input string with the reverse string

{

printf("The string is a palindrome.\n");

}

else

{

printf("The string isn't a palindrome.\n");

}

getch();

}

Output:

Run 1:

Run 2:

Page 56: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

56

37 Q: Write a C program to count the lines, words and characters in a given text.

#include<stdio.h>

#include<conio.h>

void main()

{

char line[50], ctr;

int i, c, end = 0, characters = 0, words = 0,lines = 0;

clrscr();

printf("KEY IN THE TEXT.\n");

printf("GIVE ONE SPACE AFTER EACH WORD.\n");

printf("WHEN COMPLETED, PRESS 'RETURN'.\n\n");

while( end == 0)

{

/* Reading a line of text */

c = 0;

while((ctr=getchar()) != '\n')

line[c++] = ctr;

line[c] = '\0';

/* counting the words in a line */

if(line[0] == '\0')

break ;

else Output:

{

words++;

for(i=0; line[i] != '\0';i++)

if(line[i] == ' ' || line[i] == '\t')

words++;

}

/* counting lines and characters */

lines = lines +1;

characters = characters + strlen(line);

}

printf ("\n");

printf("Number of lines = %d\n", lines);

printf("Number of words = %d\n", words);

printf("Number of characters = %d\n", characters);

getch();

}

Page 57: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

57

38 Q: Write a C program to illustrate the following:

4. Simple structure concept.

5. Enumeration concept.

6. Typedef concept.

1. Simple structure concept.

#include<stdio.h>

#include <string.h>

struct student

{ int rno;

char name[50];

}; //declaring e1 variable for structure

void main( )

{

struct student s;

clrscr();

printf("Enter student rno & name:\n");

scanf("%d%s",&s.rno,s.name);

printf( "employee details:\n");

printf( "Rno : %d Name : %s \n", s.rno,s.name);

getch();

}

Output:

Page 58: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

58

2. Enumeration concept.

#include <stdio.h>

enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday };

void main()

{

enum week today;

int i;

clrscr();

printf("printing enumerators value:\n");

for(i=sunday;i<=saturday;i++)

{

printf("5%d",i);

}

getch();

}

Output:

Page 59: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

59

3. Typedef concept.

#include<stdio.h>

#include<string.h>

struct employee

{

char name[50];

int salary;

};

void main( )

{

typedef struct employee emp;

emp e1;

clrscr();

printf("\nEnter Employee record:\n");

printf("\nEmployee name:\t");

scanf("%s", e1.name);

printf("\nEnter Employee salary: \t");

scanf("%d", &e1.salary);

printf("\nEmployee name is %s", e1.name);

printf("\nSalary is %d", e1.salary);

getch();

}

Output:

Page 60: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

60

39 Q: Write a C programs to construct a pyramid of numbers as follows:

A.

#include <stdio.h>

void main()

{

int i, j, rows;

clrscr();

printf("Enter number of rows: ");

scanf("%d",&rows);

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

{

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

{

printf("%d ",j);

}

printf("\n");

}

getch();

}

Output:

Page 61: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

61

B.

#include <stdio.h>

void main()

{

int i, j, rows;

clrscr();

printf("Enter number of rows: ");

scanf("%d",&rows);

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

{

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

{

printf(" * ");

}

printf("\n");

}

for(i=rows-1; i>0;i--)

{

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

{

printf(" * ");

}

printf("\n");

}

getch();

}

Output:

Page 62: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

62

C.

#include <stdio.h>

void main()

{

int i, j, rows;

clrscr();

printf("Enter number of rows: ");

scanf("%d",&rows);

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

{

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

{

printf("%d ",i);

}

printf("\n");

}

getch();

}

Output:

Page 63: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

63

40 Q. Write a C program to illustrate Macro’s concept (#define, #undef)

Using #define:

#include <stdio.h>

#define MIN(a,b) ((a)<(b)?(a):(b))

#define MAX(a,b) ((a)>(b)?(a):(b))

void main()

{

int x,y;

clrscr();

printf("Enter 2 numbers:\n");

scanf("%d%d",&x,&y);

printf("Minimum number of %d and %d is: %d\n", x,y,MIN(x,y));

printf("Maximum number of %d and %d is: %d\n", x,y,MAX(x,y));

getch();

}

Output:

Page 64: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

64

Using #undef:

#include <stdio.h>

#define MIN(a,b) ((a)<(b)?(a):(b))

#define MAX(a,b) ((a)>(b)?(a):(b))

#undef MAX

void main()

{

int x,y;

clrscr();

printf("Enter 2 numbers:\n");

scanf("%d%d",&x,&y);

printf("Minimum number of %d and %d is: %d\n", x,y,MIN(x,y));

printf("Maximum number of %d and %d is: %d\n", x,y,MAX(x,y));

getch();

}

Output:

Error will occur. i.e.

Page 65: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

65

Beyond the Syllabus

01

Write a C program that uses to perform the following:

a) Create a singly linked list of integers.

b) Display the contents of the above list.

02

Write a C Program to Implement a Stack using Linked List

Page 66: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

66

1. Q. Write a C program that uses to perform the following:

a) Create a singly linked list of integers.

b) Display the contents of the above list.

#include<stdio.h> #include<conio.h> #include<malloc.h> struct node { int number; struct node *p; }*h,*f,*t; void main() { char c; clrscr(); f=NULL; do { h=(struct node *)malloc(sizeof(struct node)); printf("enter a number:"); scanf("%d",&h->number); if(f!=NULL) { t->p=h; t=h; } else f=t=h; fflush(stdin); printf("\ncontinue(y/n):\n"); scanf("%c",&c); } while(c=='y'); t->p=NULL; t=f;

Page 67: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

67

printf("\nthe linked list elements are:\n"); while(t!=NULL) { printf("%5d",t->number); t=t->p; } getch(); } Output:

Page 68: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

68

/* C Program to Implement a Stack using Linked List */

#include <stdio.h>

#include <stdlib.h>

struct node

{

int info;

struct node *ptr;

}*top,*top1,*temp;

int topelement();

void push(int data);

void pop();

void empty();

void display();

void destroy();

void create();

void main()

{

int no, ch, e;

printf("\n 1 - Push");

printf("\n 2 - Pop");

printf("\n 3 - Top");

printf("\n 4 - Empty");

printf("\n 5 - Exit");

printf("\n 6 - Dipslay");

create();

while (1)

{

printf("\n Enter choice : ");

scanf("%d", &ch);

switch (ch)

{

case 1:

printf("Enter data : ");

scanf("%d", &no);

push(no);

Page 69: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

69

break;

case 2:

pop();

break;

case 3:

if (top == NULL)

printf("No elements in stack");

else

{

e = topelement();

printf("\n Top element : %d", e);

}

break;

case 4:

empty();

break;

case 5:

exit(0);

case 6:

display();

break;

default :

printf(" Wrong choice, Please enter correct choice ");

break;

}

}

}

/* Create empty stack */

void create()

{

top = NULL;

}

/* Push data into stack */

void push(int data)

{

if (top == NULL)

Page 70: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

70

{

top =(struct node *)malloc(1*sizeof(struct node));

top->ptr = NULL;

top->info = data;

}

else

{

temp =(struct node *)malloc(1*sizeof(struct node));

temp->ptr = top;

temp->info = data;

top = temp;

}

}

/* Display stack elements */

void display()

{

top1 = top;

if (top1 == NULL)

{

printf("Stack is empty");

return;

}

while (top1 != NULL)

{

printf("%d ", top1->info);

top1 = top1->ptr;

}

}

/* Pop Operation on stack */

void pop()

{

top1 = top;

if (top1 == NULL)

{

Page 71: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

71

printf("\n Error : Trying to pop from empty stack");

return;

}

else

top1 = top1->ptr;

printf("\n Popped value : %d", top->info);

free(top);

top = top1;

}

/* Return top element */

int topelement()

{

return(top->info);

}

/* Check if stack is empty or not */

void empty()

{

if (top == NULL)

printf("\n Stack is empty");

}

Page 72: DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING · 2019-04-22 · Write a C program to find the minimum, maximum and average in an array of integers. 26 16 Write a C program to perform

72

Output: