Home
Diploma Practice C Online People
Certificate Contact Us

Solutions for assignments on If and switch statements

as01.c

#include <stdio.h>

int main()
{
  int num;

  printf("Enter a number: ");
  scanf("%d", &num);

  if(num % 2)
    printf("The number %d is Odd number\n",num);
  else
    printf("The number %d is Even number\n",num);
}

as02.c

#include <stdio.h>

int main()
{
  int num;

  printf("Enter a number: ");
  scanf("%d", &num);

  if(num & 1)
    printf("The number %d is Odd number\n",num);
  else
    printf("The number %d is Even number\n",num);
}

as03.c

#include <stdio.h>

int main()
{
  int num;

  printf("Enter a number: ");
  scanf("%d", &num);

  if(num < 0)
    printf("%d is a negative number\n",num);
  else
    printf("%d is a positive number\n",num);
}

as04.c

#include <stdio.h>

int main()
{
  int num;

  printf("Enter a number: ");
  scanf("%d", &num);

  if(num & (1<<31))
    printf("%d is a negative number\n",num);
  else
    printf("%d is a positive number\n",num);
}

as05.c

#include <stdio.h>

int main()
{
  char ch;

  printf("Enter a character: ");
  scanf("%c", &ch);
  printf("Entered char is : %c\n", ch);

  if((ch >= 'a') && (ch <= 'z'))
  {
    printf("%c is small letter\n",ch);
  }
  else if( ( ch >='A' ) && ( ch <= 'Z') )
  {
    printf("%c is capital letter\n",ch);
  }
  else if( ( ch >= '0' ) && ( ch <= '9') )
  {
    printf("%c is numerical digit\n",ch);
  }
  else
    printf("%c is other than small,capital and digit\n",ch);
}

as06.c

#include <stdio.h>

int main()
{
  char ch;

  printf("Enter a character: ");
  scanf("%c", &ch);
  printf("Entered char is : %c\n", ch);

  if((ch >= 'a') && (ch <= 'z'))
  {
    printf("character after converting to capital is %c\n",ch-32);
  }
  else if( ( ch >='A' ) && ( ch <= 'Z') )
  {
    printf("character after converting to small is %c\n",ch+32);
  }
  else
    printf("Entered character is neither capital nor small\n");
}

as07.c

#include <stdio.h>

int main()
{
  int num1, num2;

  printf("\nEnter two numbers: ");
  scanf("%d%d", &num1, &num2);

  if((num1 % num2)==0)
    printf("divisibility test passed\n");
  else
    printf("divisibility test failed\n");
}

as08.c

#include <stdio.h>

int main()
{
  int num1, num2;

  printf("Enter two numbers\n");
  scanf("%d%d",&num1, &num2);

  if(num1 > num2)
    printf("%d is bigger number\n",num1);
  else if(num2 > num1)
    printf("%d is bigger number\n",num2);
  else
    printf("Both numbers are equal\n");
}

as09.c

#include <stdio.h>

int main()
{
  int num1, num2, num3;

  printf("Enter three numbers\n");
  scanf("%d%d%d",&num1, &num2, &num3);

  if((num1 > num2) && (num2 > num3))
    printf("Numbers are in decending order\n");
  else if((num1 < num2) && (num2 < num3))
    printf("Numbers are in ascending order\n");
  else if((num1 == num2) && (num2 == num3))
    printf("All Numbers are eaual\n");
  else
    printf("Numbers are in random order\n");
}

as10.c

#include <stdio.h>
#include <math.h>

int main()
{
  float radius,vol,weight,density;
  int metal;

  printf("Enter the radius of sphere in centimeters as float\n");
  scanf("%f", &radius);
  printf("Following are metal types\n\n");
  printf("1:Aluminum  2:Copper  3:Gold  4:Iron   5:Silver\n\n");
  printf("Enter metal number\n");
  scanf("%d", &metal);

  switch(metal)
  {
    case 1:
      density = 2.712;
      break;
    case 2:
      density = 8.94;
      break;
    case 3:
      density = 19.32;
      break;
    case 4:
      density = 7.85;
      break;
    case 5:
      density = 10.49;
      break;
    default:
      printf("Invalid metal nubmer. ...exiting the program\n");
      return 1;
  }
 
  vol = (4.0/3.0) * M_PI * radius * radius * radius;
  weight = vol * density;
  printf("Radius: %f   Volume: %f    Density: %f   Weight: %f\n",
          radius, vol,density,weight);
}

