💻
C - Structured Programming Language
  • C - Structured Programming Language
  • Structured Programming Language
    • Chapter 0: Overview
      • Overview of Programming
      • Overview of C Programming
      • History of C Programming
      • IDE - Code::Blocks
      • New File Creation and Managing Program Files in Folders
      • C Program Basic Structure
      • Sections of the C Program
      • Steps for Using a Program
      • Flow of a Program Execution
      • First C Program - Hello World
      • Comment
      • Lexical Elements (/Tokens)
      • Keywords
      • Character Set
    • Chapter 1: Data Type, IO
      • Data Type
      • Variable
        • Variable Declaration for Primitive Data Type
        • Variable Address and Input
        • Constant and Macro
        • Variable Scope and Lifetime
        • Variable Storage Type
        • Variable Declaration Vs Definition Vs Initialization
        • Variable Default Value
        • Identifier Naming Rules and Conventions
      • Primitive Data Type: Format Specifier and Size
      • Literals
      • Primitive Data Type: Input Output
        • Input Output Function
        • Integer IO
        • Real Number IO
        • Character IO
        • String IO
        • Formatted Output
        • Formatted Input
        • Return of Scanf and Printf
      • IO Error
      • Escape Sequence / Backslash Characters
      • Data Type Modifiers
        • Data Type Size and Format Specifier
        • Input Output Using Modifiers
      • Data Type Limit
        • Integer Number
        • Floating Point Number
      • Memory Segment
      • Type Casting
      • Declaration of Derived Data Type
        • Array
        • Pointer
        • Function
        • Structure
        • Union
      • Declaration of User Defined Data Type
        • Enumeration
          • Enum Constant Value
          • Enumeration Exercise
        • Typedef
      • Declaration of Other Data Type
        • Boolean
        • Complex and Imaginary Number
        • File
        • Clock and Time
    • Chapter 2: Operator
      • Types of Operators
      • Assignment Operator
      • Arithmetic Operators
        • Math Operation
      • Increment, Decrement Operators
      • Relational Operators
      • Logical Operators
      • Bitwise Operators
      • Miscellaneous Operators
      • Operator Precedence and Associativity
      • Programming Syntax & Expression
      • Operator Exercise
    • Chapter 3: Conditional Statement
      • Truth Value
      • If Else Exercise
        • Zero, Positive, Negative - Number Sign Test
        • Voting Eligibility
        • Complex Numbers Equality
        • Different Example
          • Print a Digit into Word
          • Grade Evaluation
          • FizzBuzz
          • EVEN or ODD
          • Leap Year
          • Maximum of 2 Numbers
          • Maximum of 3 Numbers
          • Minimum of 2 Numbers
          • Minimum of 3 Numbers
      • Ternary Operator
        • Ternary Operator Exercise
      • Switch Statement
        • Switch Statement Exercise
    • Chapter 4: Loop
      • Loop Basic
      • Series Printing using Loop
      • Summation and Factorial
      • Loop Exercise
      • Number Theory
      • Break and Continue
      • Nested Loop
      • Managing Test Case
      • Goto Statement
    • Chapter 5: Pointer and Dynamic Memory Allocation
      • Pointer Fundamental Concept
        • Pointer Declaration
        • Types of Pointer
        • Pointer for Primitive Data Types
        • Pointer Referencing, Initialization, Accessing, Dereferencing
        • Pointer Size
      • Pointer Operation
        • Value Assignment/Update
        • Pointer Input Output
        • Pointer and Operator
          • Assignment Operator
          • Arithmetic Operator
          • Increment/Decrement Operator
          • Relational Operator
        • Pointer and Flow Control
      • Multilevel Pointer
      • Dynamic Memory Allocation (DMA)
        • Dynamic Memory Allocation (DMA)
        • DMA for Primitive Data Types
        • Pointer Operation after DMA
    • Chapter 6: Function & Recursion
      • Type of Function
      • Function Exercise
      • Conditional Statement and Function
      • Loop Exercise using Function
      • Pointer and Function
      • Recursion
        • Example
        • Pointer
    • Chapter 7: Array
      • Array Fundamental Concept
        • Necessity
        • Declaration (1D Array)
        • Initialization
        • Access
        • Assignment
        • Address
        • User Input
          • Test Case
      • Function and Array
      • Basic Array Operation
        • Statistical Operations
          • Summation
          • Average (Mean), Median
          • Frequency Table, Mode
          • Standard Deviation, Variance
          • Covariance, Correlation
        • Searching
          • Linear Search
          • Binary Search
          • Maximum
          • Minimum
        • Update
          • Index Based
          • Element Based
        • Delete
          • Index Based
          • Element Based
        • Insert
          • Index Based
          • Element Based
        • Sorting
          • Selection Sort
          • Insertion Sort
          • Bubble Sort
      • Other Array Operation
        • Copy
        • Reverse
        • Compare
        • Merge
        • Split
        • Cyclic Permutation
      • Pointer and Array
        • Basic
        • Insert
        • Delete
        • Summation
        • Reverse
        • Compare
        • Merge
        • Split
        • Cyclic Permutation
      • Pointer and Array
      • DMA and Array
      • 2D Array
    • Chapter 8: Character and String
      • Character
        • Declaration and Initialization
        • ASCII Code
        • Input Output
        • Conditional Statement
        • Character Arithmetic
      • String
        • String Declaration and Initialization
        • Input Output
        • Character Count in String
        • Character Conversion in String
        • String Operations
          • Substring
      • String and Pointer
    • Chapter 9: Structure
      • Basic of Structure
      • Structure Pointer
      • Function and Structure
      • Nested Structure
      • DMA and Structure
    • Chapter 10: File
    • Miscellaneous
      • Working with Math Functions
      • Working with Time Functions
      • Union
      • Preprocessor
      • Custom Header File
      • Error Handling
      • Graphics
      • Other
      • Coding Convention
      • Identifiers (Variable, Function & Structure) Naming Convention
      • Sizeof
      • Exit, Abort
      • Assert
    • Reference
      • Online Learning Material
  • Exercise
    • Problem Set 0: IDE, First C Program - 4
    • Problem Set 1: Data Types, IO - 55
      • Data Type, Variable, Format Specifier, Input, Output, String - 33
      • Data Type, Input, Output - 8
      • Data Type, Data Type Modifier, Format Specifier, Size, Limit, Address - 9
      • Formatted Input-Output - 4
    • Problem Set 2: Operator - 108
      • 2.1 Assignment Operator - 11
      • 2.2 Arithmetic Operator - 73
        • 2.2.1 Arithmetic Operator [5]
        • 2.2.2 Arithmetic Operator Exercise [10]
        • 2.2.3 Scale Conversion [25]
        • 2.2.4 Geometrical Shapes [35]
      • Bitwise Operator - 11
      • Use of Library Functions - 23
    • Problem Set 3: Conditional Statements - 47
    • Problem Set 4: Loop - 124
      • Sequence and Series - 33
      • More Loop Exercise - 52
      • Test Case - 7
      • Loop Nested - 32
    • Problem Set 5: Pointer and Dynamic Memory Allocation
      • Pointer Exercise
    • Problem Set 6: Function and Recursion - 42
      • Recursion
    • Problem Set 7: Array
      • One Dimensional Array - 114
      • Two Dimensional Array
    • Problem Set 8: Character and String - 61
      • Character - 29
      • String - 32
    • Problem Set 9: Structure
    • Problem Set 10: File
  • Solution
    • 0: IDE, First C Program
    • 1: Data Types, IO
    • 2: Operator
      • 2.1 Assignment Operator
      • 2.2 Arithmetic Operator
    • 3: Conditional Statements
      • 3.1 Conditional Statement
      • 3.2 Relational Operator
      • 3.3 Logical Operator
      • 3.4 Max, Min Median, Sorting
      • 3.5 Divisibility
    • 4: Loop
      • 4.1.1 Loop (Repetition)
      • 4.1.2 Print Sequence
      • 4.1.3 Summation of Series
      • 4.2 Loop Exercise
    • 5: Pointer and Dynamic Memory Allocation
    • 6: Function and Recursion
      • 5.1 Types of User Defined Function
      • 5.2 Exercise - Function
    • 7: Array
    • 8: Character and String
      • 7.1 Character Basic
      • 7.2 Character Input
      • 7.3 ASCII Code
      • 7.4 Character Test
      • 7.5 Character Conversion
      • 7.6 Character Arithmetic Operation
    • 9: Structure
    • 10: File
