Jun 30, 2020

C Program for Swap Two Numbers using Functions

Ex: Write a C program for swap two numbers using function. How to write a C program for swap two numbers using function. C program for swap two numbers using function.

Input from user:


Enter two numbers: 10 20


Expected output:


Numbers before swap:

number1=10
number2=20

Numbers after swaping:

number1=20
number2=10




  Step by step logic of the given program:



1. First declare function give it meaningful name swap().

2. Inside the main() accept input (two numbers) from user declare two varibles say no1 & no2 .

3. After that pass two values to the function using- swap(no1,no2) (calling the function).


5. In the body of swap()  declare third variable temp and swap the values using-
temp=a;
a=b;
b=temp;

5. After that print swaped values to the output screen.




  C program for swap two numbers using functions:


#include<stdio.h>
int swap(int,int);

int main()
{
int no1,no2;

printf("Enter two numbers:\n");
scanf("%d%d",&no1,&no2);

printf("Numbers before swap:\nnumber1= %d\nnumber2= %d",no1,no2);

swap(no1,no2);

}

int swap(int a,int b)
{
int temp;

temp=a;
a=b;
b=temp;

printf("\n\nNumbers after swap:\nnumber1=%d\nnumber2=%d",a,b);

return 0;

}



Above program shows the following output:


C program for swap two numbers using functions



C Program to print Square of given Number using Function

Ex: Write a C program to print square of given number using function. How to write a C program to print square of given number using function. C program to print square of given number using function.

Input from user:

Enter the number: 4

Expected output:

Square of given number is= 16.0000





  Step by Step logic of the given program:



1. First declare function give it meaningful name square().

2. Inside the main() accept input (number) from user declare varible say no.

3. Call the square() inside the printf() directly (It will print returned value means square of given number).

4. In the body of square() use simple logic of square which is-
Square=no*no;

5. After that return square.




  C Program to print Square of given Number using Function:


#include<stdio.h>
float square(float);

int main()
{
float no;
printf("Enter the number: ");
scanf("%f",&no);

printf("Square of given number is= %f",square(no));
}
float square(float no)
{
float square=no*no;
return square;
}


Above program shows the following output:


C Program to print Square of given Number using Function

Jun 29, 2020

C program to check given number is prime or not using function

Ex: write a C program to check given number is prime or not using function. How to write a C program to check given number is prime or not using function. C program to check given number is prime or not using function.


Input from user:

Enter the number: 5

Expected output:

Given number is prime number.





  Step by Step logic of the given program:



1. First declare function give it meaningful name prime().

2. Inside the main() accept input (number) from user declare varible say no.

3. Using if statement call prime function- if(prime(no)). after that inside the if statement print given number is prime otherwise print number is not prime. (If returned value is 1(true) then it will print number is prime otherwise it will execute else block

4. After that inside the prime() we will use following prime numbers logic-
for(i=1;i<=no;i++)
{
if(no%i==0)
{
count++;
}
}

Here, count will be 2 only if number is prime.


5. In the last check count is 2 or not using- if(count==2) 
If it's true return 1 otherwise return 0;





  C program to check given number is prime or not using function:


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

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

if(prime(no))
    {
    printf("Given number is prime number");
    }
    else
    {
    printf("Given number is not prime number");
    }
}
int prime(int no)
{
int count=0,i;

for(i=1;i<=no;i++)
{
if(no%i==0)
{
count++;
}
}
if(count==2)
{
return 1;
}
else
{
return 0;
}
}


Above program shows the following output:


C program to check given number is prime or not using function



Feel freee to drop comments in the comment section given below...


  You May Like:



Jun 28, 2020

C program to print sum of two numbers using functions

Ex: Write a C program to print sum of two numbers using function. How to write a C program to print sum of two numbers using function. C program to print sum of two numbers using function.

Input from user:


Enter two numbers: 35 15


Expected output:


Sum = 50





  Step by step logic of the given program:


1. First declare function give it meaningful name add().


2. Inside the main() accept input (two numbers) from user, declare two varibles say no1 & no2 .

3. inside the sum pass no1 and no2 to the function using: sum= add(no1, no2);

4. After that print sum.


5. In the body of add() add n1 and n2 and store it some varible say sum.


After that return sum.







  C program to print sum of two numbers using functions:

#include<stdio.h>
int add(int n1,int n2); /*function declaration*/

