Computer Science, asked by ayush672790, 1 year ago

IV. Write programs
1. To accept 10 numbers and print how many are odd and how many are
even. Also print the sum of all the odd and even numbers separately.
2. Write a program to sort the array in ascending order.
3. Finding the sum of all the numbers.
4. Sum odd and even numbers.
5. Ocurrance of a given number.
6. Swap the elements in the array.
7. Reversing the elements in the array.
V. Rewrite the program with arrays
1. void main()
{ int ml, m2, sum=0;
scanf("%d",&m1);
scanf("%d", &m2);
sum = ml+m2;
printf("%d sum=", sum); }

Answers

Answered by charlie1505
2

Answer:

2) Sorting

#include <stdio.h>

void main()

{

int i, j, a, n, number[30];

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

scanf("%d", &n);

printf("Enter the numbers \n");

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

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

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

{

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

{

if (number[i] > number[j])

{

a = number[i];

number[i] = number[j];

number[j] = a;

}

}

}

printf("The numbers arranged in ascending order are given below \n");

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

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

}

3) Sum of digits

#include<stdio.h>

int main()

{

int n,sum=0,m;

printf("Enter a number:");

scanf("%d",&n);

while(n>0)

{

m=n%10;

sum=sum+m;

n=n/10;

}

printf("Sum is=%d",sum);

return 0;

}

4)sum of odd even nos

#include <stdio.h>

void main()

{

int i, num, odd_sum = 0, even_sum = 0;

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

scanf("%d", &num);

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

{

if (i % 2 == 0)

even_sum = even_sum + i;

else

odd_sum = odd_sum + i;

}

printf("Sum of all odd numbers = %d\n", odd_sum);

printf("Sum of all even numbers = %d\n", even_sum);

}

6) swap array element

#include <stdio.h>

#define MAX 100

int main()

{

int arr[MAX],n,i;

int temp;

printf("Enter total number of elements: ");

scanf("%d",&n);

//value of n must be even

if(n%2 !=0)

{

printf("Total number of elements should be EVEN.");

return 1;

}

//read array elements

printf("Enter array elements:\n");

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

{

printf("Enter element %d:",i+1);

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

}

//swap adjacent elements

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

{

temp = arr[i];

arr[i] = arr[i+1];

arr[i+1]= temp;

}

printf("\nArray elements after swapping adjacent elements:\n");

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

{

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

}

return;

}

7)reverse array element

#include <stdio.h>

int main()

{

int n, c, d, a[100], b[100];

printf("Enter the number of elements in array\n");

scanf("%d", &n);

printf("Enter array elements\n");

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

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

/*

* Copying elements into array b starting from end of array a

*/

for (c = n - 1, d = 0; c >= 0; c--, d++)

b[d] = a[c];

/*

* Copying reversed array into the original.

* Here we are modifying original array, this is optional.

*/

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

a[c] = b[c];

printf("Reverse array is\n");

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

printf("%d\n", a[c]);

return 0;

}

Similar questions