Array C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

C- Programming

1. Write a C program to print multiplication table from 1 to 10.


#include <stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
printf("Multiplication table from 1 to 10\n");
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
printf("%dx%d = %d",j,i,i*j);

}
printf("\n");
}
getch();
}

2. Write a C program that reads two numbers and swap their content.
#include <stdio.h>
#include<conio.h>
void main()
{
int x, y, t;
clrscr();
printf("Enter first number:\n");
scanf("%d", &x);
printf("Enter second number:\n");
scanf("%d%”&y);
printf("Before Swapping\nFirst no. = %d\nSecond no.%d\n", x, y);
t = x;
x = y;
y = t;
printf("After Swapping\nFirst no. = %d\nSecond no. = %d\n", x, y);
getch();
}

3. Write a C program to input two numbers and swap without using third
variable.
#include <stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
printf("Enter first number:\n");
scanf("%d", &a);
printf("Enter second number:\n");
scanf("%d”,&b);
a=a+b;

b=a-b;

a=a-b;

printf(“Swapping without using third variable:\n”);

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

getch();

}
4. Write a C program to read an array of n integers and find the largest

and smallest in that array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the numbers:");
scanf("%d",&a[i]);
}
large=small=a[0]; // means large=a[0]; and small=a[0];
for(i=1;i<n;i++)

if(a[i]>large)

large=a[i];

if(a[i]<small)

small=a[i];

printf("The largest element is %d",large);


printf("\nThe smallest element is %d",small);
getch();
}

5. Define ternary operator. How does ternary operator used instead of


if---else statement? Explain with example.
Ternary operator/Conditional operator:
If any operator is used on three operands or variable is known

as Ternary Operator. It can be represented with ? : . It is also called

as conditional operator.The ternary operator is an operator that takes

three arguments. The first argument is a comparison argument, the

second is the result upon a true comparison, and the third is the result

upon a false comparison. Ternary operator is shortened way of writing

an if-else statement.

Ternary operator is a?b:c it say that the condition a is true b will be

executed else c will be executed.

Advantages of ternary operator:

 It reduces the number of line codes and improve the performance


of the application
For example:
Program to find the largest number among three input numbers using
ternary or conditional operator
# include <stdio.h>
#include<conio.h>
void main()
{
int a, b, c, grt ;
clrscr();
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
grt = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
printf("\nThe largest number is : %d", grt) ;
getch();
}

6. A C-program to read the elements of the two matrices of order 2x3

and 3x2 and perform the matrix multiplication


#include<stdio.h>

#include<conio.h>

void main()

int a[2][3],b[3][2],prod[2][2];

int i,j,k;

clrscr();

printf("enter the elements for matrix A:\n");

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

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

printf("Enter the number[%d][%d]:",i,j);

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

printf("enter the elements for matrix B:\n");

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

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

printf("Enter the number[%d][%d]:",i,j);

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

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

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

prod[i][j]=0;

for(k=0;k<3;k++)

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

printf("\n The product of two matrix is:\n\n");

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

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

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

printf("\n");

getch();

You might also like