C program to accept id from user to confirm department using switch-case

Ex: write a C program to accept id from user and display department. How to write a C program to accept id from user and display department. C program to accept id from user and display department.

Input from user:

Enter your ID: 11

Expected output:

Software development



    Quick links:


  Step by step logic of the given program:


1. Accept input(id) from user declare variable say id.

2. Switch the value of id using switch(id) and match with cases.

3. Inside the body of switch we will give 6 possible values of no

4. Therefore write 6 cases inside the switch.

5. If any case does not matches then for default case print invalid ID.



  C program to accept id from user to confirm department using switch-case:


#include<stdio.h>
void main()
{
int id;

printf("Enter your ID: ");
scanf("%d",&id);

   switch(id)
   {
    case 11:
    case 12:
    case 13:
   printf("Software department");
    break;
   
    case 21:
    case 22:
    case 23:
  printf("Developer department");
    break;
   
    default:
    printf("Invalid ID");
   }

}


Above program shows the following output:


C program to accept id from user to confirm department using switch-case


C program to print number between 1 to 10 in character format using switch-case

 Ex: write a C program to print  number between 1 to 10 in character formate. How to write a C program to print number between 1 to 10 in character format. C program to print number between 1 to 10 in character format.

Input from user:

Enter the number between 1 to 10:  5

Expected output:

You entered FIVE



    Quick links:


  Step by step logic of the given program:


1. Accept input (number) from user variable say no.

2. Switch the value of no using switch(no) and match with cases.

3. Inside the body of switch we will give 10 possible values of no  i.e. 1 to 10

4. Therefore we will write 10 cases inside the switch.

5. If any case does not matches then for default case print invalid number.



  C program to print  number between 1 to 10 in character format using switch-case:



#include<stdio.h>
void main()
{
int no;

printf("Enter the number between 1 to 10: ");
scanf("%d",&no);

switch(no)
{
case 1:
printf("You entered ONE");
break;
 
 case 2:
printf("You entered TWO");
 break;
 
 case 3:
printf("You entered THREE");
 break;
 
 case 4:
printf("You entered FOUR");
 break;
 
 case 5:
printf("You entered FIVE");
 break;
 
case 6:
printf("You entered SIX");
 break;
 
 case 7:
printf("You entered SEVEN");
 break;
 
 case 8:
printf("You entered EIGHT");
 break;

case 9:
printf("You entered NINE");
 break;

case 10:
printf("You entered TEN");
 break;
 
default:
printf("Invalid number");

}
}



Above program shows the following output:


C program to print  number between 1 to 10 in character format using switch-case

Switch-case in C Definition and Syntax

Definition: The control structure that allows us to make decision from the number of choices is called as switch

Below is shown syntax of Switch case.

Syntax:



  What is break statement?


Switch statement tests the value of a variable and compares it with multiple cases. Ones the case match is found a block of statements associated with that particular case is executed.

To Explore switch-case statements examples and their solutions ðŸ‘‰Click Here.

C program using do-while loop to check inside or outside the loop

Ex: Write a C program to check program is inside or outside the loop. How to write a C program to check program is inside or outside the loop.  C program to check program is inside or outside the loop. 


This program shows the working of do-while loop.




   Step by step logic of the given program:

1. First declare integer variable i=0.

2. After that inside the do-while loop accept input from user.

3. Give condition while(i!=1) that means the loop will iterate until 1 is entered.




   C program using do-while loop to check inside or outside the loop:


#include<stdio.h>
int main()
{
int i=0;

do
{
printf("\nYou are inside the loop");
printf("\nPress 1 to exit from loop: ");
scanf("%d",&i);
}while(i!=1);

printf("\nYou are out of do-while loop");

return 0;

}



Above prgram shows the following output:



C program to print numbers till 5 using do-while loop

Ex: write a C program to print number if it is less than or equal to 5 using do while loop. How to write a C program to print number if it is less than or equal to 5 using do while loop. C program to print number if it is less than or equal to 5 using do while loop. 


Note:
do-while loop runs at least one even if the condition is false because the condition is evaluated, after the execution of the body of loop.


   Step by step logic of the given program:


1. First declare intiger variable i=1.

2. After that inside the do while print value of i.

3. Increment value of i. 
Give condition while(i<=5).





   Program to print numbers till 5 using do-while loop:


#include<stdio.h>
int main()
{
int i=1;

do
{
printf("%d\n",i);
i++;
}while(i<=5);

return 0;
}



Above prgram shows the following output:


C Program To Print Fibonacci Series

Ex: Write a C program to print Fibonacci series. How to write a C program to print Fibonacci series. C program for Fibonacci series.

Input from user:

Enter number for how many times generate series: 7

Expected output:

Fibonacci series is: 0 1 1 2 3 5 8 13 21





   Step by step logic of the given program:


1. First  declare intiger variable's  no(number), a=0, b=1, c, i .

2. After that accept input (number) from user.

3. print value of a and b directly. (and decrease number by 2 in the for loop)

4. After that use one for loop which looks like. 
for(i=0;i<no-2;i++)

5. Inside the for loop Fibonacci series calculate using..

c=a+b;
a=b;
b=c;

6. After that Print value of c inside the for loop.





   C program to print Fibonacci series :



#include<stdio.h>
int main()
{

 int no,a=0,b=1,c,i;

printf("Enter number for how many times generate series:");
 scanf("%d",&no);

 printf("FIBONACCI SERIES: ");

 printf("%d %d ",a,b);

 for(i=0;i<no-2;i++)
 {
c=a+b;
a=b;
b=c;

   printf("%d ",c);
 }

 return 0;
}




Above prgram shows the following output:



C Program to Find Product of Digits of a Number

Ex: Write a C program to print product of digits of a number. How to write a C program to print product of digits of a number. C program to find product of digits of number.


Input from user:

Enter the number: 12345

Expected output:

Product of given number is: 120





   Step by step logic of the given program:

1. Accept input (number) from user.

2. Use one while loop to check given number is greater than 0 or not.

3. Loop will iterate if number is greater than 0.

4. Inside the while loop product of digit calculate using...

z=no%10;
product*=z;
no=no/10;

5. Print product on the output screen.





   C Program to find product of digits of a number:


#include<stdio.h>
int main()
{
int no,z;
long long int product=1ll;

printf("Enter the number\n");
scanf("%d",&no);

while(no!=0)
{
z=no%10;
product*=z;
no=no/10;
}
printf("product of given number is:%lld",product);

return 0;

}


Above prgram shows the following output:



C Program to Check Given Number is Palindrome or Not

Ex: write a C program to check given number is palindrome or not. How to write a C program to check given number is palindrome or not. C Program to check given number is palindrome or not.


Input from user:


Enter the number: 12321


Expected output:

Given number is palindrome.





   Step by step logic of the given program:



1. Accept input (number) from user.

2. After that use one while loop to check given number is greater than zero or not.

3. If number is greater than 0 then while loop will iterate.

4. Use reverse number logic inside the loop to calculate reverse number.

reverse=reverse*10; reverse=reverse+(no % 10);
no=no/10;

5. After that using if statement check condition
if(reverse==num)

6. Then print given number is palindrome. Otherwise print given numbers is not palindrome.





   C program to check given number is palindrome or not:

#include<stdio.h>
int main()
{
int no,num,reverse=0;

    printf("Enter the number:");
    scanf("%d",&no);
    num=no;
    
   while(no!=0)
    {
reverse=reverse*10; reverse=reverse+(no % 10);
no=no/10;   
    }    
    if(reverse==num)
    {
    printf("Given number is palindrome");
    }
    else
    {
    printf("Given number is not palindrome");
    }
    
    return 0;

}



Above prgram shows the following output:



C program to reverse a given number using while loop

Ex: Write a C program for reverse a given number. How to write a C program to reverse a given number. C program for reverse a given number.

Input from user:

Enter the number: 12345


Expected output:

Given number in reverse order: 54321





   Step by step logic of the given program:


1. Accept input (number) from user.

2. Use one while loop to check given number is greater than 0 or not.

3. After that inside the while loop add reverse number logic.

4. Inside the loop the reverse number computed using..

reverse=reverse*10;
reverse=reverse+no%10;
no=no/10;

5. Print reverse number on the output screen.





   C program to reverse a given number using while loop:



#include<stdio.h>
int main()
{
 int no,reverse=0;

printf("Enter the number\n");
scanf("%d",&no);

    while(no>0)
    {
reverse=reverse*10;
reverse=reverse+no%10;
no=no/10;
    }

printf("Given number in reverse order:%d",reverse);

return 0;
}




Above program shows the following output:






C Program to Print Digit Sum Using While Loop

Ex: write a C program to print digit sum. How to write a C program to print digit sum. C program to print digit sum.


Input from user:

Enter the number: 145

Expected output:

Sum of digit=10





  Step by step logic of the given program:


1. Accept number from user.

2. Use while loop to check given number is greater than zero or not.

3.  Enter given digit sum logic inside the while loop.

z=no%10;
sum=sum+z;
no=no/10;


4. Print sum outside the while loop.





  C program to print digit sum using while loop:


#include<stdio.h>
void main()
{
int no,z,sum=0;

printf("Enter the number:");
scanf("%d",&no);

while(no>0)
{
z=no%10;
sum=sum+z;
no=no/10;
}
printf("sum of digit=%d",sum);

}



Above prgram shows the following output:

C program to print digit sum using while loop