Powered by GitBook
On this page
  • ৫.২ অনুশীলন: ইউজার ডিফাইনড ফাংশন এর ব্যবহার (১৬)
  • সমাধান
  • [5] Calculate Square [Function Prototype: int square(int)]
  • [6] Calculate the Average of three integer numbers [Function Prototype: float average(int, int, int)]
  • [7] Determine EVEN or ODD [Function Prototype: int is_even(int)]
  • [8] Determine Leap Year or Not [Function Prototype: int is_leap_year(int)]
  • [9] Find the Maximum between 2 numbers [Function Prototype: int maximum(int, int)]
  • [10] FizzBuzz - Define a function that takes an integer as an argument and print Fizz if it is divisible by 3, print Buzz if divisible by 5, and print FizzBuzz if it is divisible by both 3 and 5. [Function Prototype: void fizz_buzz(int)]
  • [11] Print the natural numbers up to N.
  • [12] Calculate the summation of natural numbers up to N.
  • [13] Calculate factorial N.
  • [14] Print the Multiplication Table of a given number.
  • [15] Print the Multiplication Table of 1 to 10.
  • [16] Print each divisor (or factor) of the given number.
  • [17] Calculate the total number of divisors of a number.
  • [18] Calculate the summation of divisors of a number.
  • [19] Define a function that takes an integer array as an argument and returns the average of all array elements.
  • [20] Define a function that takes an integer array as an argument and a search key value X. If X is found in the array, return the index; otherwise, return -1.
  1. Solution
  2. 6: Function and Recursion

