0% found this document useful (0 votes)
42 views4 pages

Include

This C program takes in two integers from the user, calculates the sum and average of those integers, and outputs the results. It uses cout and cin commands to input and output values, and performs basic arithmetic operations like addition and division to calculate the sum and average.

Uploaded by

Shruthi Punya
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)
42 views4 pages

Include

This C program takes in two integers from the user, calculates the sum and average of those integers, and outputs the results. It uses cout and cin commands to input and output values, and performs basic arithmetic operations like addition and division to calculate the sum and average.

Uploaded by

Shruthi Punya
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/ 4

#include <iostream.

h>
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int x,y,sum;
float average;
cout << "Enter 2 integers : " << endl;
cin>>x>>y;
sum=x+y;
average=sum/2;
cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl;
cout << "The average of " << x << " and " << y << " is " << average << "."
<< endl;
getch();
}

This program takes in two integers x and y as a screen input from the user.
The sum and average of these two integers are calculated and outputted
using the 'cout' command.
1.

/*

2.

* C program to sort N numbers in ascending order using Bubble sort

3.

* and print both the given and the sorted array

4.

*/

5.

#include <stdio.h>

6.

#define MAXSIZE 10

7.
8.

void main()

9.

10.

int array[MAXSIZE];

11.

int i, j, num, temp;

12.
13.

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

14.

scanf("%d", &num);

15.

printf("Enter the elements one by one \n");

16.

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

17.

18.

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

19.

20.

printf("Input array is \n");

21.

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

22.

23.

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

24.

25.

/* Bubble sorting begins */

26.

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

27.

28.

for (j = 0; j < (num - i - 1); j++)

29.

30.

if (array[j] > array[j + 1])

31.

32.

temp = array[j];

33.

array[j] = array[j + 1];

34.

array[j + 1] = temp;

35.

36.

37.

38.

printf("Sorted array is...\n");

39.

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

40.

41.

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

42.

43.

}
44.

/*

45.

* C program to read N integers and store them in an array A.

46.

* Find the sum of all these elements using pointer.

47.

*/

48.

#include <stdio.h>

49.

#include <malloc.h>

50.
51.

void main()

52.

53.

int i, n, sum = 0;

54.

int *a;

55.
56.

printf("Enter the size of array A \n");

57.

scanf("%d", &n);

58.

a = (int *) malloc(n * sizeof(int));

59.

printf("Enter Elements of First List \n");

60.

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

61.

62.

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

63.

64.

</* Compute the sum of all elements in the given array */

65.

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

66.

67.

sum = sum + *(a + i);

68.

69.
70.

printf("Sum of all elements in array = %d\n", sum);


}

71.
* C Program to find the nth number in Fibonacci series using recursion
72.

*/

73.

#include <stdio.h>

74.

int fibo(int);

75.
76.

int main()

77.

78.

int num;

79.

int result;

80.
81.

printf("Enter the nth number in fibonacci series: ");

82.

scanf("%d", &num);

83.

if (num < 0)

84.

85.

printf("Fibonacci of negative number is not possible.\n");

86.

87.

else

88.

89.

result = fibo(num);

90.

printf("The %d number in fibonacci series is %d\n", num, result);

91.

92.

return 0;

93.

94.

int fibo(int num)

95.

96.

if (num == 0)

97.

98.

return 0;

99.

100.

else if (num == 1)

101.

102.

return 1;

103.

104.

else

105.

106.
107.
108. }
109. *

return(fibo(num - 1) + fibo(num - 2));


}

110. * C program to implement stack. Stack is a LIFO data structure.


111. * Stack operations: PUSH(insert operation), POP(Delete operation)
112. * and Display stack.
113. */
114. #include <stdio.h>
115. #define MAXSIZE 5
116.
117. struct stack
118. {
119.

int stk[MAXSIZE];

120.

int top;

121. };
122. typedef struct stack STACK;
123. STACK s;
124.
125. void push(void);
126. int pop(void);
127. void display(void);
128.
129. void main ()
130. {
131.

int choice;

132.

int option = 1;

133.

s.top = -1;

134.
135.

printf ("STACK OPERATION\n");

136.

while (option)

137.

138.

printf ("------------------------------------------\n");

139.

printf ("

-->

PUSH

140.

printf ("

-->

POP

141.

printf ("

-->

DISPLAY

142.

printf ("

-->

EXIT

143.

printf ("------------------------------------------\n");

\n");
\n");
\n");
\n");

144.
145.

printf ("Enter your choice\n");

146.

scanf

147.

switch (choice)

148.

149.

case 1:

150.

("%d", &choice);

push();

151.

break;

152.

case 2:

153.

pop();

154.

break;

155.

case 3:

156.

display();

157.

break;

158.

case 4:

159.

return;

160.

161.

fflush (stdin);

162.

printf ("Do you want to continue(Type 0 or 1)?\n");

163.
164.

scanf

("%d", &option);

165. }
166. /* Function to add an element to the stack */
167. void push ()
168. {
169.

int num;

170.

if (s.top == (MAXSIZE - 1))

171.

172.

printf ("Stack is Full\n");

173.

return;

174.

175.

else

176.

177.

printf ("Enter the element to be pushed\n");

178.

scanf ("%d", &num);

179.

s.top = s.top + 1;

180.

s.stk[s.top] = num;

181.

182.

return;

183. }
184. /* Function to delete an element from the stack */
185. int pop ()
186. {
187.

int num;

188.

if (s.top == - 1)

189.

190.

printf ("Stack is Empty\n");

191.

return (s.top);

192.

193.

else

194.

195.

num = s.stk[s.top];

196.

printf ("poped element is = %dn", s.stk[s.top]);

197.

s.top = s.top - 1;

198.

199.

return(num);

200. }
201. /* Function to display the status of the stack */
202. void display ()
203. {
204.

int i;

205.

if (s.top == -1)

206.

207.

printf ("Stack is empty\n");

208.

return;

209.

210.

else

211.

212.

printf ("\n The status of the stack is \n");

213.

for (i = s.top; i >= 0; i--)

214.

215.

printf ("%d\n", s.stk[i]);

216.

217.

218.

printf ("\n");

219. }

You might also like