Coding Practice

Write a C program to print all ASCII character with their values

Print all ASCII character with their values
Sample Output
Print all ASCII character with their values.


ASCII value of character   =  0

ASCII value of character ☺ =  1

ASCII value of character ☻ =  2

ASCII value of character ♥ =  3

ASCII value of character ♦ =  4

ASCII value of character ♣ =  5

ASCII value of character ♠ =  6

ASCII value of character  =  7

ASCII value of character =  8

ASCII value of character         =  9

ASCII value of character
 =  10

ASCII value of character ♂ =  11

ASCII value of character ♀ =  12

 =  13value of character

ASCII value of character ♫ =  14

ASCII value of character ☼ =  15

ASCII value of character ► =  16

ASCII value of character ◄ =  17

ASCII value of character ↕ =  18

ASCII value of character ‼ =  19

ASCII value of character ¶ =  20

ASCII value of character § =  21

ASCII value of character ▬ =  22

ASCII value of character ↨ =  23

ASCII value of character ↑ =  24

ASCII value of character ↓ =  25

ASCII value of character → =  26

ASCII value of character ← =  27

ASCII value of character ∟ =  28

ASCII value of character ↔ =  29

ASCII value of character ▲ =  30

ASCII value of character ▼ =  31

ASCII value of character   =  32

ASCII value of character ! =  33

ASCII value of character " =  34

ASCII value of character # =  35

ASCII value of character $ =  36

ASCII value of character % =  37

ASCII value of character & =  38

ASCII value of character ' =  39

ASCII value of character ( =  40

ASCII value of character ) =  41

ASCII value of character * =  42

ASCII value of character + =  43

ASCII value of character , =  44

ASCII value of character - =  45

ASCII value of character . =  46

ASCII value of character / =  47

ASCII value of character 0 =  48

ASCII value of character 1 =  49

ASCII value of character 2 =  50

ASCII value of character 3 =  51

ASCII value of character 4 =  52

ASCII value of character 5 =  53

ASCII value of character 6 =  54

ASCII value of character 7 =  55

ASCII value of character 8 =  56

ASCII value of character 9 =  57

ASCII value of character : =  58

ASCII value of character ; =  59

ASCII value of character < =  60

ASCII value of character = =  61

ASCII value of character > =  62

ASCII value of character ? =  63

ASCII value of character @ =  64

ASCII value of character A =  65

ASCII value of character B =  66

ASCII value of character C =  67

ASCII value of character D =  68

ASCII value of character E =  69

ASCII value of character F =  70

ASCII value of character G =  71

ASCII value of character H =  72

ASCII value of character I =  73

ASCII value of character J =  74

ASCII value of character K =  75

ASCII value of character L =  76

ASCII value of character M =  77

ASCII value of character N =  78

ASCII value of character O =  79

ASCII value of character P =  80

ASCII value of character Q =  81

ASCII value of character R =  82

ASCII value of character S =  83

ASCII value of character T =  84

ASCII value of character U =  85

ASCII value of character V =  86

ASCII value of character W =  87

ASCII value of character X =  88

ASCII value of character Y =  89

ASCII value of character Z =  90

ASCII value of character [ =  91

ASCII value of character \ =  92

ASCII value of character ] =  93

ASCII value of character ^ =  94

ASCII value of character _ =  95

ASCII value of character ` =  96

ASCII value of character a =  97

ASCII value of character b =  98

ASCII value of character c =  99

ASCII value of character d =  100

ASCII value of character e =  101

ASCII value of character f =  102

ASCII value of character g =  103

ASCII value of character h =  104

ASCII value of character i =  105

ASCII value of character j =  106

ASCII value of character k =  107

ASCII value of character l =  108

ASCII value of character m =  109

ASCII value of character n =  110

ASCII value of character o =  111

ASCII value of character p =  112

ASCII value of character q =  113

ASCII value of character r =  114

ASCII value of character s =  115

ASCII value of character t =  116

ASCII value of character u =  117

ASCII value of character v =  118

ASCII value of character w =  119

ASCII value of character x =  120

ASCII value of character y =  121

ASCII value of character z =  122

ASCII value of character { =  123

ASCII value of character | =  124

ASCII value of character } =  125

ASCII value of character ~ =  126

ASCII value of character ⌂ =  127

ASCII value of character Ç =  128

ASCII value of character ü =  129

ASCII value of character é =  130

ASCII value of character â =  131

ASCII value of character ä =  132

ASCII value of character à =  133

ASCII value of character å =  134

ASCII value of character ç =  135

ASCII value of character ê =  136

ASCII value of character ë =  137

ASCII value of character è =  138

ASCII value of character ï =  139

ASCII value of character î =  140

ASCII value of character ì =  141

ASCII value of character Ä =  142

ASCII value of character Å =  143

ASCII value of character É =  144

ASCII value of character æ =  145

ASCII value of character Æ =  146

ASCII value of character ô =  147

ASCII value of character ö =  148

ASCII value of character ò =  149

ASCII value of character û =  150

ASCII value of character ù =  151

ASCII value of character ÿ =  152

ASCII value of character Ö =  153

ASCII value of character Ü =  154

ASCII value of character ¢ =  155

ASCII value of character £ =  156

ASCII value of character ¥ =  157

ASCII value of character ₧ =  158

ASCII value of character ƒ =  159

ASCII value of character á =  160

ASCII value of character í =  161

ASCII value of character ó =  162

ASCII value of character ú =  163

ASCII value of character ñ =  164

ASCII value of character Ñ =  165

ASCII value of character ª =  166

ASCII value of character º =  167

ASCII value of character ¿ =  168

ASCII value of character ⌐ =  169

ASCII value of character ¬ =  170

ASCII value of character ½ =  171

ASCII value of character ¼ =  172

ASCII value of character ¡ =  173

ASCII value of character « =  174

ASCII value of character » =  175

ASCII value of character ░ =  176

ASCII value of character ▒ =  177

ASCII value of character ▓ =  178

ASCII value of character │ =  179

ASCII value of character ┤ =  180

ASCII value of character ╡ =  181

ASCII value of character ╢ =  182

ASCII value of character ╖ =  183

ASCII value of character ╕ =  184

ASCII value of character ╣ =  185

ASCII value of character ║ =  186

ASCII value of character ╗ =  187

ASCII value of character ╝ =  188

ASCII value of character ╜ =  189

ASCII value of character ╛ =  190

ASCII value of character ┐ =  191

ASCII value of character └ =  192

ASCII value of character ┴ =  193

ASCII value of character ┬ =  194

ASCII value of character ├ =  195

ASCII value of character ─ =  196

ASCII value of character ┼ =  197

ASCII value of character ╞ =  198

ASCII value of character ╟ =  199

ASCII value of character ╚ =  200

ASCII value of character ╔ =  201

ASCII value of character ╩ =  202

ASCII value of character ╦ =  203

ASCII value of character ╠ =  204

ASCII value of character ═ =  205

ASCII value of character ╬ =  206

ASCII value of character ╧ =  207

ASCII value of character ╨ =  208

ASCII value of character ╤ =  209

ASCII value of character ╥ =  210

ASCII value of character ╙ =  211

ASCII value of character ╘ =  212

ASCII value of character ╒ =  213

ASCII value of character ╓ =  214

ASCII value of character ╫ =  215

ASCII value of character ╪ =  216

ASCII value of character ┘ =  217

ASCII value of character ┌ =  218

ASCII value of character █ =  219

ASCII value of character ▄ =  220

ASCII value of character ▌ =  221

ASCII value of character ▐ =  222

ASCII value of character ▀ =  223

ASCII value of character α =  224

ASCII value of character ß =  225

ASCII value of character Γ =  226

ASCII value of character π =  227

ASCII value of character Σ =  228

ASCII value of character σ =  229

ASCII value of character µ =  230

ASCII value of character τ =  231

ASCII value of character Φ =  232

ASCII value of character Θ =  233

ASCII value of character Ω =  234

ASCII value of character δ =  235

ASCII value of character ∞ =  236

ASCII value of character φ =  237

ASCII value of character ε =  238

ASCII value of character ∩ =  239

ASCII value of character ≡ =  240

ASCII value of character ± =  241

ASCII value of character ≥ =  242

ASCII value of character ≤ =  243

ASCII value of character ⌠ =  244

ASCII value of character ⌡ =  245

ASCII value of character ÷ =  246

ASCII value of character ≈ =  247

ASCII value of character ° =  248

ASCII value of character ∙ =  249

ASCII value of character · =  250

ASCII value of character √ =  251

ASCII value of character ⁿ =  252

ASCII value of character ² =  253

ASCII value of character ■ =  254

ASCII value of character   =  255
Source Code
#include<stdio.h>

int main()
{
    int n;
    printf("Print all ASCII character with their values.\n\n");

    for(n = 0; n <= 255; n++)
    {
        printf("\nASCII value of character %c =  %d\n", n, n);
    }
    return 0;
}
Sample Output
Print all ASCII character with their values.


ASCII value of character   =  0

ASCII value of character ☺ =  1

ASCII value of character ☻ =  2

ASCII value of character ♥ =  3

ASCII value of character ♦ =  4

ASCII value of character ♣ =  5

ASCII value of character ♠ =  6

ASCII value of character  =  7

ASCII value of character =  8

ASCII value of character         =  9

ASCII value of character
 =  10

ASCII value of character ♂ =  11

ASCII value of character ♀ =  12

 =  13value of character

ASCII value of character ♫ =  14

ASCII value of character ☼ =  15

ASCII value of character ► =  16

ASCII value of character ◄ =  17

ASCII value of character ↕ =  18

ASCII value of character ‼ =  19

ASCII value of character ¶ =  20

ASCII value of character § =  21

ASCII value of character ▬ =  22

ASCII value of character ↨ =  23

ASCII value of character ↑ =  24

ASCII value of character ↓ =  25

ASCII value of character → =  26

ASCII value of character ← =  27

ASCII value of character ∟ =  28

ASCII value of character ↔ =  29

ASCII value of character ▲ =  30

ASCII value of character ▼ =  31

ASCII value of character   =  32

ASCII value of character ! =  33

ASCII value of character " =  34

ASCII value of character # =  35

ASCII value of character $ =  36

ASCII value of character % =  37

ASCII value of character & =  38

ASCII value of character ' =  39

ASCII value of character ( =  40

ASCII value of character ) =  41

ASCII value of character * =  42

ASCII value of character + =  43

ASCII value of character , =  44

ASCII value of character - =  45

ASCII value of character . =  46

ASCII value of character / =  47

ASCII value of character 0 =  48

ASCII value of character 1 =  49

ASCII value of character 2 =  50

ASCII value of character 3 =  51

ASCII value of character 4 =  52

ASCII value of character 5 =  53

ASCII value of character 6 =  54

ASCII value of character 7 =  55

ASCII value of character 8 =  56

ASCII value of character 9 =  57

ASCII value of character : =  58

ASCII value of character ; =  59

ASCII value of character < =  60

ASCII value of character = =  61

ASCII value of character > =  62

ASCII value of character ? =  63

ASCII value of character @ =  64

ASCII value of character A =  65

ASCII value of character B =  66

ASCII value of character C =  67

ASCII value of character D =  68

ASCII value of character E =  69

ASCII value of character F =  70

ASCII value of character G =  71

ASCII value of character H =  72

ASCII value of character I =  73

ASCII value of character J =  74

ASCII value of character K =  75

ASCII value of character L =  76

ASCII value of character M =  77

ASCII value of character N =  78

ASCII value of character O =  79

ASCII value of character P =  80

ASCII value of character Q =  81

ASCII value of character R =  82

ASCII value of character S =  83

ASCII value of character T =  84

ASCII value of character U =  85

ASCII value of character V =  86

ASCII value of character W =  87

ASCII value of character X =  88

ASCII value of character Y =  89

ASCII value of character Z =  90

ASCII value of character [ =  91

ASCII value of character \ =  92

ASCII value of character ] =  93

ASCII value of character ^ =  94

ASCII value of character _ =  95

ASCII value of character ` =  96

ASCII value of character a =  97

ASCII value of character b =  98

ASCII value of character c =  99

ASCII value of character d =  100

ASCII value of character e =  101

ASCII value of character f =  102

ASCII value of character g =  103

ASCII value of character h =  104

ASCII value of character i =  105

ASCII value of character j =  106

ASCII value of character k =  107

ASCII value of character l =  108

ASCII value of character m =  109

ASCII value of character n =  110

ASCII value of character o =  111

ASCII value of character p =  112

ASCII value of character q =  113

ASCII value of character r =  114

ASCII value of character s =  115

ASCII value of character t =  116

ASCII value of character u =  117

ASCII value of character v =  118

ASCII value of character w =  119

ASCII value of character x =  120

ASCII value of character y =  121

ASCII value of character z =  122

ASCII value of character { =  123

ASCII value of character | =  124

ASCII value of character } =  125

ASCII value of character ~ =  126

ASCII value of character ⌂ =  127

ASCII value of character Ç =  128

ASCII value of character ü =  129

ASCII value of character é =  130

ASCII value of character â =  131

ASCII value of character ä =  132

ASCII value of character à =  133

ASCII value of character å =  134

ASCII value of character ç =  135

ASCII value of character ê =  136

ASCII value of character ë =  137

ASCII value of character è =  138

ASCII value of character ï =  139

ASCII value of character î =  140

ASCII value of character ì =  141

ASCII value of character Ä =  142

ASCII value of character Å =  143

ASCII value of character É =  144

ASCII value of character æ =  145

ASCII value of character Æ =  146

ASCII value of character ô =  147

ASCII value of character ö =  148

ASCII value of character ò =  149

ASCII value of character û =  150

ASCII value of character ù =  151

ASCII value of character ÿ =  152

ASCII value of character Ö =  153

ASCII value of character Ü =  154

ASCII value of character ¢ =  155

ASCII value of character £ =  156

ASCII value of character ¥ =  157

ASCII value of character ₧ =  158

ASCII value of character ƒ =  159

ASCII value of character á =  160

ASCII value of character í =  161

ASCII value of character ó =  162

ASCII value of character ú =  163

ASCII value of character ñ =  164

ASCII value of character Ñ =  165

ASCII value of character ª =  166

ASCII value of character º =  167

ASCII value of character ¿ =  168

ASCII value of character ⌐ =  169

ASCII value of character ¬ =  170

ASCII value of character ½ =  171

ASCII value of character ¼ =  172

ASCII value of character ¡ =  173

ASCII value of character « =  174

ASCII value of character » =  175

ASCII value of character ░ =  176

ASCII value of character ▒ =  177

ASCII value of character ▓ =  178

ASCII value of character │ =  179

ASCII value of character ┤ =  180

ASCII value of character ╡ =  181

ASCII value of character ╢ =  182

ASCII value of character ╖ =  183

ASCII value of character ╕ =  184

ASCII value of character ╣ =  185

ASCII value of character ║ =  186

ASCII value of character ╗ =  187

ASCII value of character ╝ =  188

ASCII value of character ╜ =  189

ASCII value of character ╛ =  190

ASCII value of character ┐ =  191

ASCII value of character └ =  192

ASCII value of character ┴ =  193

ASCII value of character ┬ =  194

ASCII value of character ├ =  195

ASCII value of character ─ =  196

ASCII value of character ┼ =  197

ASCII value of character ╞ =  198

ASCII value of character ╟ =  199

ASCII value of character ╚ =  200

ASCII value of character ╔ =  201

ASCII value of character ╩ =  202

ASCII value of character ╦ =  203

ASCII value of character ╠ =  204

ASCII value of character ═ =  205

ASCII value of character ╬ =  206

ASCII value of character ╧ =  207

ASCII value of character ╨ =  208

ASCII value of character ╤ =  209

ASCII value of character ╥ =  210

ASCII value of character ╙ =  211

ASCII value of character ╘ =  212

ASCII value of character ╒ =  213

ASCII value of character ╓ =  214

ASCII value of character ╫ =  215

ASCII value of character ╪ =  216

ASCII value of character ┘ =  217

ASCII value of character ┌ =  218

ASCII value of character █ =  219

ASCII value of character ▄ =  220

ASCII value of character ▌ =  221

ASCII value of character ▐ =  222

ASCII value of character ▀ =  223

ASCII value of character α =  224

ASCII value of character ß =  225

ASCII value of character Γ =  226

ASCII value of character π =  227

ASCII value of character Σ =  228

ASCII value of character σ =  229

ASCII value of character µ =  230

ASCII value of character τ =  231

ASCII value of character Φ =  232

ASCII value of character Θ =  233

ASCII value of character Ω =  234

ASCII value of character δ =  235

ASCII value of character ∞ =  236

ASCII value of character φ =  237

ASCII value of character ε =  238

ASCII value of character ∩ =  239

ASCII value of character ≡ =  240

ASCII value of character ± =  241

ASCII value of character ≥ =  242

ASCII value of character ≤ =  243

ASCII value of character ⌠ =  244

ASCII value of character ⌡ =  245

ASCII value of character ÷ =  246

ASCII value of character ≈ =  247

ASCII value of character ° =  248

ASCII value of character ∙ =  249

ASCII value of character · =  250

ASCII value of character √ =  251

ASCII value of character ⁿ =  252

ASCII value of character ² =  253

ASCII value of character ■ =  254

ASCII value of character   =  255

Write a C program to enter a number and print it in words

Enter a number and print it in words
Variable declaration
  • 'number' is input number.
  • 'digit' is every single digit. i.e number = 123, digit = 1, 2 and 3.
  • 'revNumber' is numbers reverse form.
  • 'lastZero' means if the numbers last digit is 0 (ZERO).
Sample Output
Enter a number and print it in words.

Enter number: 2580
Words: Two Five Eight Zero
Source Code
#include<stdio.h>

int main()
{
    int number, digit, revNumber = 0, lastZero = 0;

    /**
     * Variable declaration
     * -----------------------
     * 'number' is input number.
     * 'digit' is every single digit. i.e number = 123, digit = 1, 2 and 3.
     * 'revNumber' is numbers reverse form.
     * 'lastZero' means if the numbers last digit is 0 (ZERO).
    */
    
    printf("Enter a number and print it in words.\n\n");
    printf("Enter a number: ");
    scanf("%d", &number);

    if (number % 10 == 0)
    {
        lastZero = 1;
    }
    while (number >= 10)
    {
        digit = number % 10;
        revNumber = (revNumber * 10) + digit;
        number = (number / 10);
    }
    revNumber = (revNumber * 10) + number;
    number = revNumber;

    printf("Words: ");
    while (number != 0)
    {
        digit = number % 10;
        number = number / 10;
        switch (digit)
        {
        case 0:
            printf("Zero ");
            break;
        case 1:
            printf("One ");
            break;
        case 2:
            printf("Two ");
            break;
        case 3:
            printf("Three ");
            break;
        case 4:
            printf("Four ");
            break;
        case 5:
            printf("Five ");
            break;
        case 6:
            printf("Six ");
            break;
        case 7:
            printf("Seven ");
            break;
        case 8:
            printf("Eight ");
            break;
        case 9:
            printf("Nine ");
            break;
        }
    }
    if(lastZero == 1)
    {
        printf("Zero");
    }
    printf("\n\n");

    return 0;
}
Sample Output
Enter a number and print it in words.

Enter number: 19224
Words: One Nine Two Two Four

Write a C program to calculate factorial of a number

Calculate factorial of a number
Sample Output
Calculate factorial of a number.

Number: 5

Factorial of 5 = 120
Source Code
#include<stdio.h>

int main()
{
    int n, num, fact = 1;

    printf("Calculate factorial of a number.\n\nNumber: ");
    scanf("%d", &num);

    for(n = 1; n <= num; n++)
    {
        fact = fact * n;
    }
    printf("\nFactorial of %d = %d\n", num, fact);

    return 0;
}
Sample Output
Calculate factorial of a number.

Number: 4

Factorial of 4 = 24

Write a C program to find power of a number using for loop

Find power of a number using for loop
Sample Output
Find power of a number using for loop.

Number: 2
Power: 4

2 Power 4 = 16
Source Code
#include<stdio.h>

int main()
{
    int i, num, power, result = 1;

    printf("Find power of a number using for loop.\n\nNumber: ");
    scanf("%d", &num);

    printf("Power: ");
    scanf("%d", &power);

    for(i = 0; i < power; i++)
    {
        result = result * num;
    }
    printf("\n%d Power %d = %d\n", num, i, result);
    
    return 0;
}
Sample Output
Find power of a number using for loop.

Number: 5
Power: 0

5 Power 0 = 1

Write a C program to find first and last digit of a number

Find first and last digit of a number
Sample Output
Find first and last digit of a number.

Enter number: 19224

First Digit: 1, Last Digit: 4
Source Code
#include<stdio.h>

int main()
{
    int n, num, firstDigit, lastDigit;

    printf("Find first and last digit of a number.\n");
    printf("\nEnter number: ");
    scanf("%d", &num);

    firstDigit = num;
    lastDigit = num % 10;

    for(n = 1; firstDigit  >= 10; n++)
    {
        firstDigit = firstDigit / 10;
    }
    printf("\nFirst Digit: %d, Last Digit: %d\n", firstDigit, lastDigit);

    return 0;
}
Sample Output
Find first and last digit of a number.

Enter number: 2021

First Digit: 2, Last Digit: 1

Mini-Max Sum in C | HackerRank

Mini-Max Sum in C

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Example

arr = [1, 3, 5, 7, 9]

The minimum sum is 1 + 3 + 5 + 7 = 16 and the maximum sum is 3 + 5 + 7 + 9 = 24. The function prints

16 24

Function Description

Complete the miniMaxSum function in the editor below.

miniMaxSum has the following parameter(s):

  • arr: an array of 5 integers

Print

Print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements.

Input Format

A single line of five space-separated integers.

Constraints

1 <= arr[i] <= 109

Output Format

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14

Explanation

The numbers are 1, 2, 3, 4, and 5. Calculate the following sums using four of the five integers:

  1. Sum everything except 1, the sum is 2 + 3 + 4 + 5 = 14.
  2. Sum everything except 2, the sum is 1 + 3 + 4 + 5 = 13.
  3. Sum everything except 3, the sum is 1 + 2 + 4 + 5 = 12.
  4. Sum everything except 4, the sum is 1 + 2 + 3 + 5 = 11.
  5. Sum everything except 5, the sum is 1 + 2 + 3 + 4 = 10.

Hints: Beware of integer overflow! Use 64-bit Integer.

Need help to get started? Try the Solve Me First problem

Sample Output
1 2 3 4 5
10 14
C-Source Code
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();
char** split_string(char*);

void miniMaxSum(int arr_count, int* arr) {
    
    int i, j;
    long sum[5] = {0};
 
        for (i = 0; i < arr_count; i++) {
 
            for (j = 0; j < arr_count; j++) {
 
                sum[i] = sum[i] + arr[j];
            }
            sum[i] = sum[i] - arr[i];
        }
        long min = sum[0], max = sum[0];
 
        for (i = 0; i < arr_count; i++) {
 
            if (sum[i] > max) {
 
                max = sum[i];
            }
            if (sum[i] < min) {
 
                min = sum[i];
            }
        }
        printf("%ld %ld", min, max);
}

int main()
{
    char** arr_temp = split_string(readline());

    int* arr = malloc(5 * sizeof(int));

    for (int i = 0; i < 5; i++) {
        char* arr_item_endptr;
        char* arr_item_str = *(arr_temp + i);
        int arr_item = strtol(arr_item_str, &arr_item_endptr, 10);

        if (arr_item_endptr == arr_item_str || *arr_item_endptr != '\0') { exit(EXIT_FAILURE); }

        *(arr + i) = arr_item;
    }

    int arr_count = 5;

    miniMaxSum(arr_count, arr);

    return 0;
}

char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);

    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) { break; }

        data_length += strlen(cursor);

        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }

        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);

        if (!data) { break; }

        alloc_length = new_length;
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    }

    data = realloc(data, data_length);

    return data;
}

char** split_string(char* str) {
    char** splits = NULL;
    char* token = strtok(str, " ");

    int spaces = 0;

    while (token) {
        splits = realloc(splits, sizeof(char*) * ++spaces);
        if (!splits) {
            return splits;
        }

        splits[spaces - 1] = token;

        token = strtok(NULL, " ");
    }

    return splits;
}
Sample Output
1 2 3 4 5
10 14

Mini-Max Sum in Java | HackerRank

Mini-Max Sum in Java

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Example

arr = [1, 3, 5, 7, 9]

The minimum sum is 1 + 3 + 5 + 7 = 16 and the maximum sum is 3 + 5 + 7 + 9 = 24. The function prints

16 24

Function Description

Complete the miniMaxSum function in the editor below.

miniMaxSum has the following parameter(s):

  • arr: an array of 5 integers

Print

Print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements.

Input Format

A single line of five space-separated integers.

Constraints

1 <= arr[i] <= 109

Output Format

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14

Explanation

The numbers are 1, 2, 3, 4, and 5. Calculate the following sums using four of the five integers:

  1. Sum everything except 1, the sum is 2 + 3 + 4 + 5 = 14.
  2. Sum everything except 2, the sum is 1 + 3 + 4 + 5 = 13.
  3. Sum everything except 3, the sum is 1 + 2 + 4 + 5 = 12.
  4. Sum everything except 4, the sum is 1 + 2 + 3 + 5 = 11.
  5. Sum everything except 5, the sum is 1 + 2 + 3 + 4 = 10.

Hints: Beware of integer overflow! Use 64-bit Integer.

Need help to get started? Try the Solve Me First problem

Sample Output
1 2 3 4 5
10 14
Java-Source Code
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    static void miniMaxSum(int[] arr) {
        int i, j;
        long sum[] = new long[5];

        for (i = 0; i < arr.length; i++) {

            for (j = 0; j < arr.length; j++) {

                sum[i] = sum[i] + arr[j];
            }
            sum[i] = sum[i] - arr[i];
        }
        long min = sum[0], max = sum[0];

        for (i = 0; i < arr.length; i++) {

            if (sum[i] > max) {

                max = sum[i];
            }
            if (sum[i] < min) {

                min = sum[i];
            }
        }
        System.out.println(min + " " + max);
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int[] arr = new int[5];

        String[] arrItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < 5; i++) {
            int arrItem = Integer.parseInt(arrItems[i]);
            arr[i] = arrItem;
        }

        miniMaxSum(arr);

        scanner.close();
    }
}
Sample Output
1 2 3 4 5
10 14

SQL Questions and Answers for Practice

1. Consider a database used to record the marks that students get in different exams of different course offerings (sections).
Draw an E-R diagram that models exams as entities, and uses a ternary relationship, for the database. Define the primary keys and foreign keys in the ERD.

Answer:

  • E-R diagram for marks database

E-r Diagram

  • Primary keys and foreign keys: Primary and foreign keys are the most basic components on which relational database theory is based. Primary keys enforce entity integrity by uniquely identifying entity instances. Foreign keys enforce referential integrity by completing an association between two entities.

2. Consider the bank database of given figure. Prepare an expression in the relational algebra for each of the following queries:
    branch(branch name, branch city, assets)
    customer (customer name, customer street, customer city)
    loan (loan number, branch name, amount)
    borrower (customer name, loan number)
    account (account number, branch name, balance)
    depositor (customer name, account number)

a. Find all loan numbers with a loan value greater than $10,000.
Answer: Relational algebra:
πloan_numberamount > 10000 (loan))
Query
SELECT loan_number, amount
FROM loan
WHERE amount > 10000;
b. Find the names of all depositors who have an account with a value greater than $6,000.
Answer: Relational algebra:
πcustomer_namebalance > 6000 (depositor ⋈ account))
Query
SELECT depositor.customer_name, account.balence
FROM depositor, account
WHERE account.balence > 6000;
c. Find the names of all depositors who have an account with a value greater than $6,000 at the “Uptown” branch.
Answer: Relational algebra:
πcustomer_namebranch_name = “Uptown” (depositor ⋈ account))
Query
SELECT depositor.customer_name, account.branch_name
FROM depositor, account
WHERE branch_name = "Uptown";

Java Loops II | HackerRank

Java Loops II

We use the integers a, b, and n to create the following series:

(a + 20.b), (a + 20.b + 21.b),..., (a + 20.b + 21.b +...+ 2n-1.b)

You are given q queries in the form of a, b, and n. For each query, print the series corresponding to the given a, b, and n values as a single line of n space-separated integers.

Input Format

The first line contains an integer, q, denoting the number of queries.

Each line i of the q subsequent lines contains three space-separated integers describing the respective ai, bi, and ni values for that query.

Constraints

  • 0 <= q <= 500
  • 0 <= a, b <= 50
  • 1 <= n <= 15

Output Format

For each query, print the corresponding series on a new line. Each series must be printed in order as a single line of n space-separated integers.

Sample Input

2
0 2 10
5 3 5

Sample Output

2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98

Explanation

We have two queries:

1. We use a = 0, b = 2, and c = 10 to produce some series ss1 and sn-1:

  • s0 =  0 + 1.2 = 2
  • s1 = 0 + 1.2 + 2.2 = 6
  • s2 = 0 + 1.2 + 2.2 + 4.2 = 14

... and so on.

Once we hit n = 10, we print the first ten terms as a single line of space-separated integers.

2. We use a = 5, b = 3, and n = 5 to produce some series ss1 and sn-1:

  • s0 =  5 + 1.3 = 8
  • s1 = 5 + 1.3 + 2.3 = 14
  • s2 = 5 + 1.3 + 2.3 + 4.3 = 26
  • s3 = 5 + 1.3 + 2.3 + 4.3 + 8.3 = 50
  • s4 = 5 + 1.3 + 2.3 + 4.3 + 8.3 + 16.3 = 98

We then print each element of our series as a single line of space-separated values.

Sample Output
2
0 2 10
5 3 5

2 6 14 30 62 126 254 510 1022 2046 
8 14 26 50 98
Source Code
import java.util.*;
import java.io.*;

public class Solution {

    public static void main(String[] argh) {

        Scanner in = new Scanner(System.in);

        int i, j, s, s2, x, t = in.nextInt();

        for(i = 0; i < t; i++) {

            int a = in.nextInt();
            int b = in.nextInt();
            int n = in.nextInt();

            s2 = 0;
            x = 1;

            for (j = 0; j < n; j++) {
                s = (x * b) + s2;
                System.out.print(+s+a+ " ");
                s2 = s;
                x = x * 2;
            }
            System.out.println();
        }
        in.close();
    }
}
Sample Output
2
0 2 10
5 3 5

2 6 14 30 62 126 254 510 1022 2046 
8 14 26 50 98


Java Loops I | HackerRank

Java Loops I

Objective

In this challenge, we're going to use loops to help us do some simple math.

Task

Given an integer, N, print its first 10 multiples. Each multiple N * i (where 1 <= i <= 10) should be printed on a new line in the form: N x i = result.

Input Format

A single integer, N.

Constraints

  • 2 <= N <= 20

Output Format

Print 10 lines of output; each line i (where 1 <= i <= 10) contains the result of N * i in the form:

N x i = result.

Sample Input

2

Sample Output

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Sample Output
2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Java-Source Code
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int i, N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for(i = 1; i <= 10; i++){
            //System.out.printf("%d x %d = %d\n", N, i, N*i);
            System.out.println(+N+" x "+i+" = "+N*i);
        }
        scanner.close();
    }
}
Sample Output
2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20


Java Output Formatting | HackerRank

Java Output Formatting

Java's System.out.printf function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using printf.

To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.

Input Format

Every line of input will contain a String followed by an integer.

Each String will have a maximum of 10 alphabetic characters, and each integer will be in the inclusive range from 0 to 999.

Output Format

In each line of output there should be two columns:
The first column contains the String and is left justified using exactly 15 characters.
The second column contains the integer, expressed in exactly 3 digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes.

Sample Input

java 100
cpp 65
python 50

Sample Output

================================
java           100
cpp            065
python         050
================================

Explanation

Each String is left-justified with trailing whitespace through the first 15 characters. The leading digit of the integer is the 16th character, and each integer that was less than 3 digits now has leading zeroes.

Sample Output
java 100
cpp 65
python 50
================================
java           100 
cpp            065 
python         050 
================================
Java-Source Code
import java.util.Scanner;

public class Solution {
    
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("================================");
        for(int i=0;i<3;i++){
            String s1=sc.next();
            int x=sc.nextInt();
            System.out.printf("%-13s  %03d\n", s1, x);
        }
        System.out.println("================================");
    }
}
Sample Output
java 100
cpp 65
python 50
================================
java           100 
cpp            065 
python         050 
================================


Java Stdin and Stdout II | HackerRank Solution

Java Stdin and Stdout II

In this challenge, you must read an integer, a double, and a String from stdin, then print the values according to the instructions in the Output Format section below. To make the problem a little easier, a portion of the code is provided for you in the editor.

Note: We recommend completing Java Stdin and Stdout I before attempting this challenge.

Input Format

There are three lines of input:

  1. The first line contains an integer.
  2. The second line contains a double.
  3. The third line contains a String.

Output Format

There are three lines of output:

  1. On the first line, print String: followed by the unaltered String read from stdin.
  2. On the second line, print Double: followed by the unaltered double read from stdin.
  3. On the third line, print Int: followed by the unaltered integer read from stdin.

To make the problem easier, a portion of the code is already provided in the editor.

Note: If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty).

Sample Input

42
3.1415
Welcome to HackerRank's Java tutorials!

Sample Output

String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42
Sample Output
42
3.1415
Welcome to HackerRank's Java tutorials!
String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42
Source Code
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int i = scan.nextInt();
        double d = scan.nextDouble();
        scan.nextLine();
        String s = scan.nextLine();

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}
Sample Output
42
3.1415
Welcome to HackerRank's Java tutorials!
String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42


Java Stdin and Stdout I | HackerRank Solution

Java Stdin and Stdout I

Most HackerRank challenges require you to read input from stdin (standard input) and write output to stdout (standard output).

One popular way to read input from stdin is by using the Scanner class and specifying the Input Stream as System.in. For example:

Scanner scanner = new Scanner(System.in);
String myString = scanner.next();
int myInt = scanner.nextInt();
scanner.close();

System.out.println("myString is: " + myString);
System.out.println("myInt is: " + myInt);

The code above creates a Scanner object named scanner and uses it to read a String and an int. It then closes the Scanner object because there is no more input to read, and prints to stdout using System.out.println(String). So, if our input is:

Hi 5

Our code will print:

myString is: Hi
myInt is: 5

Alternatively, you can use the BufferedReader class.

Task

In this challenge, you must read  integers from stdin and then print them to stdout. Each integer must be printed on a new line. To make the problem a little easier, a portion of the code is provided for you in the editor below.

Input Format

There are 3 lines of input, and each line contains a single integer.

Sample Input

42
100
125

Sample Output

42
100
125
Sample Output
42
100
125

42
100
125
Java-Source Code
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}
Sample Output
42
100
125

42
100
125


Java If-Else | HackerRank

Java If-Else

In this challenge, we test your knowledge of using if-else conditional statements to automate decision-making processes. An if-else statement has the following logical flow:

Wikipedia if-else flow chart

Source: Wikipedia

Task

Given an integer, n, perform the following conditional actions:

  • If n is odd, print Weird
  • If n is even and in the inclusive range of 2 to 5, print Not Weird
  • If n is even and in the inclusive range of 6 to 20, print Weird
  • If n is even and greater than 20, print Not Weird

Complete the stub code provided in your editor to print whether or not  is weird.

Input Format

A single line containing a positive integer, n.

Constraints

  • 1 <= n <= 100

Output Format

Print Weird if the number is weird; otherwise, print Not Weird.

Sample Input 0

3

Sample Output 0

Weird

Sample Input 1

24

Sample Output 1

Not Weird

Explanation

Sample Case 0: n = 3

n is odd and odd numbers are weird, so we print Weird.

Sample Case 1: n = 24

n > 20 and  is even, so it isn't weird. Thus, we print Not Weird.

Sample Output
3
Weird


...Program finished with exit code 0
Press ENTER to exit console.
Java-Source Code
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        if(N%2 == 0){
            if(N >= 2 && N <= 5){
                System.out.println("Not Weird");
            }
            else if(N >= 6 && N <= 20){
                System.out.println("Weird");
            }
            else if(N >= 20){
                System.out.println("Not Weird");
            }
        }
        else{
            System.out.println("Weird");
        }

        scanner.close();
    }
}
Sample Output
24
Not Weird


...Program finished with exit code 0
Press ENTER to exit console.


Welcome to Java! | HackerRank

Welcome to Java!

Welcome to the world of Java! In this challenge, we practice printing to stdout.

The code stubs in your editor declare a Solution class and a main method. Complete the main method by copying the two lines of code below and pasting them inside the body of your main method.

System.out.println("Hello, World.");

System.out.println("Hello, Java.");

Input Format

There is no input for this challenge.

Output Format

You must print two lines of output:

  1. Print Hello, World. on the first line.
  2. Print Hello, Java. on the second line.

Sample Output

Hello, World.

Hello, Java.

Sample Output
Hello, World.
Hello, Java.


...Program finished with exit code 0
Press ENTER to exit console.
Java-Source Code
public class Solution {

    public static void main(String[] args) {
        System.out.println("Hello, World.");
        System.out.println("Hello, Java.");
    }
}
Sample Output
Hello, World.
Hello, Java.


...Program finished with exit code 0
Press ENTER to exit console.


Write a program in C to print all alphabets using pointer

Print all alphabets using pointer
Sample Output
Print all alphabets from a to z using pointer.
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

Process returned 0 (0x0)   execution time : 0.062 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    char ch = 'a';
    char *ch_pointer;

    ch_pointer = &ch;

    printf("Print all alphabets from a to z using pointer.\n");
    while(ch <= 'z')
    {
        printf("%c\n", *ch_pointer);
        ch++;
    }
    return 0;
}
Sample Output
Print all alphabets from a to z using pointer.
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

Process returned 0 (0x0)   execution time : 0.062 s
Press any key to continue.

Arrays: Left Rotation | Java HackerRank

Arrays: Left Rotation

A left rotation operation on an array shifts each of the array's elements  unit to the left. For example, if  left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2]. Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.

Given an array a of n integers and a number, d, perform d left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.

Function Description

Complete the function rotLeft in the editor below.

rotLeft has the following parameter(s):

  • int a[n]: the array to rotate
  • int d: the number of rotations

Returns

  • int a'[n]: the rotated array

Input Format

The first line contains two space-separated integers n and d, the size of a and the number of left rotations.

The second line contains n space-separated integers, each an a[i].

Constraints

  • 1 <= n <= 105
  • 1 <= d <= n
  • 1 <= a[i] <= 106

Sample Input

5 4

1 2 3 4 5

Sample Output

5 1 2 3 4

Explanation

When we perform d = 4 left rotations, the array undergoes the following sequence of changes:

[1,2,3,4,5] -> [2,3,4,5,1] -> [3,4,5,1,2] -> [4,5,1,2,3] -> [5,1,2,3,4]

Sample Output
5 4
1 2 3 4 5
5 1 2 3 4


...Program finished with exit code 0
Press ENTER to exit console.
Source Code
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    static int[] rotLeft(int[] a, int d, int n) {

        int i, j, k, b[] = new int[n], c[] = new int[n];

        for(i = d, j = 0, k = 0; j < n; j++){

            b[j] = a[j];

            if(j > d-1){
                a[k] = a[i];
                k++;
                i++;
            }
            else{
                c[j] = b[j];
            }
        }
        for(i = n-d, j = 0; i < n; i++, j++){
            
            a[i] = c[j];
        }
        
        return a;

    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] nd = scanner.nextLine().split(" ");

        int n = Integer.parseInt(nd[0]);

        int d = Integer.parseInt(nd[1]);

        int[] a = new int[n];

        String[] aItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int aItem = Integer.parseInt(aItems[i]);
            a[i] = aItem;
        }

        int[] result = rotLeft(a, d, n);

        for (int i = 0; i < result.length; i++) {
            bufferedWriter.write(String.valueOf(result[i]));

            if (i != result.length - 1) {
                bufferedWriter.write(" ");
            }
        }

        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}
Sample Output
5 4
1 2 3 4 5
5 1 2 3 4


...Program finished with exit code 0
Press ENTER to exit console.

Java String Reverse - Palindrome | Java HackerRank

Java String Reverse

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.(Wikipedia)

Given a string A, print Yes if it is a palindrome, print No otherwise.

Constraints

  • A will consist at most 50 lower case English letters.

Sample Input

madam

Sample Output

Yes

Sample Output
madam
Yes


...Program finished with exit code 0
Press ENTER to exit console.
Source Code
import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        String A = sc.next();
        String B = "";
        int i, j, length = A.length();

        for(i = length - 1, j = 0; i >= 0; i--, j++){

            B = B + A.charAt(i);
        }
        if(B.equals(A)){
            System.out.println("Yes");
        }
        else{
            System.out.println("No");
        }
        
    }
}
Sample Output
java
No


...Program finished with exit code 0
Press ENTER to exit console.


Java Substring Comparisons | Java HackerRank

Java Substring Comparisons
We define the following terms:
A < B <...< Y < Z < a < b <...< y < z
For example, ball < cat, dog < dorm, Happy < happy, Zoo < ball.
  • A substring of a string is a contiguous block of characters in the string. For example, the substrings of abc are a, b, c, ab, bc, and abc.
Given a string, s, and an integer, k, complete the function so that it finds the lexicographically smallest and largest substrings of length k.

Input Format

The first line contains a string denoting s.
The second line contains an integer denoting k.

Constraints

  • 1 <= |s| <= 1000
  • s consists of English alphabetic letters only (i.e., [a-zA-Z]).

Output Format

Return the respective lexicographically smallest and largest substrings as a single newline-separated string.

Sample Input 0

welcometojava
3

Sample Output 0

ava
wel

Explanation 0

String s = "welcometojava" has the following lexicographically-ordered substrings of length k = 3:

["ava", "com", "elc", "eto", "jav", "lco", "met", "oja", "ome", "toj", "wel"]

We then return the first (lexicographically smallest) substring and the last (lexicographically largest) substring as two newline-separated values (i.e., ava\nwel).

The stub code in the editor then prints ava as our first line of output and wel as our second line of output.

Sample Output
welcometojava
3
ava
wel


...Program finished with exit code 0
Press ENTER to exit console.
Source Code
import java.util.Scanner;

public class Main {

    public static String getSmallestAndLargest(String s, int k) {
        
        int i, j, length;

        String temp = "";
        String smallest = s.substring(0, k);
        String largest = "";
        
        for(i = 0; i <= s.length()-k; i++){

            temp = s.substring(i, (i+k));

            if(temp.compareTo(smallest) <= 0){

                smallest = temp;
            }
            if(temp.compareTo(largest) >= 0){

                largest = temp;
            }
        }
        return smallest + "\n" + largest;
    }


    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.next();
        int k = scan.nextInt();
        scan.close();
      
        System.out.println(getSmallestAndLargest(s, k));
    }
}
Sample Output
welcometojava
3
ava
wel


...Program finished with exit code 0
Press ENTER to exit console.


Write a program in Java to perform the followings conditions - Java Time Example | Java Method Overloading

