C-Programming

C Programming Questions Solutions with Arrays

  1. Design a program to read hundred numbers in an array variable and find out whether individual number is odd or even.

#include<stdio.h>

#include<conio.h>

void main()

{

            int i,j,num[100];

//loop for reading the 100 numbers

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

{
printf(“\nPlease enter %dth number:\t”,i);

scanf(“%d”,&num[i]);

            }

//loop for determine whether the number is odd or even

printf(“\n\n\n”);

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

            {

if(num[i]%2==0)

printf(“\n%d is even number.”);

else

printf(“\n%d is odd number.”);

            }

getch();

}

  1. Write a program that reads two m*n matrices and displays the sum of these matrices.

#include<stdio.h>

#inlcude<conio.h>

void main()

{

int a[10][10],b[10][10],c[10][10];

int i,j,m,n;

clrscr();

printf(“\n please enter  the size of the matrices:\t”);

scanf(“%d%d”,&m,&n);

printf(“\n\nEnter the elements of first matrix:\n”);

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

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

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

printf(“\n\nEnter the elements of second matrix:\n”);

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

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

scanf(“%d”,&b[i][j]);

//to add the elements of the matrices

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

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

c[i][j])=a[i][j]+b[i][j];

//to prints the first matrix:

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

{

printf(“\n”);

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

printf(“%d\t”,&a[i][j]);

}

//to prints the second matrix:

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

{

printf(“\n”);

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

printf(“%d\t”,&b[i][j]);

}

//to prints the sum of the given matrices:

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

{

printf(“\n”);

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

printf(“%d\t”,&c[i][j]);

}

getch();

}

 

  1. Write a program that reads n numbers and find out the greatest number among them.

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,great;

int num[100];

clrscr();

printf(“\n How many numbers are there?\t”);

scanf(“%d”,&n);

//loop to read n numbers

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

            {

printf(“\nplease enter the %dth number:\t”,i+1);

scanf(“%d”,&num[i]);

}

great=num; //which means let’s assume num[0] is the greatest number.

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

            {

            if(num[i]>great)

great=num[i];

            }

printf(“\n\n\nthe entered numbers are:\n”);

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

printf(“%d\t”,num[i]);

printf(“\n\nThe greatest number among these numbers is %d.”,great);

getch();

}

  1. Write a program that reads n numbers and find out the smallest number among them.

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,small;

int num[100];

clrscr();

printf(“\n How many numbers are there?\t”);

scanf(“%d”,&n);

//loop to read n numbers

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

            {

printf(“\nplease enter the %dth number:\t”,i+1);

scanf(“%d”,&num[i]);

}

smallt=num; //which means let’s assume num[0] is the smallest number.

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

            {

            if(num[i]<great)

small=num[i];

            }

printf(“\n\n\nthe entered numbers are:\n”);

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

printf(“%d\t”,num[i]);

printf(“\n\nThe smallest number among these numbers is %d.”,small);

getch();

}

  1. Write a program to read the elements for a 1-D array of any order and sort in ascending order.

#include<stdio.h>

#inlcud<conio.h>

void main()

{

int i,j,n,a[150],temp;

clrscr();

printf(“\n Please enter the order of the array:\t”);

scanf(“%d”,n);

//loop to read the array elements

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

{

printf(“\netner the %dth number:\t”,i);

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

}

//loops for sorting

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

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

{

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

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

printf(“\n\n The sorted numbers in ascending order are:\n”);

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

printf(“%d\t”,a[i]);

getch();

}

  1. Write a program to read the elements for a 1-D array of any order and sort in descending order.

#include<stdio.h>

#inlcud<conio.h>

void main()

{

int i,j,n,a[150],temp;

clrscr();

printf(“\n Please enter the order of the array:\t”);

scanf(“%d”,n);

//loop to read the array elements

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

{

printf(“\netner the %dth number:\t”,i);

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

}

//loops for sorting

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

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

{

if(a[i]<a[j])

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

printf(“\n\n The sorted numbers in descending order are:\n”);

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

printf(“%d\t”,a[i]);

getch();

}

  1. Write a program to read the elements for a 1-D array of any order and sort in descending order as well as descending order.

 

#include<stdio.h>

#inlcud<conio.h>

void main()

{

int i,j,n,a[150],temp;

clrscr();

printf(“\n Please enter the order of the array:\t”);

scanf(“%d”,n);

//loop to read the array elements

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

{

printf(“\netner the %dth number:\t”,i);

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

}

//loops for sorting

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

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

{

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

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

printf(“\n\n The sorted numbers in ascending order are:\n”);

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

printf(“%d\t”,a[i]);

printf(“\n\n\nThe sorted list in descending order is:\n”);

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

printf(“%d\t”,a[i]);

getch();

}

Arrays (Prev Lesson)
(Next Lesson) Functions
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!