int main()
{
    /*Variable declaration*/
    int no1,no2, sum;

    /*Input two numbers from user*/
    printf("Enter two numbers: ");
    scanf("%d%d", &no1,&no2);

    
    sum=add(no1, no2);

    /*Print value of sum*/
    printf("Sum = %d",sum);

    return 0;
}


int add(int n1,int n2)
{
    int sum= n1+n2;

    /*Return value of sum to the main*/
    return sum;
}


Above program shows the following output:


C program to print sum of two numbers using functions


Jun 24, 2020

C program to print factorial of given number using function

Ex: write a C program to print factorial of given number using function. How to write a C program to print factorial of the given number using function. C program to print factorial of given number using function.

Input from user:

Enter the number: 5

Expected output: 

Factorial of given number is 120.


In this program we will used recursion function
to find factorial of given number.
In short recursion function means- function calling it self that means call a function inside the same function then it is called a recursive call of the function.




  Step by step logic to print factorial of given number using function:


1. First declare function give it meaningful name fact().

2. Inside the main() accept input (number) from user declare variable say no.

3. Call the fact() inside the printf() directly (It will print returned value means factorial of given number).

4. In the body of fact() use  one if statement to check number is less than 1. If it is less than or equal to 1 return 1.

5. Otherwise use following factorial logic no=no*fact(no).

After that return no.






  C program to print factorial of given number using function:



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

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

printf("Factorial of given number is %d",fact(no));
}
int fact(int no)
{
   if(no<=1)
   {
    return 1;
   }
   else
   {
    no=no*fact(no-1);
    return no;
   }
}

Above program shows the following output:


C program to print factorial of given number using function

C program to print given number is even or odd using function

Ex: write a C program to print given number is even or odd using function. How to write a C program to print given number is even or odd using function. C program to print given number is even or odd using function.


Input from user:

Enter the number: 24

Expected output:

Given number is even




  Step by step logic of the given program:


1. First declare function give meaningful name say even().

2.  Inside the main() accept input(number) from user, declare varible say no.

3.  Call function and store returned value in variable r.

4.  If  returned value is 0 if(r==0) print is even otherwise print is odd.

5. inside the function check number is even or odd using if(no%2==0) return 0 else return 1.





  C program to print given number is even or odd using function:


#include<stdio.h>
int even(int);

int main()

{
int no,r=0;
printf("Enter the number\n");
scanf("%d",&no);

r=even(no);

if(r==0)
printf("Given number is even");
else
printf("Given number is odd");
}
int even(int no)
{
if(no%2==0)
{
return 0;
}
else
{
return 1;
}
}



Above program shows the following output:


C program to print given number is even or odd using function



If you like this article, then don't forget to share it with your friends and colleagues. If you have any doubts and feedback then feel free to use below comment section.

Jun 22, 2020

Function's in C with there syntax, advantages & types

Definition: Function is a self-contained block of statement that perform a coherent tasks. using function  we can divide a large program into the basic building blocks.

There are many functions we are used already such as- printf()scanf()pow()sqrt() and most important function is main().


  Types of function's:


There are two types of function's.

  1. Standard library functions.
  2. User-defined functions.


1. Standard library functions:  functions which already have a definition in header files(like stdio.h) this type of functions are called standard library functions. Which are printf()scanf()puts()gets() etc. 


2. User defined functions: The functions that we create in a program this type of functions are called as user defined functions.
In this tutorial we will learn how works user defined functions.


Syntax:


 return_type function_name (argument list)
 {
 // block of statements
 }




  • How It works?:
1.Return type: means  datatype such as int, double, char, float, void, short etc.

2.Function name: you can give any name to function. But to understand easily we will give meaningful name to functions.

3.Argument list: It contains variable names with there data types.


4.Block of statement/code: It will be executed whenever a call will be made to the function.





   Advantages of user defined functions:


1. The program will easier to understand by using functions.

2. It makes possible top down modular programming.

3. The length of the program can be reduced by using functions.

4. Large programs we can divide into  smaller modules using functions.



  Example program of user defined function:


Below program shows the working of the user defined functions.


#include<stdio.h>
void function();//function prototype declaration

void main()
{
 printf("Hii from main()\n");
 
 function();//function calling
}
void function()//function definition
{
 printf("Hii from user defined function");
}


Above program shows the following output:


Function's in C with there syntax, advantages & types


Jun 21, 2020

Function's in C with Exercises and Solutions

Function is a self-contained block of statement that perform a coherent tasks. using function  we can divide a large program into the basic building blocks.
    