Write a program in Java to perform the followings

  1. Create a class TimeShow with three private members second, minute and hour.
  2. Design three constructors of the TimeShow class. The first constructor will be a default constructor, the second constructor will receive three different values and the third constructor will receive another object of the same class to initialize the second, minute and hour values of an object.
  3. Design a display method to display the time of the calling object in hours:minutes:seconds format.
  4. Create a TimeExample class and define a main method inside it. Now create three objects of the TimeShow class inside the main method with three different types of initialization.
  5. Display the time of the three objects of TimeShow class.

Sample Output
5:16:27
14:25:36
14:25:36


...Program finished with exit code 0
Press ENTER to exit console.
Source Code
package timeexample;

//Create a class TimeShow with three private members second, minute and hour.
class TimeShow{
    
    private int hour;
    private int second;
    private int minute;
    
    //The first constructor will be a default constructor
    TimeShow(){
        hour = 5;
        minute = 16;
        second = 27;
    }
    //The second constructor will receive three different values.
    TimeShow(int x, int y, int z){
        
        hour = x;
        minute = y;
        second = z;        
    }
   /*
    Third constructor will receive another object of the same class to
    initialize the second, minute and hour values of an object.
    */
    TimeShow(TimeShow obj) {
        hour = obj.hour;
        second = obj.second;
        minute = obj.minute;
    }
 

    /*Design a display method to display the time of the calling object in
      hours:minutes:seconds format.*/
    void displayMethod(){
        
        System.out.println(+hour+":"+minute+":"+second+"");
    }
}

//Create a TimeExample class and define a main method inside it.
public class TimeExample {

    public static void main(String[] args) {       
        /*Now create three objects of the TimeShow class inside the main
            methodwith three different types of initialization.*/
        //Display the time of the three objects of TimeShow class.
        
        TimeShow obj1 = new TimeShow();
        TimeShow obj2 = new TimeShow(14, 25, 36);
        TimeShow obj3 = new TimeShow(obj2);
        
        obj1.displayMethod();
        obj2.displayMethod();
        obj3.displayMethod();
    }
}
Sample Output
5:16:27
14:25:36
14:25:36


...Program finished with exit code 0
Press ENTER to exit console.

Data Structure and Algorithm Problem Solution

Write a program to calculate employees age | Data Structure

Calculate employees age

Suppose a company keeps a linear array A (1920:1970) such that A[K] contains the number of employees born in year K, write a module/algorithm for the following tasks:

  1. Find the number NUM of years in which no employee was born.
  2. Find the number NUM of employee whose age is not greater 45 years.
  3. Find the number NUM of employees whose age is an even number
  4. Find the average AVG age of the employees.
  5. Find the total number NUM of employee having an age between 40 to 60.

Sample Output
Enter Year(i.e. 1970 to 1990): 1975 1980
Employee Bron
1975: 3
1976: 0
1977: 3
1978: 0
1979: 9
1980: 8
No Employee Was Bron: 2

Employee whose age is not greater 45 years: 6

Employees whose age is an even number: 8

Average age of the employees: 42

Employee having an age between 40 to 60: 6



Process returned 0 (0x0)   execution time : 35.168 s
Press any key to continue.
C-Source Code
#include<stdio.h>

int main()
{
    int i, k, from, to, from2, to2, noEmployeeBorn, current = 2020;
    int notGreater45, even, avg, between;

    noEmployeeBorn = notGreater45 = even = avg = between = 0;

    printf("Enter Year(i.e. 1970 to 1990): ");
    scanf("%d %d", &from, &to);

    int employeeBorn[to];

    k = to - from + 1;
    from2 = from;
    to2 = to;

    printf("Employee Bron\n");
    for(i = from; i <= to; i++)
    {
        printf("%d: ", i);
        scanf("%d", &employeeBorn[i]);
    }
    for(i = from2; i <= to2; i++)
    {
        if(employeeBorn[i] == 0)
        {
            noEmployeeBorn++;
        }
        if((current - i) <= 45)
        {
            notGreater45++;
        }
        if((current - i) % 2 == 0)
        {
            even = even + employeeBorn[i];
        }
        avg = (avg + (current - i));
        if(((current - i) >= 40) && ((current - i) <= 60))
        {
            between++;
        }
    }
    printf("No Employee Was Bron: %d\n\nEmployee whose age is not greater 45 years: %d\n\nEmployees whose age is an even number: %d\n\nAverage age of the employees: %d\n\nEmployee having an age between 40 to 60: %d\n\n\n", noEmployeeBorn, notGreater45, even, avg / k, between);

    return 0;
}
Sample Output
Enter Year(i.e. 1970 to 1990): 1975 1980
Employee Bron
1975: 3
1976: 0
1977: 3
1978: 0
1979: 9
1980: 8
No Employee Was Bron: 2

Employee whose age is not greater 45 years: 6

Employees whose age is an even number: 8

Average age of the employees: 42

Employee having an age between 40 to 60: 6



Process returned 0 (0x0)   execution time : 35.168 s
Press any key to continue.

Write a c program to reverse an array before target number and after target number

Reverse an array where the output will be like this...

Write a c program to reverse an array before target number and after target number. i.e. if the array is 1 2 3 4 5 6 7 and the target number is 4 then it will print 3 2 1 4 7 6 5

Sample Output
Enter array size: 7
Enter array numbers: 1 2 3 4 5 6 7
Enter a target number: 4
3 2 1 4 7 6 5

Process returned 0 (0x0)   execution time : 10.573 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int a[100], i, j, k, size, num, LHS[100], RHS[100];

    /**
    LHS = Left Hand Side
    RHS = Right Hand Side

    LHS is the left side of its target number
    and RHS is the right side of target number.
    */

    printf("Enter array size: ");
    scanf("%d", &size);

    printf("Enter array numbers: ");
    for(i = 0; i < size; i++)
    {
        scanf("%d", &a[i]);
    }

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

    for(i = 0; i < size; i++)
    {
        if(num == a[i])
        {
            break;
        }
    }
    if(num != a[i])
    {
        printf("\nTarget number not found!\nThe target number must be include in input number.\n\n");
        goto target;
    }

    for(j = i - 1, k = 0; j >= 0 ; j--, k++)
    {
        LHS[k] = a[j];
    }
    for(j = 0; j < i; j++)
    {
        a[j] = LHS[j];
    }
    for(j = size - 1, k = i + 1; k < size; k++, j--)
    {
        RHS[k] = a[j];
    }
    for(j = i + 1; j < size; j++)
    {
        a[j] = RHS[j];
    }
    for(i = 0; i < size; i++)
    {
        printf("%d ", a[i]);
    }
    printf("\n");

    return 0;
}
Sample Output
Enter array size: 5
Enter array numbers: 1 2 3 4 5
Enter a target number: 9

Target number not found!
The target number must be include in input number.

Enter a target number: 7

Target number not found!
The target number must be include in input number.

Enter a target number: 3
2 1 3 5 4

Process returned 0 (0x0)   execution time : 16.298 s
Press any key to continue.

Write a C program to count number of digits in a number

Count number of digits in a number
Sample Output
Count number of digits in a number.

Enter number: 192002024

Total 9 digit(s).

Process returned 0 (0x0)   execution time : 12.468 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int n, num, count = 0;

    printf("Count number of digits in a number.\n");
    printf("\nEnter number: ");
    scanf("%d", &num);
    
    for(n = 1; num != 0; n++)
    {
        num = num / 10;
        count++;
    }
    printf("\nTotal %d digit(s).\n", count);
    
    return 0;
}
Sample Output
Count number of digits in a number.

