Home  >  Article  >  Backend Development  >  Collection of C++ programming puzzles: stimulate thinking and improve programming skills

Collection of C++ programming puzzles: stimulate thinking and improve programming skills

WBOY
WBOYOriginal
2024-06-01 22:26:00467browse

C programming puzzles cover algorithm and data structure concepts such as Fibonacci sequence, factorial, Hamming distance, maximum and minimum values ​​of arrays, etc. By solving these puzzles, you can consolidate C knowledge and improve algorithm understanding and programming skills. .

C++ 编程谜题集锦:激发思维,提升编程水平

C Collection of programming puzzles: stimulate thinking and improve programming level

Preface

Programming puzzles are a great way to stimulate creativity and improve programming skills. This article has carefully selected some C programming puzzles covering various programming concepts and algorithms to help you gain a deeper understanding of this powerful language.

Puzzle 1: Fibonacci Sequence

Question:Write a C program to print the first n numbers of the Fibonacci Sequence number.

Code:

#include <iostream>

int main() {
  int n;
  std::cout << "Enter the number of Fibonacci numbers to print: ";
  std::cin >> n;

  int a = 0, b = 1;
  std::cout << "Fibonacci Series: ";
  for (int i = 1; i <= n; i++) {
    std::cout << a << " ";
    int c = a + b;
    a = b;
    b = c;
  }
  std::cout << std::endl;

  return 0;
}

Puzzle 2: Factorial

Question: Write a C program , calculates the factorial of a given number.

Code:

#include <iostream>

int main() {
  int n;
  std::cout << "Enter the number whose factorial you want to calculate: ";
  std::cin >> n;

  int factorial = 1;
  for (int i = 1; i <= n; i++) {
    factorial *= i;
  }
  std::cout << "Factorial of " << n << " is: " << factorial << std::endl;

  return 0;
}

Puzzle 3: Hamming Distance

Question: Write a C program to calculate Hamming distance between two binary numbers.

Code:

#include <iostream>

int main() {
  int n1, n2;
  std::cout << "Enter two binary numbers: ";
  std::cin >> n1 >> n2;

  int distance = 0;
  while (n1 > 0 || n2 > 0) {
    if ((n1 % 10) != (n2 % 10)) {
      distance++;
    }
    n1 /= 10;
    n2 /= 10;
  }
  std::cout << "Hamming distance between the two numbers is: " << distance << std::endl;

  return 0;
}

Puzzle 4: Array Maximum and Minimum Value

Question: Write a C program to find the maximum and minimum values ​​in a given array.

Code:

#include <iostream>

int main() {
  int arr[] = {10, 20, 5, 15, 30};
  int size = sizeof(arr) / sizeof(arr[0]);

  int max = arr[0];
  int min = arr[0];
  for (int i = 1; i < size; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
    if (arr[i] < min) {
      min = arr[i];
    }
  }
  std::cout << "Maximum element: " << max << std::endl;
  std::cout << "Minimum element: " << min << std::endl;

  return 0;
}

Conclusion

These puzzles are designed to challenge your programming skills and stimulate your creative potential. By solving these puzzles, you will be able to solidify your C knowledge and improve your understanding of algorithms and data structures.

The above is the detailed content of Collection of C++ programming puzzles: stimulate thinking and improve programming skills. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn