Showing posts with label Switch. Show all posts

Write a C program to implement the following: 1. Take an int array of 100 elements. Populate it using random number generator built-in function. 2. User will be given the following choices: a) To insert a new element in the array. b) To delete an element in the array. c) To search an item in the array. d) To update an item in the array. e) To show the elements of the array. f) To quit from the program.

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int main()
{
    int i, a[100], loc, item, size=99;
    char option;
    for(i=0; i<=size; i++)
    {
             a[i]= rand() % 20;
    }
    printf("Press a, b, c, d, e, f\na)  To insert a new element in the array. \nb)  To delete an element in the array.  \nc)  To search an item in the array. \nd)  To update an item in the array. \ne)  To show the elements of the array. \nf)  To quit from the program.");
    option=getche();
    while(option!='f')
    {
    switch(option)
    {
           case 'a':
                if(size==100)
                {
                             printf("you can never add more elements because array in completlty filled.");
                }
                else
                {
                    printf("\nEnter location where you want to enter number: ");
                    scanf("%d", &loc);
                    printf("\nEnter number: ");
                    scanf("%d", &item);
                    for(i=size+1; i>loc; i--)
                                {
                                         a[i]=a[i-1];
                                }
                   a[loc]=item;
                   size++;
                   break;
                }
    case 'b':
         printf("\nEnter location where you want to delete number: ");
    scanf("%d", &loc);
    for(i=loc; i<=size; i++)
    {
             a[i]=a[i+1];
    }
    size--;
    break;
    case 'c':
         printf("\nEnter number which you want to search from array: ");
    scanf("%d", &item);
    for(i=0; i<=size; i++)
    {
             if(item==a[i])
             {
                         loc=i;
                         break;
             }
    }
    if(i>size)
    {
           printf("\nNumber is not found in array.");
    }
    else
    {
        printf("\n%d is found at location %d", item, loc);
    }
    break;
    case 'd':
         printf("\nEnter location where you want to update number: ");
    scanf("%d", &loc);
    printf("Enter number: ");
    scanf("%d", &item);
    a[loc]=item;
    for(i=0; i<=size; i++)
    {
             printf("%d ", a[i]);
    }
    break;
    case 'e':
    printf("\n");
    for(i=0; i<=size; i++)
    {
             printf("%d ", a[i]);
    }
    break;
         default:
         printf("\nYou have entered invalid entry.");
}
printf("\nPress a, b, c, d, e, f\na)  To insert a new element in the array. \nb)  To delete an element in the array.  \nc)  To search an item in the array. \nd)  To update an item in the array. \ne)  To show the elements of the array. \nf)  To quit from the program.");
option=getche();
}
}

Write a Program To Add,Muliplication,Subtraction,Division,Average Through Switch Statement


#include<stdio.h>
#include<conio.h>
int main()
{
 
float a,b,c;
int d;
printf("Enter the value of a and b\n");
printf("value of a is:");
scanf("%f",&a);
printf("value of b is: ");
scanf("%f",&b);
printf("Enter the any one of one number from keyboard 1,2,3,4,5,6\n");
printf("press 1 for addition\n");
printf("press 2 for subtraction\n");
printf("press 3 for muliplication\n");
printf("press 4 for division\n");
printf("press 5 for average\n");
scanf("%d",&d);
switch(d)
{
case 1:
c=a+b;
printf("sum=%f",c);
break;
case 2:
c=a-b;
printf("difference=%f",c);
break;
case 3:
c=a*b;
printf("product=%f",c);
break;
case 4:
c=a/b;
printf("divison=%f",c);
break;
case 5:
c=(a+b)/2;
printf("avg=%f",c);
break;
case 6:
default:
printf("invalid command");
}
getch();
}

Write a program calculate tomorrow's date.


#include <stdio.h>
#include <conio.h>
struct date
{
       int day;
       int month;
       int year;
 
} today, tomorrow;

int main()
{
    char again;
    again:
     struct date;
     printf("Enter today's date: ");
     scanf("%d", &today.day);
     printf("Enter month: ");
     scanf("%d", &today.month);
     printf("Enter year:  ");
     scanf("%d", &today.year);

     switch(today.month)
     {
              case 1:
                   if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else
                   printf("Enter a valid date, month and year");              
              break;
              case 2:
                                      if(today.day==28)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<28 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                else
                   printf("Enter a valid date, month and year");
              break;
              case 3:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else
                   printf("Enter a valid date, month and year");
              break;
              case 4:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                     else
                   printf("Enter a valid date, month and year");
              break;
              case 5:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                               
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 6:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 7:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 8:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 9:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 10:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                  else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 11:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                    else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 12:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=1;
                                             tomorrow.day=1;
                                             tomorrow.year=today.year+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, tomorrow.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
         
              default:
                      printf("Enter a valid date, month and year");              
                   
     }
     printf("\nWould you like to input again? y/n ");
     again=getche();
     printf("\n");
     if(again=='y' || again=='Y')
     {
                   goto again;        
     }

     getch();
}

Get two integers and operator from user and apply given operator on given numbers and display answer

#include<stdio.h>

#include<conio.h>
int cal(int,char,int);
int main()
{
int n1, n2;
char sign;
printf("enter 1st value & 2nd value\n");
printf("and press enter\n");
scanf("%d %d", &n1, &n2);
printf("entetr operator +, -, x, /, %\n");
sign = getch();
cal(n1, sign, n2);

getch();
}
int cal(int x,char aop, int y)
{
switch(aop)
{
case '+':
return printf(" the sum is %d\n",x+y);
break;

case '-':
return printf(" the subtract is %d\n",x-y);
break;

case '*':
return printf("the muliply is %d\n",x*y);
break;

case '/':
return printf("the division is %d\n",x/y);
break;

case '%':
return printf("the reamainder is %d\n",x%y);
break;
default:
return printf("invalid input");
}}

write a program to find the average, sum, subtraction, product, remainder of given numners using switch statement (calculator)


#include<stdio.h>
#include<conio.h>


int main()
{
int a,b,c,d,e,f,g,h,i,j,subtract,sum,multiply,average,remainder,alpha;
    int floataverage=0; 
    
    printf("Enter any 10 numbers:\n");
    printf("Enter the operator 1,2,3,4\n");
  scanf("%d %d %d %d %d %d %d %d %d %d",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j);
  printf("enter add for case 1\n enter sub for case 2\n enter multi for case 3\n enter avg for case 4\n enter remainder for case 5\n");
  scanf("%d",&alpha);


switch(alpha)
{
case 1:
sum=a+b+c+d+e+f+g+h+i+j;
printf("sum=%d\n",sum);
break;
case 2:
subtract=a-b-c-d-e-f-g-h-i-j;
printf("difference=%d\n",subtract);
break;
case 3:
multiply=a*b*c*d*e*f*g*h*i*j;
printf("product=%d\n",multiply);
break;
case 4:
average=(a+b+c+d+e+f+g+h+i+j)/10;
printf("avg=%d\n",average);
break;
case 5:
remainder=a%b;
printf("remainder=%d",remainder);
break;
default:
printf("invalid command");
}
getch();
}