Enter number: 6789

Total 4 digit(s).

Process returned 0 (0x0)   execution time : 6.375 s
Press any key to continue.

Write a C program to print multiplication table of any number

Print multiplication table of any number
Sample Output
Enter a number: 5
Enter limit: 10
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Process returned 0 (0x0)   execution time : 33.760 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int n, num, limit;

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

    scanf("%d", &limit, printf("Enter limit: "));

    for(n = 1; n <= limit; n++)
    {
        printf("%d * %d = %d\n", num, n, num * n);
    }
    return 0;
}
Sample Output
Enter a number: 7
Enter limit: 5
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35

Process returned 0 (0x0)   execution time : 24.210 s
Press any key to continue.

Write a C program to find sum of all odd numbers between 1 to n

Find sum of all odd numbers between 1 to n
Sample Output
Enter a number: 10

Odd numbers are...
1
3
5
7
9

Sum: 25

Process returned 0 (0x0)   execution time : 91.045 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int n, num, sum = 0;
    
    printf("Enter a number: ");
    scanf("%d", &num);

    printf("\nOdd numbers are...\n");
    
    for(n = 1; n <= num; n++)
    {
        if(n % 2 != 0)
        {
            sum += n; // Or, sum = sum + n;
            
            printf("%d\n", n);
        }
    }
    printf("\nSum: %d\n", sum);
    
    return 0;
}
Sample Output
Enter a number: 7

Odd numbers are...
1
3
5
7

Sum: 16

Process returned 0 (0x0)   execution time : 24.620 s
Press any key to continue.

Write a C program to find sum of all even numbers between 1 to n

Find sum of all even numbers between 1 to n
Sample Output
Enter a number: 6

Even numbers are...

2
4
6
Sum: 12

Process returned 0 (0x0)   execution time : 217.563 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int n, num, sum = 0;

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

    printf("\nEven numbers are...\n");

    for(n = 1; n <= num; n++)
    {
        if(n % 2 == 0)
        {
            sum += n; // Or, sum = sum + n;

            printf("\n%d", n);
        }
    }
    printf("\nSum: %d\n", sum);

    return 0;
}
Sample Output
Enter a number: 11

Even numbers are...

2
4
6
8
10
Sum: 30

Process returned 0 (0x0)   execution time : 21.162 s
Press any key to continue.

Write a C program to find sum of all natural numbers between 1 to n

Find sum of all natural numbers between 1 to n
Sample Output
Enter a number: 6

Natural numbers are...

1

2

3

4

5

6

Sum: 21

Process returned 0 (0x0)   execution time : 11.998 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int n, num, sum = 0;

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

    printf("\nNatural numbers are...\n");

    for(n = 1; n <= num; n++)
    {
        sum += n; // Or, sum = sum + n;

        printf("\n%d\n", n);
    }
    printf("\nSum: %d\n", sum);

    return 0;
}
Sample Output
Enter a number: 6

Natural numbers are...

1

2

3

4

5

6

Sum: 21

Process returned 0 (0x0)   execution time : 11.998 s
Press any key to continue.

Write a C program to print all odd number from 1 to 100. - Using while loop

Print all odd numbers from 1 to 100. - Using while loop
Sample Output
Start...
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99

Process returned 0 (0x0)   execution time : 0.091 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int num, n;
    
    printf("Start...\n");

    n = 1;
    while(n <= 100)
    {
        if(n % 2 != 0)
        {
            printf("%d\n", n);
        }
        n++;
    }
    return 0;
}
Sample Output
Start...
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99

Process returned 0 (0x0)   execution time : 0.091 s
Press any key to continue.

Write a C program to print all even numbers from 1 to 100. - Using while loop

Print all even numbers from 1 to 100. - Using while loop
Sample Output
Start...
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

Process returned 0 (0x0)   execution time : 0.087 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int num, n;
    printf("Start...\n");

    n = 1;

    while(n <= 100)
    {
        if(n % 2 == 0)
        {
            printf("%d\n", n);
        }
        n++;
    }
    return 0;
}
Sample Output
Start...
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

Process returned 0 (0x0)   execution time : 0.087 s
Press any key to continue.

Write a C program to print all alphabets from a to z. - using while loop

Print all alphabets from a to z. - using while loop
Sample Output
All alphabets from a to z.
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

Process returned 0 (0x0)   execution time : 0.055 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    char c;

    printf("All alphabets from a to z.\n");

    c = 'a';
    while(c <= 'z')
    {
        printf("%c\n", c);
        c++;
    }
    return 0;
}
Sample Output
All alphabets from a to z.
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

Process returned 0 (0x0)   execution time : 0.055 s
Press any key to continue.

Write a C program to print all natural numbers in reverse (from n to 1). - Using while loop

Print all natural numbers in reverse (from n to 1). - using while loop
Sample Output
Enter a number: 6
6
5
4
3
2
1

Process returned 0 (0x0)   execution time : 7.692 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int num, n;

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

    n = num;
    while(n >= 1)
    {
        printf("%d\n", n);
        n--;
    }
    return 0;
}
Sample Output
Enter a number: 4
4
3
2
1

Process returned 0 (0x0)   execution time : 280.473 s
Press any key to continue.

Data Communication Problem Solution

1. Write a C program that takes a classless IP address as input and displays it in binary, the number of network and host address depending on number represented after the backslash

Sample Input: 10.15.15.1/24
Sample Output:
IP in binary: 00001010.00001111.00001111.00000001
Network Address: 16777214
Hosts Address: 254

2. Write a C program that takes a classful IP address as input and displays it in binary, identifies its class from the first binary octet and the number of network and host address depending on the class

Sample Input:10.15.15.1
Sample Output:
IP in binary: 00001010.00001111.00001111.00000001
Class: A
Network Address: 254
Hosts Address: 16777214

3. Write a C/JAVA program to implement CRC. The program will take a sequence of binary data bits, and a divider as input. And gives the resultant data with CRC as output. (Where, divider < binary data bits)

Sample Input:
Data: 10110
Divider: 1101

Sample Output:
Modified data: 10110000
Data Send: 10110101
CRC bit: 101

4. Write a program that choose Even parity / Odd parity according to user choose. And accordingly performs Even parity / Odd parity process

Example 1:
Enter 1 for Even parity and 2 for Odd parity.
Input: 1

Even parity:
Input:
Data: 110110
Output:
modified data: 1101100
Parity bit: 0

Example 2:
Enter 1 for Even parity and 2 for Odd parity.
Input: 2

Odd parity:
Input:
Data: 1101101
Output:
Modified data: 11011010
Parity bit: 0

5. Write a program that construct an Even parity Hamming code

Example-1:
Input: 10111010100101
Output: 
Mod data: 1111011110101000101
Number of parity bits: 5

Example-2:
Input: 101100
Output: 
Mod data: 0110011000
Number of parity bits: 4

6. Write a program to calculate the MINIMUM HAMMING DISTANCE for a given set of binary data taken as input. The first line of input is an integer, i (0<i<10) which mentions the number of inputs to be taken

Sample Input:
    Number of inputs: 3
    10110
    01110
    11001
    Sample Output:
    Minimum hamming distance, (C1, C2) = 2

7. Write a program to find Hamming Distance

Change Theme
X