C / C ++棘手程序

这里有10个棘手的程序,它们将测试您的编程基础知识。

1.程序在C ++中打印“”

在C ++编程语言中,我们使用引号表示要打印的文本的开头和结尾。因此,打印引号“需要特殊的转义序列。因此,我们将使用\“在c ++中打印引号。

示例

#include<iostream>
using namespace std;
int main() {
   cout<<"\"nhooo.com \"";
   return 0;
}

输出结果

"nhooo.com "

2.使用循环或goto语句打印从1到10的数字

在编程中多次遍历同一代码块时,有几种方法。他们是-

  • 使用循环

  • 使用goto语句

  • 使用递归函数

由于您不能使用循环或goto语句,因此唯一有效的方法是使用递归函数。让我们看看如何使用递归调用来打印1到10之间的数字。

示例

#include <stdio.h>
void printNumber(int count){
   printf("%d\n", count );
   count+=1;
   if(count<=10)
      printNumber(count);
}
int main(){
   printNumber(1);
   return 0;
}

输出结果

1
2
3
4
5
6
7
8
9
10

3.在不使用算术或比较运算符的情况下检查两个数是否相等

要检查两个数字是否相等,我们可以使用按位XOR运算符(^)。如果两个数字相等,则这些数字的按位XOR为0。现在,让我们在程序中实现此概念。

示例

#include<iostream>
using namespace std;
int main(){
   int a = 132;
   int b = 132;
   if ( (a ^ b) )
      cout<<"a is not equal to b";
   else
      cout<<"a is else to b";
      return 0;
}

输出结果

a is equal to b

4.打印“ Hello”而不使用;在C / C ++中

在c / c ++编程语言中,有一些无需使用分号即可打印内容的方法。我们可以通过使用输出方法printf的返回类型来做到这一点。c ++中的printf方法将打印到输出屏幕的字符数返回。我们可以使用和条件语句,而无需分号即可执行。

示例

#include <stdio.h>
int main(){
   if(printf("Hello "))
   return 0;
}

输出结果

Hello

5.编程以查找两个数字的最大值和最小值,而无需使用比较运算符。

为了找到不使用比较运算符定义的两个数字的最大和最小数目,我们将使用abs方法,并将两个数字的差值传递给它。它将返回数字之间的正差,我们将减去此绝对差以找到两个给定数字的最大值和最小值。

示例

#include<iostream>
using namespace std;
int main (){
   int x = 15, y = 20;
   cout<<"The numbers are x = "<<x<<"and y = "<<y<<endl;
   cout<<"The max of the numbers is "<<((x + y) + abs(x - y)) / 2<<endl;
   cout<<"The min of the numbers is "<<((x + y) - abs(x - y)) / 2<<endl;
   return 0;
}

输出结果

The numbers are x = 15and y = 20
The max of the numbers is 20
The min of the numbers is 15

6,打印程序源代码并输出

将程序的源代码打印为同一程序的输出是一个棘手的问题,并且需要对编程语言有很好的理解。

在此程序中,我们将使用文件处理的概念并打开用于编写代码的同一文件,然后打印文件的内容。

示例

#include <stdio.h>
int main(void){
   FILE *program;
   char ch;
   program = fopen(__FILE__, "r");
   do{
      ch=fgetc(program);
      printf("%c", ch);
   }
   while(ch!=EOF);
      fclose(program);
   return 0;
}

7.程序无需使用+运算符即可查找两个数字的和

通过在代码中多次使用-运算符,可以找到两个数字的和而不使用+运算符。下面的程序显示了如何。

示例

#include<iostream>
using namespace std;
int main(){
   int x = 5;
   int y = 5;
   int sum = x - (-y);
   cout<<"The numbers are x = "<<x<<" y = "<<y<<endl;
   cout<<"Their sum = "<<sum;
   return 0;
}

输出结果

The numbers are x = 5 y = 5
Their sum = 10

8.在不使用算术或关系运算符的情况下检查给定数字是否为偶数。

要检查给定的数字是否为偶数,我们可以使用按位运算符。按位&运算符与0x01一起将检查数字中第0位的位。如果第0位的位为1,则数字为奇数,否则为偶数。

示例

#include<iostream>
using namespace std;
int main(){
   int a = 154;
   if(a & 0x01) {
      cout<<a<<" is an odd number";
   } else{
      cout<<a<<" is an even number";
   }
   printf("\n");
   return 0;
}

输出结果

154 is an even number

9.编程将数字除以4,而不使用/运算符。

要在不使用除法运算符的情况下将数字除以4,我们可以使用右移运算符>>来移位最后一位。

示例

#include<iostream>
using namespace std;
int main(){
   int n = 128;
   cout<<n<<"divided by 4 = ";
   n = n >> 2;
   cout<< n;
   return 0;
}

输出结果

128 divided by 4 = 32

10.C ++程序递归计算一个数字的总和,直到它成为一位数字为止。

通过将数字的所有数字相加来计算递归总和,然后查看它是否为一位数字,然后停止,否则重新计算总和,直到总和变为一位数字为止。

示例

#include <iostream>
using namespace std;
int main() {
   int a = 534;
   int sum;
   if(a)
      sum = a % 9 == 0 ? 9 : a % 9 ;
   else
      sum = 0;
   cout<<"The final sum is "<<sum;
   return 0;
}

输出结果

The final sum is 3