as11.c

#include <stdio.h>

int main()
{
  int num1, num2;
  char opr;

  printf("Enter two numbers\n");
  scanf("%d%d",&num1, &num2);
  printf("Following are arithmetic operators:\n");
  printf(" +  -  *  /   %%\n");
  printf("Enter any one arithmetic operator character: ");
  __fpurge(stdin);
  scanf("%c",&opr);
  if(opr == '+')
    printf("%d %c %d = %d\n",num1, opr, num2, num1+num2);
  else if(opr == '-')
    printf("%d %c %d = %d\n",num1, opr, num2, num1-num2);
  else if(opr == '*')
    printf("%d %c %d = %d\n",num1, opr, num2, num1*num2);
  else if(opr == '/')
    printf("%d %c %d = %d\n",num1, opr, num2, num1/num2);
  else if(opr == '%')
    printf("%d %c %d = %d\n",num1, opr, num2, num1%num2);
}

as12.c

#include <stdio.h>

int main()
{
  int num1, num2;
  char opr;

  printf("Enter two numbers\n");
  scanf("%d%d",&num1, &num2);

  printf("Following are arithmetic operators:\n");
  printf(" +  -  *  /   %%\n");
  printf("Enter any one arithmetic operator character: ");
  __fpurge(stdin);
  scanf("%c",&opr);

  switch(opr)
  {
    case '+':
      printf("%d %c %d = %d\n",num1, opr, num2, num1+num2);
      break;
    case '-':
      printf("%d %c %d = %d\n",num1, opr, num2, num1-num2);
      break;
    case '*':
      printf("%d %c %d = %d\n",num1, opr, num2, num1*num2);
      break;
    case '/':
      printf("%d %c %d = %d\n",num1, opr, num2, num1/num2);
      break;
    case '%':
      printf("%d %c %d = %d\n",num1, opr, num2, num1%num2);
      break;
  }
}

as13.c

#include <stdio.h>

int main()
{
  int month; 

  printf("Enter month (1 for Jan, 2 for Feb ... 12 for Dec)\n");
  scanf("%d",&month);

  switch(month)
  {
    case 1:
      printf("Number of days in January are 31\n");
      break;
    case 2:
      printf("Number of days in February are 28\n");
      break;
    case 3:
      printf("Number of days in March are 31\n");
      break;
    case 4:
      printf("Number of days in April are 30\n");
      break;
    case 5:
      printf("Number of days in May are 31\n");
      break;
    case 6:
      printf("Number of days in June are 30\n");
      break;
    case 7:
      printf("Number of days in July are 31\n");
      break;
    case 8:
      printf("Number of days in August are 31\n");
      break;
    case 9:
      printf("Number of days in September are 30\n");
      break;
    case 10:
      printf("Number of days in October are 31\n");
      break;
    case 11:
      printf("Number of days in November are 30\n");
      break;
    case 12:
      printf("Number of days in December are 31\n");
      break;
  }
}

as14.c

#include <stdio.h>

int main()
{
  int month, year; 

  printf("Enter month (1 for Jan, 2 for Feb ... 12 for Dec)\n");
  scanf("%d",&month);
  printf("Enter year\n"); 
  scanf("%d",&year);

  switch(month)
  {
    case 1:
      printf("Number of days in January are 31\n");
      break;

    case 2:
      if(year%4)
        printf("Number of days in February are 28\n");
      else
        printf("Number of days in February are 29\n");
      break;

    case 3:
      printf("Number of days in March are 31\n");
      break;

    case 4:
      printf("Number of days in April are 30\n");
      break;

    case 5:
      printf("Number of days in May are 31\n");
      break;

    case 6:
      printf("Number of days in June are 30\n");
      break;

    case 7:
      printf("Number of days in July are 31\n");
      break;

    case 8:
      printf("Number of days in August are 31\n");
      break;

    case 9:
      printf("Number of days in September are 30\n");
      break;

    case 10:
      printf("Number of days in October are 31\n");
      break;

    case 11:
      printf("Number of days in November are 30\n");
      break;

    case 12:
      printf("Number of days in December are 31\n");
      break;
  }
}


#401, Sai Sushma Homes, 23/A, S.R. Nagar Main Road, Hyderabad-500038, India.
Ph: +91-40-48508764, 7702028989, depik.help@gmail.com