5.2 Exercise - Function

৫.২ অনুশীলন: ইউজার ডিফাইনড ফাংশন এর ব্যবহার (১৬)

  1. [5] Calculate Square [Function Prototype: int square(int)]

  2. [6] Calculate the Average of three integer numbers [Function Prototype: float average(int, int, int)]

  3. [7] Determine EVEN or ODD [Function Prototype: int is_even(int)]

  4. [8] Determine Leap Year or Not [Function Prototype: int is_leap_year(int)]

  5. [9] Find the Maximum between 2 numbers [Function Prototype: int maximum(int, int)]

  6. [10] Define a function that takes an integer as an argument and print Fizz if it is divisible by 3, print Buzz if divisible by 5, and print FizzBuzz if it is divisible by both 3 and 5. [Function Prototype: void fizz_buzz(int)]

  7. [11] Print the natural numbers up to N.

  8. [12] Calculate the summation of natural numbers up to N.

  9. [13] Calculate factorial N.

  10. [14] Print the Multiplication Table of a given number.

  11. [15] Print the Multiplication Table of 1 to 10.

  12. [16] Print each divisor (or factor) of the given number.

  13. [17] Calculate the total number of divisors of a number.

  14. [18] Calculate the summation of divisors of a number.

  15. [19] Define a function that takes an integer array as an argument and returns the average of all array elements.

  16. [20] Define a function that takes an integer array as an argument and a search key value X. If X is found in the array, return the index; otherwise, return -1.

সমাধান

[5] Calculate Square [Function Prototype: int square(int)]

#include<stdio.h>

int square(int num);

int main(){
    int num = 5;
    
    printf("Square of %d: %d\n", num, square(num));
    
    return 0;
}

int square(int num){
    return num * num;
}

[6] Calculate the Average of three integer numbers [Function Prototype: float average(int, int, int)]

#include<stdio.h>

float average(int a, int b, int c);

int main(){
    int a = 4, b = 5, c = 6;
    
    printf("%f\n", average(a, b, c));
    
    return 0;
}

float average(int a, int b, int c){
    float sum, average;
    
    sum = a + b + c;
    average = sum / 3.0;
    
    return average;
}

[7] Determine EVEN or ODD [Function Prototype: int is_even(int)]

#include <stdio.h>

int is_even(int num);

int main(){
    int num = 10;
    
    if(is_even(num) == 1){
        printf("%d is even\n", num);
    }
    else{
        printf("%d is odd\n", num);
    }
    
    return 0;
}

int is_even(int num){
    if(num % 2 == 0){
        return 1;
    }
    else{
        return 0;
    }
}

[8] Determine Leap Year or Not [Function Prototype: int is_leap_year(int)]

#include <stdio.h>

int is_leap_year(int year);

int main(){
    int year = 2024;
    
    if(is_leap_year(year) == 1){
        printf("%d is a leap year\n", year);
    }
    else{
        printf("%d is not a leap year\n", year);
    }
    
    return 0;
}

int is_leap_year(int year){
    if(year % 400 == 0){
        return 1;
    }
    else if(year % 100 == 0){
        return 0;
    }
    else if(year % 4 == 0){
        return 1;
    }
    else{
        return 0;
    }
}

[9] Find the Maximum between 2 numbers [Function Prototype: int maximum(int, int)]

#include <stdio.h>

int maximum(int a, int b);

int main() {
    int num1 = 10, num2 = 15;
    
    printf("%d\n", maximum(num1, num2));
    
    return 0;
}

int maximum(int a, int b) {
    if(a > b){
        return a;
    }
    else{
        return b;
    }
}

[10] FizzBuzz - Define a function that takes an integer as an argument and print Fizz if it is divisible by 3, print Buzz if divisible by 5, and print FizzBuzz if it is divisible by both 3 and 5. [Function Prototype: void fizz_buzz(int)]

#include <stdio.h>

void fizz_buzz(int num);

int main(){
    
    fizz_buzz(6);
    fizz_buzz(10);
    fizz_buzz(15);
    fizz_buzz(16);
    
    return 0;
}

void fizz_buzz(int num){
    if(num % 3 == 0 && num % 5 == 0){
        printf("FizzBuzz\n");
    }
    else if (num % 3 == 0){
        printf("Fizz\n");
    }
    else if (num % 5 == 0){
        printf("Buzz\n");
    }
    else{
        printf("%d\n", num);
    }
}

[11] Print the natural numbers up to N.

#include<stdio.h>

int series_print(int a, int n);

int main(){
    int a = 1, n = 10;
    
    series_print(a, n);
    
    return 0;
}

int series_print(int a, int n){
    int sum = 0, i;
    for(i = a; i <= n; i++){
        printf("%d ", i);
    }
    printf("\n");
    return sum;
}

[12] Calculate the summation of natural numbers up to N.

#include<stdio.h>

int series_sum(int a, int n);

int main(){
    int a = 1, n = 10;
    printf("%d\n", series_sum(a, n));
    return 0;
}

int series_sum(int a, int n){
    int sum = 0, i;
    for(i = a; i <= n; i++){
        sum += i;
    }
    return sum;
}

[13] Calculate factorial N.

#include<stdio.h>

long long int factorial(int n);

int main(){
    int n = 10;
    
    printf("%d! = %lld\n", n, factorial(n));
    
    return 0;
}

long long int factorial(int n){
    int i;
    long long int fact = 1;
    for(i = 1; i <= n; i++){
        fact *= i;
    }
    return fact;
}

[14] Print the Multiplication Table of a given number.

#include<stdio.h>

void print_multiplication_table(int n);

int main(){
    int n;
    
    scanf("%d", &n);
    print_multiplication_table(n);
    
    return 0;
}

void print_multiplication_table(int n){
    for(int i = 1; i <= 10; i++){
        printf("%d x %d = %d\n", n, i, n * i);
    }
}
Sample Input
Sample Output

7

7 x 1 = 7

7 x 2 = 14

7 x 3 = 21

7 x 4 = 28

7 x 5 = 35

7 x 6 = 42

7 x 7 = 49

7 x 8 = 56

7 x 9 = 63

7 x 10 = 70

[15] Print the Multiplication Table of 1 to 10.

#include<stdio.h>

void print_multiplication_table(int num);

int main(){
    int i;
    for(i=1; i<=10; i++){
        print_multiplication_table(i);
    }
    
    return 0;
}

void print_multiplication_table(int num){
    printf("\nTable of %d\n", num);
    for(int i = 1; i <= 10; i++){
        printf("%d x %d = %d\n", num, i, num * i);
    }
}

[16] Print each divisor (or factor) of the given number.

#include<stdio.h>

void print_divisors(int num);

int main(){
    int num = 12;
    
    print_divisors(num);
    
    return 0;
}

void print_divisors(int num){
    for(int i = 1; i <= num; i++){
        if(num % i == 0){
            printf("%d ", i);
        }
    }
    printf("\n");
}

[17] Calculate the total number of divisors of a number.

#include<stdio.h>

int total_divisors(int num);

int main(){
    int num = 12;
    
    printf("%d\n", total_divisors(num));
    
    return 0;
}

int total_divisors(int num){
    int count = 0;
    for(int i = 1; i <= num; i++){
        if(num % i == 0){
            count++;
        }
    }
    return count;
}

[18] Calculate the summation of divisors of a number.

#include<stdio.h>

int summation_divisors(int num);

int main(){
    int num = 12;
    
    printf("%d\n", summation_divisors(num));
    
    return 0;
}

int summation_divisors(int num){
    int sum = 0;
    for(int i = 1; i <= num; i++){
        if(num % i == 0){
            sum += i;
        }
    }
    return sum;
}

[19] Define a function that takes an integer array as an argument and returns the average of all array elements.

#include<stdio.h>

float array_average(int arr[], int current_size);

int main(){
    int arr[10] = {1, 2, 3, 4, 5}, current_size = 5, total_size = 10;
    
    printf("%f\n", array_average(arr, current_size));
    
    return 0;
}

float array_average(int arr[], int current_size){
    float sum = 0, average = 0;
    for(int i = 0; i < current_size; i++){
        sum += arr[i];
    }
    average = sum / current_size;
    return average;
}

[20] Define a function that takes an integer array as an argument and a search key value X. If X is found in the array, return the index; otherwise, return -1.

#include<stdio.h>

int search_key(int arr[], int current_size, int key);

int main(){
    int arr[10] = {10, 20, 30, 40, 50}, current_size = 5, total_size = 10;
    int key = 30;
    int index = search_key(arr, current_size, key);
    
    if(index != -1){
        printf("Element %d found at index %d\n", key, index);
    }
    else{
        printf("Element %d not found in the array\n", key);
    }
    
    return 0;
}

int search_key(int arr[], int current_size, int key){
    for(int i = 0; i < current_size; i++){
        if(arr[i] == key){
            return i;  // Return the index if key value found
        }
    }
    return -1;  // Return -1 if key not found
}
Previous5.1 Types of User Defined FunctionNext7: Array

Last updated 8 months ago