Welcome To Uday Satya Blog

46 - 48 PROGRAMS



/*****************************************************
   46.To implement various arithmetic operators using Switch
 **************************************************** */
#include <*stdio.h>
#include <*conio.h>
void main()
{
clrscr();
 int ch;
 float n1,n2,r;
 printf("Enter your choice \n 1. Addition 2. Subtraction 
                           3. Multiplication 4. Division ");
 scanf("%d",&ch);
 printf("Enter the numbers ");
 scanf("%f %f",&n1,&n2);

switch (ch)
{
case 1:
  r=n1+n2;
  printf("The sum of the numbers is : %f",r);
  break;
case 2:
  r=n1-n2;
  printf("The difference of the numbers is : %f",r);
  break;
case 3:
  r=n1*n2;
  printf("The product of the numbers is : %f",r);
  break;
case 4:
  r=n1/n2;
  printf("The quotient is : %f",r);
  break;
default : printf("Wrong input");
}//switch
getch();
}//main

OUTPUT
Enter your choice
 1. Addition 2. Subtraction 3. Multiplication 4. Division 
3
Enter the numbers 4
6
The product of the numbers is : 24.000000


/* *******************************************
         47.To find sum of elements of a 1 D array
   ******************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
clrscr();
int i,sum=0,n;
int arr[100];
printf("Enter the length of the array ");
scanf("%d",&n);
printf("Enter the array elements");

for(i=0;i
{
scanf("%d",&arr[i]);
 sum=sum+arr[i];
 }

printf("\nThe sum of the elements is %d",sum);

getch();
}

OUTPUT
Enter the length of the array 5
Enter the array elements 1 2 3 4 5

The sum of the elements is 15

/* ****************************************
     48.To sort an array is ascending order
   **************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
clrscr();
int i,j,n,temp;
int arr[100];
printf("Enter the length of the array ");
scanf("%d",&n);
printf("Enter the array elements");

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

for(i=0;i
{
for(j=i+1;j
 {
if(arr[i]>arr[j])
  {
temp=arr[i];
   arr[i]=arr[j];
   arr[j]=temp;
  }
 }
}
printf("\nThe sorted array is ");
for(i=0;i
 printf("%d\t",arr[i]);

getch();
}

OUTPUT
Enter the length of the array 5
Enter the array elements 87 54 14 3 67

The sorted array is 3   14      54      67      87

0 comments:

Post a Comment