For more details click on the below link:



There are many functions we are used already in our C program's such as- printf(), scanf(), pow(), sqrt() and most important function is main().




There are two types of function's in C:



If you think above examples are helpful, then don't forget to share it with you friends and if you have any doubts then drop a comment in the comment section given below.

Jun 20, 2020

Switch-Case in C with Exercises and Solutions

Switch case is used when we need to test the value of a variable and compare it with multiple cases if case match is found then block of code will be executed and if match is not found, then default statement will be executed.


In this exercise we will practice lot of Switch case questions or examples with their solutions. Below is the list of switch case examples with their solutions.


  Exercises and examples of Switch-case:


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




  You Might Like:




Thanks for reading this article. If you have any questions, then feel free to drop a comments.... :)

Jun 19, 2020

C program to check password is correct or incorrect using switch-case

Ex: write a C program to check password is correct or incorrect using switch-case. How to write a C program to check given password is correct or incorrect using switch-case. C program to check password is correct or incorrect using switch-case.

Input from user:

Enter your password: 1010

Expected output:

Welcome



Quick links:





  Step by step logic of the given program:


1. Accept input (password) from user declare variable say ps.

2. Switch the value of ps using switch(ps).

3. There is 1 possible value of ps(1010).

4. Therefore write 1 case inside the switch.

5. If  case does not match then for default case print incorrect password. 





  C program to check password is correct or incorrect using switch-case :


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

printf("Enter your password: ");
scanf("%d",&ps);

switch(ps)
{
case 1010:
printf("--WELCOME--");
break;

default:
printf("-INCORRECT PASSWORD-");
}
return 0;
}



Above program shows the following output:


C program to check password is correct or incorrect using switch-case

C program to print day of week using switch-case

Ex: write a C program to print day of week using switch-case. How to write a C program to print day of week using switch-case. C program to print day of week using switch-case.

Input from user:

Enter number of day: 4

Expected output:

Thursday



Quick links:






  Step by step logic of the given program:


1. Accept day number from user declare variable say no.

2. Switch the value of no using switch(no).

3. There are six possible values of no which are 1,2,3,4,5,6&7.

4. Therefore write 6 cases inside the switch.

5. If any case does not matches then for default case print enter number between 1-7.






  C program to print day of week using switch-case:


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

 int no;

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

  switch(no)
  {

  case 1:  printf("\nMonday");
  break;

  case 2:
printf("\nTuesday");
  break;

  case 3:
printf("\nWednesday");
  break;

  case 4:
printf("\nThursday");
  break;

  case 5:
printf("\nFriday");
  break;

  case 6:
printf("\nSaturday");
  break;

  case 7:
printf("\nSunday");
  break;
  
  default:
  printf("Enter number between 1-7");

 }
 return 0;

}


Above program shows the following output:


C program to print day of week using switch-case

Jun 18, 2020

C program to create calculator using switch-case

Ex: Write a C program to create calculator using switch-case. How to write a C program to create calculator using switch-case. C program to create calculator using switch-case.

Input from user:

Enter Number1[+,-,*,/] Number 2:
125/25

Expected output:

Answer is= 5.000000




Quick links:



  Step by step logic to create calculator using switch-case:


1. Accept two numbers and operator from user variable say no1, no2 , op.

2. Switch the value of op using switch(op).

3. There are four possible values of op +, -, *, /.

4. For case '+' perform addition and  print this, similarly for case '-' perform subtraction and print this using printf.

5. Repeat this process for multiplication and division.

6. In the last for default case print Invalid Operator.





  Program to create calculator using switch-case:


#include<stdio.h>
int main()
{
    char op;
    float no1, no2;

 printf("\t  SIMPLE CALCULATOR\n");
 printf("-------------------------------------\n");
 printf("Enter  Number1 [+,-,*,/] Number2 \n");
scanf("%f%c%f", &no1,&op,&no2);

 switch(op)
    {
        case '+': 
    printf("Answer= %f",no1+no2);
        break;

        case '-': 
    printf("Answer= %f",no1-no2);
        break;

        case '*': 
    printf("Answer= %f",no1*no2);
        break;

        case '/': 
    printf("Answer= %f",no1/no2);
        break;

        default
     printf("Invalid operator");
    }
    
    return 0;
}



Above program shows the following output:


C program to create calculator using switch-case, Create calculator using switch case in C programming language

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


Jun 17, 2020

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