BeginnersBook-C-语言示例-一-

news/2024/10/24 18:14:44/文章来源:https://www.cnblogs.com/apachecn/p/18500086

BeginnersBook C 语言示例(一)

原文:BeginnersBook

协议:CC BY-NC-SA 4.0

C 程序:检查阿姆斯特朗数

原文: https://beginnersbook.com/2014/06/c-program-to-check-armstrong-number/

如果数字的各位的立方和等于数字本身,则将数字称为阿姆斯特朗数。在下面的 C 程序中,我们检查输入的数字是否是阿姆斯特朗数。

#include<stdio.h>
int main()
{int num,copy_of_num,sum=0,rem;//Store input number in variable numprintf("\nEnter a number:");scanf("%d",&num);/* Value of variable num would change in thebelow while loop so we are storing it in another variable to compare the results at the end of program*/copy_of_num = num;/* We are adding cubes of every digit* and storing the sum in variable sum*/ while (num != 0){rem = num % 10;sum = sum + (rem*rem*rem);num = num / 10;}/* If sum of cubes of every digit is equal to number* itself then the number is Armstrong*/if(copy_of_num == sum)printf("\n%d is an Armstrong Number",copy_of_num);elseprintf("\n%d is not an Armstrong Number",copy_of_num);return(0);
}

输出:

Enter a number: 370
370 is an Armstrong Number

您可以像这样验证结果:

370 = 3*3*3 + 7*7*7 + 0*0*0= 27 + 343 + 0= 370

如您所见,数字 370 的各位立方和等于数字本身。

C 程序:检查数字是否为回文数

原文: https://beginnersbook.com/2015/02/c-program-to-check-if-a-number-is-palindrome-or-not/

如果我们反转数字,它也保持不变,该数字也称为回文数。例如,12321 是回文数,因为如果我们反转它的数字它仍然是相同的。在本文中,我们共享了两个 C 程序来检查输入数字是否为回文数。 1)使用while循环 2)使用递归。

程序 1:使用while循环检查回文

/* Program to check if a number is palindrome or not* using while loop*/#include <stdio.h>
int main()
{int num, reverse_num=0, remainder,temp;printf("Enter an integer: ");scanf("%d", &num);/* Here we are generating a new number (reverse_num)* by reversing the digits of original input number*/temp=num;while(temp!=0){remainder=temp%10;reverse_num=reverse_num*10+remainder;temp/=10;} /* If the original input number (num) is equal to* to its reverse (reverse_num) then its palindrome* else it is not.*/ if(reverse_num==num) printf("%d is a palindrome number",num);elseprintf("%d is not a palindrome number",num);return 0;
}

输出:

checking_palindrome_number_output

程序 2:使用递归检查回文

#include<stdio.h>int check_palindrome(int num){static int reverse_num=0,rem;if(num!=0){rem=num%10;reverse_num=reverse_num*10+rem;check_palindrome(num/10);}return reverse_num;
}
int main(){int num, reverse_num;printf("Enter a number: ");scanf("%d",&num);reverse_num = check_palindrome(num);if(num==reverse_num)printf("%d is a palindrome number",num);elseprintf("%d is not a palindrome number",num);return 0;
}

输出:

palindrome_using_recursion

C 程序:查找给定范围内的回文数

原文: https://beginnersbook.com/2015/02/c-program-to-find-palindrome-numbers-in-a-given-range/

在上一个教程中,我们学习了如何检查数字是否是回文。在本教程中,我们将学习如何在给定范围内查找回文数。

C 程序 - 在给定范围内生成回文数

#include<stdio.h>
int main(){int num, rem, reverse_num, temp, start, end;printf("Enter the lower limit: ");scanf("%d",&start);printf("Enter the upper limit: ");scanf("%d",&end);printf("Palindrome numbers between %d and %d are: ",start,end);for(num=start;num<=end;num++){temp=num;reverse_num=0;while(temp){rem=temp%10;temp=temp/10;reverse_num=reverse_num*10+rem;}if(num==reverse_num)printf("%d ",num);}return 0;
}

输出:

generating_palindrome_numbers_in_a_range

C 程序:检查数字是偶数还是奇数

原文: https://beginnersbook.com/2015/02/c-program-to-check-if-number-is-even-or-odd/

如果一个数字可以被 2 整除,则它是偶数,否则它是一个奇数。在本文中,我们分享了两种方式(两个 C 程序)来检查输入数字是偶数还是奇数。 1)使用模数运算符(%)2)使用按位运算符。

程序 1:使用模数运算符

/* Program to check whether the input integer number * is even or odd using the modulus operator (%)*/
#include<stdio.h>
int main()
{// This variable is to store the input number int num;printf("Enter an integer: ");scanf("%d",&num);// Modulus (%) returns remainderif ( num%2 == 0 )printf("%d is an even number", num);elseprintf("%d is an odd number", num);return 0;
}

输出:

checking_even_odd_modulus_cmd

程序 2:使用按位运算符

/* Program to check if number is even or odd* using bitwise operator*/
#include<stdio.h>int main()
{int n;printf("Enter an integer: ");scanf("%d",&n);if ( n & 1)printf("%d is an odd number", n);elseprintf("%d is an even number", n);return 0;
}

输出:

checking_even_odd_bitwise

C 程序:查找字符的 ASCII 值

原文: https://beginnersbook.com/2017/09/c-program-to-find-ascii-value-of-a-character/

ASCII 值将英文字符表示为数字,每个字母分配一个 0 到 127 之间的数字。例如,大写Q的 ASCII 值为 81。

示例 1:显示用户输入的字符的 ASCII 值的程序

该程序获取用户输入的字符并显示其 ASCII 值。

#include <stdio.h>
int main()
{char ch;printf("Enter any character:");/* Reads the entered character and stores it* into the char variable ch*/scanf("%c", &ch);/* Using the format specifiers we can get the ASCII code* of a character. When we use %d format specifier for a* char variable then it displays the ASCII value of a char*/printf("ASCII value of character %c is: %d", ch, ch);return 0;
}

输出:

Enter any character:Q
ASCII value of character Q is: 81

查看相关的 C 程序:

  1. C 程序:展示斐波那契序列
  2. C 程序:检查数字是偶数还是奇数
  3. C 程序:计算并打印 nPr 的值
  4. C 程序:将大写字符串转换为小写

C 程序:查找intfloatdoublechar的大小

原文: https://beginnersbook.com/2017/09/c-program-to-find-the-size-of-int-float-double-andchar_

该程序查找数据类型的大小,如charintfloatdouble

示例:用于在 C 中查找数据类型大小的程序

在这个程序中,我们使用sizeof()运算符来查找数据类型的大小。当sizeof 与原始数据类型(如intfloatdoublechar)一起使用时,它将返回分配给它们的内存量。

#include<stdio.h>
int main()
{printf("Size of char: %ld byte\n",sizeof(char));printf("Size of int: %ld bytes\n",sizeof(int));printf("Size of float: %ld bytes\n",sizeof(float));printf("Size of double: %ld bytes", sizeof(double));return 0;
}

输出:

Size of char: 1 byte
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes

看看这些相关的 C 程序:

  1. C 程序:相加两个数字
  2. C 程序:查找数组中的元素数
  3. C 程序:检查数字是正数还是负数
  4. C 程序:按升序排列数字
  5. C 程序:查找商和余数

C 程序:检查字母是元音还是辅音

原文: https://beginnersbook.com/2017/09/c-program-to-check-whether-an-alphabet-is-vowel-or-consonant/

该程序检查输入字符是元音还是辅音。

示例:用于检查元音或辅音的程序

此程序将字符值(由用户输入)作为输入,并使用if-else语句检查该字符是否为元音或辅音。由于允许用户以小写和大写形式输入字母,因此程序检查大写和小写元音和辅音。要理解该程序,您应该熟悉以下 C 编程概念:

  1. C 编程if语句
  2. C 编程if..else语句
#include <stdio.h>
int main()
{char ch;bool isVowel = false;printf("Enter an alphabet: ");scanf("%c",&ch);if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U'){isVowel = true;}if (isVowel == true)printf("%c is a Vowel", ch);elseprintf("%c is a Consonant", ch);return 0;
}

输出:

Enter an alphabet: E
E is a Vowel

看看这些相关的 C 程序:

  1. C 程序:将小写字符串转换为大写
  2. C 程序:将大写字符串转换为小写
  3. C 程序:查找字符的 ASCII 值
  4. C 程序:在不使用预定义函数的情况下连接两个字符串
  5. C 程序:在不使用strlen 的情况下查找字符串的长度

C 程序:检查闰年

原文: https://beginnersbook.com/2017/09/c-program-to-check-leap-year/

该程序检查输入年份是否为闰年。

示例:检查闰年的程序

您可以使用以下数学逻辑检查一年是否是闰年:

闰年:

如果一年可以被 4, 100 和 400 整除,则它是闰年。

如果一年可以被 4 整除但不能被 100 整除那么它就是闰年

不是闰年:

如果一年不能被 4 整除那么它不是闰年

如果一年可被 4 和 100 整除但不能被 400 整除那么它就不是闰年

让我们在 C 程序中编写这个逻辑。要了解该程序,您应该具有以下 C 编程主题的知识:

  • C 编程if..else,嵌套if..else
#include <stdio.h>
int main()
{int y;printf("Enter year: ");scanf("%d",&y);if(y % 4 == 0){//Nested if elseif( y % 100 == 0){if ( y % 400 == 0)printf("%d is a Leap Year", y);elseprintf("%d is not a Leap Year", y);}elseprintf("%d is a Leap Year", y );}elseprintf("%d is not a Leap Year", y);return 0;
}

输出:

Enter year: 1991
1991 is not a Leap Year

看看这些相关的 C 程序:

  1. C 程序:检查字母是元音还是辅音
  2. C 程序:检查数字是否是回文
  3. C 程序:检查阿姆斯壮编号
  4. C 程序:检查数字是偶数还是奇数
  5. C 程序:检查数字是正数还是负数

C 程序:查找前 n 个自然数的和

原文: https://beginnersbook.com/2017/10/c-program-to-find-the-sum-of-first-n-natural-numbers/

程序查找前n个自然数的总和。我们将看到计算自然数的总和的两个 C 程序。在第一个 C 程序中,我们使用for循环查找总和,在第二个程序中,我们使用while循环执行相同操作。

要了解这些程序,您应该熟悉以下 C 编程概念:

  1. C 编程for循环
  2. C 编程while循环

示例 1:使用for循环查找自然数之和的程序

用户输入n的值,程序使用for循环计算前n个自然数的总和。

#include <stdio.h>
int main()
{int n, count, sum = 0;printf("Enter the value of n(positive integer): ");scanf("%d",&n);for(count=1; count <= n; count++){sum = sum + count;}printf("Sum of first %d natural numbers is: %d",n, sum);return 0;
}

输出:

Enter the value of n(positive integer): 6
Sum of first 6 natural numbers is: 21

示例 2:使用while循环查找自然数的总和

#include <stdio.h>
int main()
{int n, count, sum = 0;printf("Enter the value of n(positive integer): ");scanf("%d",&n);/* When you use while loop, you have to initialize the* loop counter variable before the loop and increment* or decrement it inside the body of loop like we did * for the variable "count"*/count=1;while(count <= n){sum = sum + count;count++;}printf("Sum of first %d natural numbers is: %d",n, sum);return 0;
}

输出:

Enter the value of n(positive integer): 7
Sum of first 7 natural numbers is: 28

看看这些相关的 C 程序:

  1. C 程序:查找数组元素之和
  2. C 程序:相加两个数字
  3. C 程序:查找商和余数
  4. C 程序:交换两个数字
  5. C 程序:查找数字的阶乘

字符串程序

简单的 C 程序

C 程序:将大写字符串转换为小写字符串

原文: https://beginnersbook.com/2015/02/c-program-to-convert-uppercase-string-to-lowercase-string/

在下面的 C 程序中,将要求用户输入一个字符串(它可以是完全大写或部分大写),然后程序会将其转换为完全(所有字符)小写的字符串。我们在以下程序中使用的逻辑是:所有大写字符(A-Z)的 ASCII 值范围为 65 到 90,它们对应的小写字符(a-z)的 ASCII 值比它们大 32。例如'A'的 ASCII 值为 65,'a'的 ASCII 值为 97(65 + 32)。同样适用于其他字符。


/* C program to convert uppercase string to* lower case* written by: Chaitanya*/
#include<stdio.h>
#include<string.h>
int main(){/* This array can hold a string of upto 25* chars, if you are going to enter larger string* then increase the array size accordingly*/char str[25];int i;printf("Enter the string: ");scanf("%s",str);for(i=0;i<=strlen(str);i++){if(str[i]>=65&&str[i]<=90)str[i]=str[i]+32;}printf("\nLower Case String is: %s",str);return 0;
}

输出:

uppercase_to_lowercase_string_output

正如你在输出中看到的那样,我们输入了一个部分(只有少数字符)大写字符串,程序输出是一个完全小写字符串。

C 程序:将小写字符串转换为大写字符串

原文: https://beginnersbook.com/2015/02/c-program-to-convert-lowercase-string-to-uppercase-string/

在以下程序中,将要求用户输入小写字符串,程序将其转换为大写字符串。程序中遵循的逻辑:所有小写字符(az)的 ASCII 值范围从 97 到 122,它们对应的大写字符(AZ)的 ASCII 值比它们小 32。例如,'a'具有 ASCII 值 97,'A'具有 ASCII 值 65(97-32)。其他字母同样适用。基于这个逻辑,我们编写了以下 C 程序进行转换。

C 程序 - 将字符串从小写转换为大写


/* C Program to convert Lower case* String to Upper case.* Written by: Chaitanya*/#include<stdio.h>
#include<string.h>
int main(){char str[25];int i;printf("Enter the string:");scanf("%s",str);for(i=0;i<=strlen(str);i++){if(str[i]>=97&&str[i]<=122)str[i]=str[i]-32;}printf("\nUpper Case String is: %s",str);return 0;
}

输出:

lowercase_to_uppercase_output

正如您在上面的截图中所观察到的,我们输入了一个小写字符串(beginnersbook.com)并且程序将其转换为大写字符串(BEGINNERSBOOK.COM

C 程序:按字母顺序对字符串集进行排序

原文: https://beginnersbook.com/2015/02/c-program-to-sort-set-of-strings-in-alphabetical-order/

在以下程序中,将要求用户输入一组字符串,程序将按字母顺序升序排序并显示它们。

C 程序 - 对一组字符串按字母顺序升序排序

/* This program would sort the input strings in* an ascending order and would display the same*/
#include<stdio.h>
#include<string.h>
int main(){int i,j,count;char str[25][25],temp[25];puts("How many strings u are going to enter?: ");scanf("%d",&count);puts("Enter Strings one by one: ");for(i=0;i<=count;i++)gets(str[i]);for(i=0;i<=count;i++)for(j=i+1;j<=count;j++){if(strcmp(str[i],str[j])>0){strcpy(temp,str[i]);strcpy(str[i],str[j]);strcpy(str[j],temp);}}printf("Order of Sorted Strings:");for(i=0;i<=count;i++)puts(str[i]);return 0;
}

输出:

sorted_strings_ascending

正如你在上面的输出屏幕截图中观察到的那样,我们输入了 5 个字符串,程序然后按升序对它们进行排序。我们得到一组有序的字符串作为输出。

C 程序:在不使用函数strlen()的情况下查找字符串的长度

原文: https://beginnersbook.com/2015/02/c-program-to-find-the-length-of-a-string/

在下面的 C 程序中,我们计算给定字符串中的字符数,并在控制台上显示其长度。在执行该程序时,将要求用户输入字符串,然后程序将对字符进行计数并输出字符串的长度。

C 程序 - 在不使用标准库函数strlen的情况下查找字符串的长度

/* C Program to find the length of a String without*  using any standard library function */
#include <stdio.h>
int main()
{/* Here we are taking a char array of size * 100 which means this array can hold a string * of 100 chars. You can change this as per requirement*/char str[100],i;printf("Enter a string: \n");scanf("%s",str);// '\0' represents end of Stringfor(i=0; str[i]!='\0'; ++i);printf("\nLength of input string: %d",i);return 0;
}

输出:

string_length_output

C 程序:在不使用strcat的情况下连接两个字符串

原文: https://beginnersbook.com/2015/02/c-program-to-concatenate-two-strings-without-using-strcat/

在下面的程序中,将要求用户输入两个字符串,然后程序将它们连接起来。对于连接,我们没有使用标准库函数strcat(),而是编写了一个逻辑来在第一个字符串的末尾附加第二个字符串。

用于字符串连接的 C 程序

/* C program to concatenate two strings without* using standard library function strcat()*/
#include <stdio.h>
int main()
{char str1[50], str2[50], i, j;printf("\nEnter first string: ");scanf("%s",str1);printf("\nEnter second string: ");scanf("%s",str2);/* This loop is to store the length of str1 in i* It just counts the number of characters in str1* You can also use strlen instead of this.*/for(i=0; str1[i]!='\0'; ++i); /* This loop would concatenate the string str2 at* the end of str1*/for(j=0; str2[j]!='\0'; ++j, ++i){str1[i]=str2[j];}// \0 represents end of stringstr1[i]='\0';printf("\n输出: %s",str1);return 0;
}

输出:

string_concat

正如您所看到的,我们已经输入了两个字符串,并且在程序的输出中两个字符串都被连接起来。

C 程序:使用递归来反转字符串

原文: https://beginnersbook.com/2014/06/c-program-to-reverse-a-string-using-recursion/

这里我们定义了一个函数reverse_string,这个函数递归地调用它自己。

#include <stdio.h>
#include <string.h>
void reverse_string(char*, int, int);int main()
{//This array would hold the string upto 150 charchar string_array[150];printf("Enter any string:");scanf("%s", &string_array);//Calling our user defined functionreverse_string(string_array, 0, strlen(string_array)-1);printf("\nReversed String is: %s",string_array);return 0;
}void reverse_string(char *x, int start, int end)
{char ch;if (start >= end)return;ch = *(x+start);*(x+start) = *(x+end);*(x+end) = ch;//Function calling itself: Recursionreverse_string(x, ++start, --end);
}

输出:

Enter any string: chaitanya
Reversed String is: aynatiahc

数组程序

C 程序:按升序排列数字

原文: https://beginnersbook.com/2015/02/c-program-to-arrange-numbers-in-ascending-order/

以下程序提示用户输入n个数字,一旦用户输入这些数字,该程序将按升序排序并显示它们。这里我们为排序目的创建了一个用户定义的函数sort_numbers_ascending()

/** C program to accept numbers as an input from user* and to sort them in ascending order.*/
#include <stdio.h>void sort_numbers_ascending(int number[], int count)
{int temp, i, j, k;for (j = 0; j < count; ++j){for (k = j + 1; k < count; ++k){if (number[j] > number[k]){temp = number[j];number[j] = number[k];number[k] = temp;}}}printf("Numbers in ascending order:\n");for (i = 0; i < count; ++i)printf("%d\n", number[i]);
}
void main()
{int i, count, number[20];printf("How many numbers you are gonna enter:");scanf("%d", &count);printf("\nEnter the numbers one by one:");for (i = 0; i < count; ++i)scanf("%d", &number[i]);sort_numbers_ascending(number, count);
}

输出:

numbers_ascending

C 程序:查找数组的最大元素

原文: https://beginnersbook.com/2015/02/c-program-to-find-largest-element-of-an-array/

在下面的程序中,我们使用给定数组的第一个元素初始化变量(max_element),然后我们使用循环将该变量与数组的所有其他元素进行比较,每当我们得到一个值大于max_element的元素时,我们将该元素移动到max_element并使用相同的方法进一步移动以获取数组中的最大元素。

#include <stdio.h>/*  This is our function to find the largest* element in the array arr[]*/
int largest_element(int arr[], int num)
{int i, max_element;// Initialization to the first array elementmax_element = arr[0];/* Here we are comparing max_element with* all other elements of array to store the * largest element in the max_element variable*/for (i = 1; i < num; i++)         if (arr[i] > max_element)max_element = arr[i];return max_element;
}int main()
{int arr[] = {1, 24, 145, 20, 8, -101, 300};int n = sizeof(arr)/sizeof(arr[0]);printf("Largest element of array is %d", largest_element(arr, n));return 0;
}

输出:

Largest element of array is 300

C 程序:使用指针,递归和函数来查找数组元素的总和

原文: https://beginnersbook.com/2014/06/c-program-to-find-sum-of-array-elements-using-pointers-recursion-functions/

在本教程中,我们将学习以下两种方法来找出数组元素的总和:

1)使用递归

2)使用指针

方法 1:使用递归的数组元素的总和:函数调用自身

该程序调用用户定义的函数sum_array_elements(),函数以递归方式调用自身。这里我们对数组元素进行了硬编码,但是如果你想让用户输入值,你可以使用for循环和scanf函数,就像我在本文的下一节(方法 2:使用指针)中所做的那样。

#include<stdio.h>
int main()
{int array[] = {1,2,3,4,5,6,7};int sum;sum = sum_array_elements(array,6);printf("\nSum of array elements is:%d",sum);return 0;
}
int sum_array_elements( int arr[], int n ) {if (n < 0) {//base case:return 0;} else{//Recursion: calling itselfreturn arr[n] + sum_array_elements(arr, n-1);}
}

输出:

Sum of array elements is:28

方法 2:使用指针的数组元素的总和

这里我们设置指向数组基址的指针然后我们递增指针并使用*运算符来获取和汇总所有数组元素的值。

#include<stdio.h>
int main()
{int array[5];int i,sum=0;int *ptr;printf("\nEnter array elements (5 integer values):");for(i=0;i<5;i++)scanf("%d",&array[i]);/* array is equal to base address* array = &array[0] */ptr = array;for(i=0;i<5;i++) {//*ptr refers to the value at addresssum = sum + *ptr;ptr++;}printf("\nThe sum is: %d",sum);
}

输出:

Enter array elements (5 integer values): 1 2 3 4 5
The sum is: 15

C 语言中的 Hello World 程序

原文: https://beginnersbook.com/2017/09/c-hello-world-program/

在这里,我们将编写两个 C 程序来在屏幕上显示Hello World。在第一个程序中,我们使用printf函数显示消息,在第二个程序中,我们调用用户定义的函数,该函数在屏幕上显示Hello World消息。

示例 1:显示Hello World

#include <stdio.h>
int main()
{/* printf function displays the content that is* passed between the double quotes.*/printf("Hello World");return 0;
}

输出:

Hello World
  1. #include <stdio.h> - 该语句告诉编译器在程序中包含此stdio.h文件。这是一个标准输入输出文件,包含常见输入输出函数的定义,如scanf()printf()。在上面的程序中,我们使用printf()函数。

  2. int main() - 这里main()是函数名,int是这个函数的返回类型。每个 C 程序都必须具有此函数,因为程序的执行以main()函数开始。此函数的返回值 0 表示程序的成功执行,而返回值 1 表示程序的不成功执行。这就是我们在这个主函数末尾有return 0;语句的原因。

  3. printf("Hello World"); - 此函数在屏幕上显示双引号内的内容。

  4. return 0; - 如上所述,值 0 表示成功执行main()函数。

示例 2:带有函数的Hello World程序

让我们查看带有用户定义的函数的相同的程序。在这个程序中,我们创建了一个函数hello(),用于在屏幕上打印消息。我们在main()函数中调用此函数。要了解用户定义的函数,请阅读本指南:C 中的用户定义函数 。

#include 
void hello(){printf("Hello World");
}
int main()
{//Calling a function herehello();return 0;
}

查看这些相关的 C 程序:

  1. C 程序:打印用户输入的号码
  2. C 程序:检查数字是偶数还是奇数
  3. C 程序:以升序排列数字

C 程序:查找数组中的元素数

原文: https://beginnersbook.com/2017/09/c-program-to-find-the-number-of-elements-in-an-array/

这里我们将编写一个 C 程序来查找给定数组中的元素数。

示例:用于查找数组大小的程序

我们用于查找元素数量的程序,对于所有类型的数组都是通用的。在这个例子中,我们有一个double数据类型的数组,但是你可以对其他数据类型的数组使用相同的逻辑,如:intfloatlongchar等。

#include <stdio.h>
int main()
{double arr[] = {11, 22, 33, 44, 55, 66};int n;/* Calculating the size of the array with this formula.* n = sizeof(array_name) / sizeof(array_name[0])* This is a universal formula to find number of elements in* an array, which means it will work for arrays of all data* types such as int, char, float etc.*/n = sizeof(arr) / sizeof(arr[0]);printf("Size of the array is: %d\n", n);return 0;
}输出:
Size of the array is: 6

查看相关的 C 程序:

  1. C 程序:查找数组的最大元素
  2. C 程序:查找数组元素之和
  3. C 程序:显示用户输入的数字
  4. C 程序:检查数字是否是回文

排序程序

C 冒泡排序程序

原文: https://beginnersbook.com/2015/02/c-program-for-bubble-sorting/

冒泡排序也称为下沉排序。该算法比较每对相邻项,如果它们的顺序错误则交换它们,并且继续进行同样的过程,直到不需要交换。在下面的程序中,我们使用 C 语言实现冒泡排序。在该程序中,将要求用户输入元素的数量以及元素值,然后程序将使用冒泡排序算法逻辑按升序对它们进行排序。

在 C 程序中实现冒泡排序算法

/* Implementing Bubble sort in a C Program* Written by: Chaitanya. */
#include<stdio.h>int main(){int count, temp, i, j, number[30];printf("How many numbers are u going to enter?: ");scanf("%d",&count);printf("Enter %d numbers: ",count);for(i=0;i<count;i++)scanf("%d",&number[i]);/* This is the main logic of bubble sort algorithm */for(i=count-2;i>=0;i--){for(j=0;j<=i;j++){if(number[j]>number[j+1]){temp=number[j];number[j]=number[j+1];number[j+1]=temp;}}}printf("Sorted elements: ");for(i=0;i<count;i++)printf(" %d",number[i]);return 0;
}

输出:

bubble_sorting_output

C 中的插入排序程序

原文: https://beginnersbook.com/2015/02/insertion-sort-program-in-c/

插入排序算法逐个选择元素,并将其放置在元素有序列表中的所在位置。在下面的 C 程序中,我们实现了相同的逻辑。

在完成程序之前,让我们看一下示例帮助下的插入排序步骤。

  • 输入元素:89 17 8 12 0
  • 步骤 1: 89 17 8 12 0(粗体元素是有序列表,非粗体是无序列表)
  • 步骤 2: 17 89 8 12 0(每个元素将从无序列表中删除并放置在有序列表中的正确位置)
  • 步骤 3: 8 17 89 12 0
  • 步骤 4: 8 12 17 89 0
  • 步骤 5: 0 8 12 17 89

C 程序 - 插入排序实现

#include<stdio.h>
int main(){/* Here i & j for loop counters, temp for swapping,* count for total number of elements, number[] to* store the input numbers in array. You can increase* or decrease the size of number array as per requirement*/int i, j, count, temp, number[25];printf("How many numbers u are going to enter?: ");scanf("%d",&count);printf("Enter %d elements: ", count);// This loop would store the input numbers in arrayfor(i=0;i<count;i++)scanf("%d",&number[i]);// Implementation of insertion sort algorithmfor(i=1;i<count;i++){temp=number[i];j=i-1;while((temp<number[j])&&(j>=0)){number[j+1]=number[j];j=j-1;}number[j+1]=temp;}printf("Order of Sorted elements: ");for(i=0;i<count;i++)printf(" %d",number[i]);return 0;
}

输出:

insertion_sort_output_cmd

正如您可以在输出中观察到的那样,我们以随机顺序输入了 6 个整数,并且上面的 C 程序通过使用插入排序算法的逻辑以升序对它们进行排序。

C 中的选择排序程序

原文: https://beginnersbook.com/2015/02/selection-sort-program-in-c/

在选择排序中,最小元素与未排序元素列表的第一个元素交换(交换元素占用最初放置最小元素的位置)。然后,第二个最小元素与未排序元素列表的第二个元素交换,依此类推,直到所有元素都被排序。在下面的 C 程序中,我们实现了相同的逻辑。

在完成程序之前,让我们通过一个例子来看看选择排序的步骤:

  • 输入的元素:22 0 -90 89 17
  • 步骤 1:-90 0 22 89 17(22 和 -90 交换位置)
  • 步骤 2:-90 0 22 89 17(0 在正确位置,无需交换)
  • 步骤 3:-90 0 17 89 22(22 和 17 交换位置)
  • 步骤 4:-90 0 17 22 89(89 和 22 交换位置)

C 程序 - 选择排序

#include<stdio.h>
int main(){/* Here i & j for loop counters, temp for swapping,* count for total number of elements, number[] to* store the input numbers in array. You can increase* or decrease the size of number array as per requirement*/int i, j, count, temp, number[25];printf("How many numbers u are going to enter?: ");scanf("%d",&count);printf("Enter %d elements: ", count);// Loop to get the elements stored in arrayfor(i=0;i<count;i++)scanf("%d",&number[i]);// Logic of selection sort algorithmfor(i=0;i<count;i++){for(j=i+1;j<count;j++){if(number[i]>number[j]){temp=number[i];number[i]=number[j];number[j]=temp;}}}printf("Sorted elements: ");for(i=0;i<count;i++)printf(" %d",number[i]);return 0;
}

输出:

selection_sort_output_cmd

正如您所看到的,我们已经按随机顺序输入了 6 个元素,程序通过使用我们在程序中实现的选择排序算法按升序对它们进行排序。您还可以修改此程序,以按降序对元素进行排序。

C 中的快速排序程序

原文: https://beginnersbook.com/2015/02/quicksort-program-in-c/

快速排序是一种分而治之的算法。步骤如下:1)从数组中选取一个元素,该元素称为支点元素。 2)将未排序的元素数组划分为两个数组,第一个子数组中是小于支点的值,而值大于支点的所有元素都在第二个子数组中(相等的值可以是任意一种)。此步骤称为分区操作。 3)对具有较小值的元素的子数组有序,和具有较大值的元素的子数组重复,递归地重复步骤 2,直到有序。我们在以下 C 程序中实现了相同的逻辑。

C 程序 - 快速排序算法实现

#include<stdio.h>
void quicksort(int number[25],int first,int last){int i, j, pivot, temp;if(first<last){pivot=first;i=first;j=last;while(i<j){while(number[i]<=number[pivot]&&i<last)i++;while(number[j]>number[pivot])j--;if(i<j){temp=number[i];number[i]=number[j];number[j]=temp;}}temp=number[pivot];number[pivot]=number[j];number[j]=temp;quicksort(number,first,j-1);quicksort(number,j+1,last);}
}int main(){int i, count, number[25];printf("How many elements are u going to enter?: ");scanf("%d",&count);printf("Enter %d elements: ", count);for(i=0;i<count;i++)scanf("%d",&number[i]);quicksort(number,0,count-1);printf("Order of Sorted elements: ");for(i=0;i<count;i++)printf(" %d",number[i]);return 0;
}

输出:

quick_sort_output_cmd

C 指针程序

C 程序:使用指针查找最大的三个数字

原文: https://beginnersbook.com/2019/02/c-program-to-find-the-largest-of-three-numbers-using-pointers/

在本教程中,我们将编写一个 C 程序,使用指针查找三个输入数字中最大的一个。

使用指针查找最大数字的程序

在下面的程序中,我们有三个整数num1num2num3。我们已将这三个数字的地址分别赋给三个指针p1p2p3。之后我们使用if else语句对存储在指针指向的地址处的值进行了比较。

#include <stdio.h>
int main()
{int num1, num2, num3;int *p1, *p2, *p3;//taking input from userprintf("Enter First Number: ");scanf("%d",&num1);printf("Enter Second Number: ");scanf("%d",&num2);printf("Enter Third Number: ");scanf("%d",&num3);//assigning the address of input numbers to pointersp1 = &num1;p2 = &num2;p3 = &num3;if(*p1 > *p2){if(*p1 > *p3){printf("%d is the largest number", *p1);}else{printf("%d is the largest number", *p3);}}else{if(*p2 > *p3){printf("%d is the largest number", *p2);}else{printf("%d is the largest number", *p3);}}return 0;
}

输出:

C Program to Find the Largest of three numbers using Pointers

相关 C 示例

  1. C 程序:使用指针来统计字符串中的元音和辅音
  2. C 程序:使用指针打印字符串
  3. C 程序:使用指针交换两个数字
  4. C 程序:创建初始化并访问指针变量
  5. C 程序:查找最大的三个数字而不使用指针

C 程序:使用指针计算字符串中的元音和辅音

原文: https://beginnersbook.com/2019/02/c-program-to-count-vowels-and-consonants-in-a-string-using-pointer/

在本教程中,我们将编写一个 C 程序,使用指针计算给定字符串中的元音和辅音。

要了解这个程序,你应该知道数组和指针在 C 中的基础知识。

使用指针计算字符串中的元音和辅音的程序

在下面的程序中,我们声明了一个char数组str来保存输入字符串,我们使用fgets()函数将其存储在数组中。我们已经将数组的基址(第一个元素的地址)赋给指针p。我们在while循环中使用指针p浏览输入字符串的所有字符,并在每次迭代时递增指针值。

#include <stdio.h>
int main()
{char str[100];char *p;int  vCount=0,cCount=0;printf("Enter any string: ");fgets(str, 100, stdin);//assign base address of char array to pointerp=str;//'\0' signifies end of the stringwhile(*p!='\0'){if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U'||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')vCount++;elsecCount++;//increase the pointer, to point next characterp++;}printf("Number of Vowels in String: %d\n",vCount);printf("Number of Consonants in String: %d",cCount);return 0;
}

输出:

C Program to Count Vowels and Consonants in a String using Pointer

相关 C 示例

  1. C 程序:使用指针打印字符串
  2. C 程序:用指针交换两个数字
  3. C 程序:创建初始化和访问指针变量

C 程序:使用指针打印字符串

原文: https://beginnersbook.com/2019/02/c-program-to-print-string-using-pointer/

在本教程中,我们将编写一个 C 程序,使用指针变量逐个字符地打印字符串。要了解此程序,您应该具备以下主题的基本知识:

  • C 指针
  • C 数组

使用指针打印字符串的程序

在下面的程序中,我们声明了一个char数组来保存输入字符串,并且我们已经声明了一个char指针。我们已经将数组基地址(数组的第一个元素的地址)分配给指针,然后我们通过在while循环中递增指针来显示char数组的每个元素。

#include <stdio.h>
int main()
{char str[100];char *p;printf("Enter any string: ");fgets(str, 100, stdin);/* Assigning the base address str[0] to pointer* p. p = str is same as p = str[0]*/p=str;printf("The input string is: ");//'\0' signifies end of the stringwhile(*p!='\0')printf("%c",*p++);return 0;
}

输出:

C Program to Print String using Pointer

相关 C 示例

  1. C 程序:用指针交换两个数字
  2. C 程序:创建,初始化并访问指针变量
  3. C 程序:查找前n个自然数的总和
  4. C 程序:查找两个数的平均值

C 程序:检查给定的整数是正还是负

译文: https://beginnersbook.com/2015/02/c-program-to-check-whether-the-given-integer-is-positive-or-negative/

在下面的程序中,我们检查输入整数是正数还是负数。如果输入数字大于零,则其为正数,否则为负数。如果数字为零则既不是正数也不是负数。我们在下面的 C 程序中遵循了相同的逻辑。

/* Description: A program to check whether the input* integer number is positive or negative. * Written by: Chaitanya Singh* Published on: beginnersbook.com*/
#include <stdio.h>void main()
{int num;printf("Enter a number: \n");scanf("%d", &num);if (num > 0)printf("%d is a positive number \n", num);else if (num < 0)printf("%d is a negative number \n", num);elseprintf("0 is neither positive nor negative");
}

输出 1:

Enter a number:
0
0 is neither positive nor negative

输出 2:

Enter a number:
-3
-3 is a negative number

输出 3:

Enter a number:
100
100 is a positive number

C 程序:使用指针交换两个数字

原文: https://beginnersbook.com/2019/02/c-program-to-swap-two-numbers-using-pointers/

在本教程中,我们将编写一个 C 程序,使用指针交换两个数字。我们已经涵盖如何在不使用指针的情况下交换两个数字。

使用指针交换两个数字的 C 示例

/*C program by Chaitanya for beginnersbook.com* Program to swap two numbers using pointers*/
#include <stdio.h>// function to swap the two numbers
void swap(int *x,int *y)
{int t;t   = *x;*x   = *y;*y   =  t;
}int main()
{int num1,num2;printf("Enter value of num1: ");scanf("%d",&num1);printf("Enter value of num2: ");scanf("%d",&num2);//displaying numbers before swappingprintf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);//calling the user defined function swap()swap(&num1,&num2);//displaying numbers after swappingprintf("After  Swapping: num1 is: %d, num2 is: %d\n",num1,num2);return 0;
}

输出:

C Program to swap two numbers using pointers

相关 C 示例

  1. C 程序:声明,初始化和访问指针
  2. C 程序:检查char是否为字母
  3. C 程序:将十进制转换为八进制
  4. C 程序:查找商和余数

C 程序:创建,初始化和访问指针变量

原文: https://beginnersbook.com/2019/02/c-program-to-create-initialize-and-access-a-pointer-variable/

在本教程中,我们将编写一个 C 程序来创建,初始化和访问指针变量。要学习指针的基础知识,请参阅我的教程: C 指针。

示例:用于创建,访问和初始化指针的程序

在下面的程序中,我们声明了一个字符变量ch和字符指针pCh,之后我们用 char ch的地址值初始化了指针变量pCh

该示例还说明了如何使用指针变量pCh访问ch的值和地址。

/* Created by Chaitanya for Beginnersbook.com* C program to create, initialize and access a pointer*/#include <stdio.h>int main()
{//char variablechar ch;//char pointerchar *pCh;/* Initializing pointer variable with the* address of variable ch*/pCh = &ch;//Assigning value to the variable chch = 'A';//access value and address of ch using variable chprintf("Value of ch: %c\n",ch);printf("Address of ch: %p\n",&ch);//access value and address of ch using pointer variable pChprintf("Value of ch: %c\n",*pCh);printf("Address of ch: %p",pCh);return 0;
}

输出:

C program to create, initialize and access a pointer variable

相关 C 示例

  1. C 程序:检查闰年
  2. C 程序:交换两个数字
  3. C 程序:查找intfloatdoublechar的大小
  4. C 程序:查找字符的 ASCII 值

计算程序

C 程序:计算并打印 nPr 的值

原文: https://beginnersbook.com/2015/02/c-program-to-calculate-and-print-the-value-of-npr/

在下面的程序中,我们根据给定的nr值计算 nPr 的值。 nPr 也可以表示为P(n, r)P(n, r)的公式是n! /(n - r)!,例如 P(6, 2)= 6! /(6-2)! => 720/24 = 30。我们在下面的 C 程序中实现了相同的逻辑。

#include <stdio.h>void main()
{int n, r, npr_var;printf("Enter the value of n:");scanf("%d", &n);printf("\nEnter the value of r:");scanf("%d", &r);/* nPr is also known as P(n,r), the formula is:* P(n,r) = n! / (n - r)! For 0 <= r <= n.*/npr_var = fact(n) / fact(n - r);printf("\nThe value of P(%d,%d) is: %d",n,r,npr_var);
}
// Function for calculating factorial
int fact(int num)
{int k = 1, i;// factorial of 0 is 1if (num == 0){return(k);}else{for (i = 1; i <= num; i++){k = k * i;}}return(k);
}

输出:

Enter the value of n:
5 
Enter the value of r:
2
The value of P(6,2) is: 30

C 程序:计算并打印 nCr 的值

原文: https://beginnersbook.com/2015/02/c-program-to-calculate-and-print-the-value-of-ncr/

在下面的程序中,我们计算并显示 nCr 的值。 nCr 也可以表示为 C(n, r)

公式为:

C(n, r)= n! /(r!(n - r)!),对于0 <= r <= n。这里!代表阶乘。例如:C(6, 2)= 6! /(2! * (6-2)!) => 720 /(2 * 24) => 15

我们在以下程序中进行了相同的计算。

#include <stdio.h>int fact(int num);void main()
{int n, r, ncr_var;printf("Enter the value of n:");scanf("%d", &n);printf("\nEnter the value of r:");scanf("%d", &r);/* ncr is also represented as C(n,r), the formula is:* C(n,r) = n! / ( r!(n - r)! ). For 0 <= r <= n.*/ncr_var = fact(n) / (fact(r) * fact(n - r));printf("\nThe value of C(%d,%d) is: %d",n,r,ncr_var);
}
/* This function is used to find the * factorial of given number num*/
int fact(int num)
{int k = 1, i;// factorial of 0 is 1if (num == 0){return(k);}else{for (i = 1; i <= num; i++){k = k * i;}}return(k);
}

输出:

Enter the value of n:
5 
Enter the value of r:
2
The value of C(6,2) is: 15

C 程序:两个浮点数相乘

原文: https://beginnersbook.com/2017/09/c-program-to-multiply-two-floating-point-numbers/

程序将两个浮点数相乘并将乘积显示为输出。

示例 1:显示两个浮点数乘积的程序

在该程序中,要求用户输入两个浮点数,程序将输入的值存储到变量num1num2中。然后程序将输入的数字相乘并将乘积显示为输出。

#include <stdio.h>
int main(){float num1, num2, product;printf("Enter first Number: ");scanf("%f", &num1);printf("Enter second Number: ");scanf("%f", &num2);//Multiply num1 and num2product = num1 * num2;// Displaying result up to 3 decimal places. printf("Product of entered numbers is:%.3f", product);return 0;
}

输出:

Enter first Number: 12.761
Enter second Number: 89.23
Product of entered numbers is:1138.664

示例 2:使用函数将两个数相乘的程序

在这个程序中,我们创建了一个用户定义的函数product(),它在函数调用期间将我们传递给它的数字相乘。此函数返回这些数字的乘积。要了解该程序,您应该具备以下 C 编程概念的知识:

  1. C - 函数
  2. C - 按值调用函数
#include <stdio.h>
/* Creating a user defined function product that* multiplies the numbers that are passed as an argument* to this function. It returns the product of these numbers*/
float product(float a, float b){return a*b;
}
int main()
{float num1, num2, prod;printf("Enter first Number: ");scanf("%f", &num1);printf("Enter second Number: ");scanf("%f", &num2);// Calling product functionprod  = product(num1, num2);// Displaying result up to 3 decimal places.printf("Product of entered numbers is:%.3f", prod);return 0;
}

输出:

Enter first Number: 12.761
Enter second Number: 89.23
Product of entered numbers is:1138.664

查看这些相关的 C 程序:

  1. C 程序:相加两个数字
  2. C 程序:打印用户输入的整数
  3. C 中的Hello World程序
  4. C 程序:查找给定范围内的回文数
  5. C 程序:将小写字符串转换为大写

C 程序:查找商和余数

原文: https://beginnersbook.com/2017/09/c-program-to-find-quotient-and-remainder/

程序根据用户输入的被除数和除数查找余数

示例 1:用于查找商和余数的程序

在该程序中,要求用户输入被除数和除数,然后程序根据输入值查找商和余数。

#include <stdio.h>
int main(){int num1, num2, quot, rem;printf("Enter dividend: ");scanf("%d", &num1);printf("Enter divisor: ");scanf("%d", &num2);/* The "/" Arithmetic operator returns the quotient* Here the num1 is divided by num2 and the quotient* is assigned to the variable quot*/quot = num1 / num2;/* The modulus operator "%" returns the remainder after* dividing num1 by num2.*/rem = num1 % num2;printf("Quotient is: %d\n", quot);printf("Remainder is: %d", rem);return 0;
}

输出:

Enter dividend: 15
Enter divisor: 2
Quotient is: 7
Remainder is: 1

示例 2:使用函数查找商和余数的程序

在这个程序中,我们正在做上述程序的相同的事情,但是在这里我们使用函数来查找商和余数。我们为计算创建了两个用户定义的函数。要理解这个程序,您应该对以下 C 编程主题有基本的了解:

  1. C 中的函数
  2. C 中的按值函数调用
#include <stdio.h>
// Function to computer quotient
int quotient(int a, int b){return a / b;
}// Function to computer remainder
int remainder(int a, int b){return a % b;
}int main(){int num1, num2, quot, rem;printf("Enter dividend: ");    scanf("%d", &num1);printf("Enter divisor: ");    scanf("%d", &num2);//Calling function quotient()    quot = quotient(num1, num2);//Calling function remainder()    rem = remainder(num1, num2);printf("Quotient is: %d\n", quot);    printf("Remainder is: %d", rem);return 0;
}

查看相关的 C 程序:

  1. C 程序:相加两个数字
  2. C 程序:相乘两个浮点数
  3. C 程序:查找字符的 ASCII 值
  4. C 程序:计算并打印 nCr 的值

C 程序:查找两个数字的平均值

原文: https://beginnersbook.com/2017/09/c-program-to-find-the-average-of-two-numbers/

在这里,我们将编写两个 C 程序来查找两个数字的平均值(由用户输入)。

示例 1:编程以查找两个数字的平均值

#include <stdio.h>
int main()
{int num1, num2;float avg;printf("Enter first number: ");scanf("%d",&num1);printf("Enter second number: ");scanf("%d",&num2);avg= (float)(num1+num2)/2;//%.2f is used for displaying output upto two decimal placesprintf("Average of %d and %d is: %.2f",num1,num2,avg);return 0;
}

输出:

Enter first number: 12
Enter second number: 13
Average of 12 and 13 is: 12.50

示例 2:使用函数用于查找均值的程序

在这个程序中,我们创建了一个用户定义函数average()来计算平均值。用户输入的数字在函数调用期间传递给该函数。

#include <stdio.h>
float average(int a, int b){return (float)(a+b)/2;
}
int main()
{int num1, num2;float avg;printf("Enter first number: ");scanf("%d",&num1);printf("Enter second number: ");scanf("%d",&num2);avg = average(num1, num2);//%.2f is used for displaying output upto two decimal placesprintf("Average of %d and %d is: %.2f",num1,num2,avg);return 0;
}

输出:

Enter first number: 20
Enter second number: 13
Average of 20 and 13 is: 16.50

查看相关的 C 程序:

  1. C 程序:相加两个数字
  2. C 程序:相乘两个浮点数
  3. C 程序:查找数组中的元素数
  4. C 程序:展示斐波那契序列

数字系统转换程序

C 程序:将二进制数转换为十进制数

原文: https://beginnersbook.com/2015/02/c-program-to-convert-binary-number-to-decimal-number/

该程序将二进制数转换为等效的十进制数。

示例:将二进制转换为十进制的程序

在这个程序中,我们创建了一个用户定义的函数binaryToDecimal(),用于二进制到十进制的转换。该程序将二进制数(由用户输入)作为输入,并使用函数将其转换为十进制数。要理解这个程序,您应该熟悉以下 C 编程的概念:

  1. C 中的用户定义函数
  2. [C while循环(https://beginnersbook.com/2014/01/c-while-loop/)
#include <stdio.h>
#include <math.h>
int binaryToDecimal(long binarynum)
{int decimalnum = 0, temp = 0, remainder;while (binarynum!=0){remainder = binarynum % 10;binarynum = binarynum / 10;decimalnum = decimalnum + remainder*pow(2,temp);temp++;}return decimalnum;
}int main()
{long binarynum;printf("Enter a binary number: ");scanf("%ld", &binarynum);printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum));return 0;
}

输出:

Enter a binary number: 1010111
Equivalent decimal number is: 87

看看这些相关的 C 程序:

  1. C 程序:将十进制数转换为八进制
  2. C 程序:将八进制数转换为十进制数
  3. C 程序:将八进制数转换为二进制数
  4. C 程序:将二进制数转换为八进制
  5. C 程序:将小写字符串转换为大写

C 程序:使用递归函数反转给定的数字

原文: https://beginnersbook.com/2014/06/c-program-to-reverse-a-given-number-using-recursive-function/

在本教程中,我们将学习以下两种反转数字的方法。

1)使用递归

2)使用while循环

使用递归反转给定的数字:

在这个程序中,我们调用用户定义的函数reverse_function,该函数递归调用自身。

#include<stdio.h>
int main(){int num,reverse_number;//User would input the numberprintf("\nEnter any number:");scanf("%d",&num);//Calling user defined function to perform reversereverse_number=reverse_function(num);printf("\nAfter reverse the no is :%d",reverse_number);return 0;
}
int sum=0,rem;
reverse_function(int num){if(num){rem=num%10;sum=sum*10+rem;reverse_function(num/10);}elsereturn sum;return sum;
}

输出:

Enter any number: 23456
After reverse the no is :65432

使用while循环反转数字

在上面的程序中,我们学习了如何使用递归函数反转数字。在这里,我们将学习如何使用while循环。

#include<stdio.h>
int main()
{int num,rem,reverse_num=0;//Input numberprintf("\nEnter any number:");scanf("%d",&num);while(num>=1){rem = num % 10;reverse_num = reverse_num * 10 + rem;num = num / 10;}printf("\nReverse of input number is: %d", reverse_num);return 0;
}

输出:

Enter any number: 49212
Reverse of input number is: 21294

C 程序:将十进制数转换为二进制数

原文: https://beginnersbook.com/2017/09/c-program-to-convert-decimal-number-to-binary-number/

该程序将十进制数转换为等效的二进制数。

示例:将十进制转换为二进制的程序

在这个程序中,我们创建了一个用户定义函数decimalToBinary(),用于十进制到二进制转换。程序将十进制数(由用户输入)作为输入,并使用函数decimalToBinary()将其转换为二进制数。要理解这个程序,你应该有一下 C 编程主题的基本概念:

  1. C - 函数
  2. C - while循环
#include <stdio.h>
#include <math.h>long decimalToBinary(int decimalnum)
{long binarynum = 0;int rem, temp = 1;while (decimalnum!=0){rem = decimalnum%2;decimalnum = decimalnum / 2;binarynum = binarynum + rem*temp;temp = temp * 10;}return binarynum;
}int main()
{int decimalnum;printf("Enter a Decimal Number: ");scanf("%d", &decimalnum);printf("Equivalent Binary Number is: %ld", decimalToBinary(decimalnum));return 0;
}

输出:

Enter a Decimal Number: 234
Equivalent Binary Number is: 11101010

看看这些相关的 C 程序:

  1. C 程序:将十进制转换为八进制数
  2. C 程序:将八进制转换为十进制数
  3. C 程序:将八进制数转换为二进制数
  4. C 程序:将二进制转换为八进制数
  5. C 程序:将二进制数转换为十进制数

C 程序:将十进制数转换为八进制数

原文: https://beginnersbook.com/2017/09/c-program-to-convert-decimal-to-octal-number/

该程序将输入的十进制数转换为八进制数。

示例:将十进制转换为八进制的程序

在这个程序中,我们为十进制到八进制转换创建了一个用户定义的函数decimalToOctal()。程序将十进制数(由用户输入)作为输入,并使用该函数将其转换为八进制数。要理解这个程序,您应该熟悉以下 C 编程概念:

  1. C 编程:用户自定义函数
  2. C 编程中的while循环
#include <stdio.h>
#include <math.h>
/* This function converts the decimal number "decimalnum"* to the equivalent octal number*/
int decimalToOctal(int decimalnum)
{int octalnum = 0, temp = 1;while (decimalnum != 0){octalnum = octalnum + (decimalnum % 8) * temp;decimalnum = decimalnum / 8;temp = temp * 10;}return octalnum;
}
int main()
{int decimalnum;printf("Enter a Decimal Number: ");scanf("%d", &decimalnum);printf("Equivalent Octal Number: %d", decimalToOctal(decimalnum));return 0;
}

输出:

Enter a Decimal Number: 436
Equivalent Octal Number: 664

看看这些相关的 C 程序:

  1. C 程序:将八进制转换为十进制
  2. C 程序:将八进制转换为二进制
  3. C 程序:将二进制转换为八进制
  4. C 程序:将二进制转换为十进制
  5. C 程序:将大写字符串转换为小写字符串

C 程序:将八进制数转换为十进制数

原文: https://beginnersbook.com/2017/09/c-program-to-convert-octal-number-to-decimal-number/

该程序将八进制数转换为等效的十进制数。

示例:将八进制转换为十进制的程序

在这个程序中,我们为八进制到十进制转换创建了一个用户定义的函数。程序将八进制数(由用户输入)作为输入,并使用函数将其转换为十进制数。要了解此程序,您应该具有以下 C 编程主题的知识:

  1. C - 用户自定义函数
  2. C while循环
#include <stdio.h>
#include <math.h>
/* This function converts the octal number "octalnum" to the* decimal number and returns it.*/
long octalToDecimal(int octalnum)
{int decimalnum = 0, temp = 0;while(octalnum != 0){decimalnum = decimalnum + (octalnum%10) * pow(8,temp);temp++;octalnum = octalnum / 10;}return decimalnum;
}
int main()
{int octalnum;printf("Enter an octal number: ");scanf("%d", &octalnum);printf("Equivalent decimal number is: %ld", octalToDecimal(octalnum));return 0;
}

输出:

Enter an octal number: 754
Equivalent decimal number is: 492

看看这些相关的 C 程序:

  1. C 程序:将八进制转换为二进制
  2. C 程序:将二进制转换为八进制
  3. C 程序:将二进制转换为十进制
  4. C 程序:将小写字符串转换为大写
  5. C 程序:将大写字符串转换为小写

C 程序:将二进制转换为八进制数

原文: https://beginnersbook.com/2017/09/c-program-to-convert-binary-to-octal-number-system/

该 C 程序将二进制数转换为等效的八进制数。

示例:将二进制转换为八进制的程序

在该程序中,要求用户输入二进制数,然后程序通过调用用户定义的函数将该二进制数转换为八进制数。要理解这个程序,您应该熟悉以下 C 编程概念:

  1. C - while循环
  2. C - 函数
#include <stdio.h>
#include <math.h>
//This function converts binary number to octal number
int binaryToOctal(long binarynum)
{int octalnum = 0, decimalnum = 0, i = 0;/* This while loop converts binary number "binarynum" to the* decimal number "decimalnum"*/while(binarynum != 0){decimalnum = decimalnum + (binarynum%10) * pow(2,i);i++;binarynum = binarynum / 10;}//i is re-initializedi = 1;/* This loop converts the decimal number "decimalnum" to the octal* number "octalnum"*/while (decimalnum != 0){octalnum = octalnum + (decimalnum % 8) * i;decimalnum = decimalnum / 8;i = i * 10;}//Returning the octal number that we got from binary numberreturn octalnum;
}
int main()
{long binarynum;printf("Enter a binary number: ");scanf("%ld", &binarynum);// calling the function hereprintf("Equivalent octal value: %d", binaryToOctal(binarynum));return 0;
}

输出:

Enter a binary number: 111001
Equivalent octal value: 71

在这个程序中,我们首先将输入的二进制数转换为十进制数,然后我们将该十进制数转换为八进制数。

看看这些相关的 C 程序:

  1. C 程序:将二进制转换为十进制
  2. C 程序:将十进制数转换为二进制数
  3. C 程序:将八进制数转换为二进制数
  4. C 程序:将小写字符串转换为大写
  5. C 程序:将大写字符串转换为小写

C 程序:将八进制数转换为二进制数

原文: https://beginnersbook.com/2017/09/c-program-to-convert-octal-number-to-binary-number/

该程序使用用户定义的函数将八进制数转换为二进制数。

示例:将八进制转换为二进制的程序

在这个程序中,我们创建了一个用户定义的函数octalToBinary()。此函数首先将八进制数(由用户输入)转换为十进制数,然后将该十进制数转换为二进制数。要了解该程序的工作原理,您应具备以下 C 编程主题的基本知识:

  1. C 编程循环
  2. C 编程函数
#include <stdio.h>
#include <math.h>
//This function converts octal number to binary number
long octalToBinary(int octalnum)
{int decimalnum = 0, i = 0;long binarynum = 0;/* This loop converts octal number "octalnum" to the* decimal number "decimalnum"*/while(octalnum != 0){decimalnum = decimalnum + (octalnum%10) * pow(8,i);i++;octalnum = octalnum / 10;}//i is re-initializedi = 1;/* This loop converts the decimal number "decimalnum" to the binary* number "binarynum"*/while (decimalnum != 0){binarynum = binarynum + (decimalnum % 2) * i;decimalnum = decimalnum / 2;i = i * 10;}//Returning the binary number that we got from octal numberreturn binarynum;
}
int main()
{int octalnum;printf("Enter an octal number: ");scanf("%d", &octalnum);//Calling the function octaltoBinaryprintf("Equivalent binary number is: %ld", octalToBinary(octalnum));return 0;
}

输出:

Enter an octal number: 71
Equivalent binary number is: 111001

看看这些相关的 C 程序:

  1. C 程序:将二进制数转换为八进制数
  2. C 程序:将二进制转换为十进制
  3. C 程序:将十进制转换为八进制数
  4. C 程序:将小写字符串转换为大写字符串
  5. C 程序:将大写字符串转换为小写

查找几何图形面积的程序

C 程序:计算圆的面积和周长

原文: https://beginnersbook.com/2014/06/c-program-to-calculate-area-and-circumference-of-circle/

这里我们编写一个简单的 C 程序,根据用户提供的半径值计算圆的面积和周长。

公式:

面积 = 3.14 * 半径 * 半径
周长 = 2 * 3.14 * 半径

根据用户输入计算面积和周长的程序

要计算面积和周长,我们必须知道圆的半径。程序将提示用户输入半径,并根据输入计算值。为简化起见,我们在程序中将标准PI值设为 3.14(常数)。其他详细信息在下面的示例程序中作为注释提及。

#include <stdio.h>
int main()
{int circle_radius;float PI_VALUE=3.14, circle_area, circle_circumf;//Ask user to enter the radius of circleprintf("\nEnter radius of circle: ");//Storing the user input into variable circle_radiusscanf("%d",&circle_radius);//Calculate and display Areacircle_area = PI_VALUE * circle_radius * circle_radius;printf("\nArea of circle is: %f",circle_area);//Caluclate and display Circumferencecircle_circumf = 2 * PI_VALUE * circle_radius;printf("\nCircumference of circle is: %f",circle_circumf);return(0);
}

输出:

Enter radius of circle: 2
Area of circle is: 12.560000
Circumference of circle is: 12.560000

如果您想要更准确的结果,那么将 PI 值设为22/7而不是 3.14,它将为您提供面积和周长的精确值。

C 程序:计算等边三角形的面积

原文: https://beginnersbook.com/2014/06/c-program-to-calculate-area-of-equilatral-triangle/

等边三角形具有相等的边(所有三边相等)。在本教程中,我们共享了一个 C 程序,它将三角形边作为输入,将区域计算和显示为输出。

计算面积的程序

为了计算等边三角形的面积,我们必须知道三角形的边。该程序将提示用户进入等边三角形的一边,并根据该值计算面积。

程序中使用的公式

Area = sqrt(3)/4 * side * side

这里sqrt表示“平方根”,这是math.h头文件的预定义函数。为了使用这个函数,我们在程序中包含了math.h头文件。

#include<stdio.h>
#include<math.h>
int main()
{int triangle_side;float triangle_area, temp_variable;//Ask user to input the length of the sideprintf("\nEnter the Side of the triangle:");scanf("%d",&triangle_side);//Caluclate and display area of Equilateral Triangletemp_variable = sqrt(3) / 4 ;triangle_area = temp_variable * triangle_side * triangle_side ;printf("\nArea of Equilateral Triangle is: %f",triangle_area);return(0);
}

输出:

Enter the Side of the triangle: 2
Area of Equilateral Triangle is: 1.732051

C 程序:查找最大的三个数字

原文: https://beginnersbook.com/2014/06/c-program-to-find-greatest-of-three-numbers/

在本教程中,我们共享了一个程序,该程序比较三个输入整数并返回最大数字作为输出。为了进行这种比较,我们使用了一个简单的if-elseif-else块。

查找三个输入数字中的最大数字的程序

该程序将提示用户输入三个整数,并根据输入,它将比较并显示最大数字作为输出。在这个程序num1, num2, num3是三个int变量,依次表示number1number2number3

#include<stdio.h>
int main()
{int num1,num2,num3;//Ask user to input any three integer numbersprintf("\nEnter value of num1, num2 and num3:");//Store input values in variables for comparsionscanf("%d %d %d",&num1,&num2,&num3);if((num1>num2)&&(num1>num3))printf("\n Number1 is greatest");else if((num2>num3)&&(num2>num1))printf("\n Number2 is greatest");elseprintf("\n Number3 is greatest");return 0;
}

输出:

Enter value of num1, num2 and num3: 15 200 101Number2 is greatest

C 程序:显示 Fibonacci 序列

原文: https://beginnersbook.com/2014/06/c-program-to-display-fibonacci-series/

在本教程中,我们将学习以下两种方法,以 C 编程语言显示 Fibonacci 序列:

1)使用for循环

2)使用递归

使用循环的 Fibonacci 序列

一个简单的for循环用于显示序列。程序提示用户输入项数,并显示具有相同项数的序列。

#include<stdio.h>
int main()
{int count, first_term = 0, second_term = 1, next_term, i;//Ask user to input number of terms printf("Enter the number of terms:\n");scanf("%d",&count);printf("First %d terms of Fibonacci series:\n",count);for ( i = 0 ; i < count ; i++ ){if ( i <= 1 )next_term = i;else{next_term = first_term + second_term;first_term = second_term;second_term = next_term;}printf("%d\n",next_term);}return 0;
}

输出:

Enter the number of terms: 8
First 8 terms of Fibonacci series:
0
1
1
2
3
5
8
13

使用递归显示 Fibonacci 序列的程序

这里我们使用用户定义的函数fibonacci_series(),它递归调用自身,以显示输入的项数量的序列。

#include<stdio.h>
int fibonacci_series(int);
int main()
{int count, c = 0, i;printf("Enter number of terms:");scanf("%d",&count);printf("\nFibonacci series:\n");for ( i = 1 ; i <= count ; i++ ){printf("%d\n", fibonacci_series(c));c++; }return 0;
}
int fibonacci_series(int num)
{if ( num == 0 )return 0;else if ( num == 1 )return 1;elsereturn ( fibonacci_series(num-1) + fibonacci_series(num-2) );
}

输出:

Enter number of terms: 6
Fibonacci series:
0
1
1
2
3
5

C 程序:使用递归查找数字的阶乘

原文: https://beginnersbook.com/2014/06/c-program-to-find-factorial-of-number-using-recursion/

该程序提示用户输入任何整数,查找输入数的阶乘,并在屏幕上显示输出。我们将使用用户定义的递归函数来执行任务。这里我们有一个函数find_factorial,它以递归的方式调用自己,来找出输入数的阶乘。我们在下面的程序中涉及用户交互,但是如果你不想要那个部分,那么你可以简单地为变量num赋一个整数值并忽略scanf语句。简而言之,您可以以任何您想要的方式调整它,每种情况的逻辑都是相同的。

查找阶乘的程序

 /* Program Name: Find FactorialWritten by: Chaitanya SinghPublished on: beginnersbook.com*/
#include<stdio.h>
int find_factorial(int);
int main()
{int num, fact;//Ask user for the input and store it in numprintf("\nEnter any integer number:");scanf("%d",&num);//Calling our user defined functionfact =find_factorial(num);//Displaying factorial of input numberprintf("\nfactorial of %d is: %d",num, fact);return 0;
}
int find_factorial(int n)
{//Factorial of 0 is 1 if(n==0)return(1);//Function calling itself: recursionreturn(n*find_factorial(n-1));
}

输出:

Enter any integer number: 4
factorial of 4 is: 24

C 程序:查找给定范围内的素数

原文: https://beginnersbook.com/2014/06/c-program-to-find-prime-numbers-in-a-given-range/

在执行以下程序时,将要求用户提供范围,然后程序将按顺序显示所提供范围的所有素数。使用此程序,您可以查找介于 1 到 100,100 到 999 等范围内的素数。您只需要输入范围,例如,如果您希望素数从 100 到 999,则在程序提示输入时输入数字 100 和 999。

寻找素数的程序

#include <stdio.h>
int main()
{int num1, num2, flag_var, i, j;/* Ask user to input the from/to range* like 1 to 100, 10 to 1000 etc.*/printf("Enter two range(input integer numbers only):");//Store the range in variables using scanfscanf("%d %d", &num1, &num2);//Display prime numbers for input rangeprintf("Prime numbers from %d and %d are:\n", num1, num2);for(i=num1+1; i<num2; ++i){flag_var=0;for(j=2; j<=i/2; ++j){if(i%j==0){flag_var=1;break;}}if(flag_var==0)printf("%d\n",i);}return 0;
}

输出:

Enter two range(input integer numbers only):Prime numbers from 1 and 50 are: 1 50
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/821288.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

BeginnersBook-C---教程-一-

BeginnersBook C++ 教程(一)原文:BeginnersBook 协议:CC BY-NC-SA 4.0C++ 中的for循环原文: https://beginnersbook.com/2017/08/cpp-for-loop/循环用于重复执行语句块,直到满足特定条件。例如,当您显示从 1 到 100 的数字时,您可能希望将变量的值设置为 1 并将其显示 …

BeginnersBook-Servlet-教程-一-

BeginnersBook Servlet 教程(一)原文:BeginnersBook 协议:CC BY-NC-SA 4.0项目的web.xml文件中的welcome-file-list标签原文: https://beginnersbook.com/2014/04/welcome-file-list-in-web-xml/你有没见过web.xml文件中的<welcome-file-list>标签并想知道它是什么?…

.net7.0 WebApi 添加 JWT 验证

https://blog.csdn.net/u012563853/article/details/128659472 详细步骤: 1.创建默认WebApi 项目2.开始添加认证包 安装 Microsoft.AspNetCore.Authentication.JwtBearer 包 这个包是用来支持WebApi 的 JWT 认证的3.在appsetting 中配置JWT的配置信息 这里需要注意的是 key …

2024-10-24 瀑布流(vue3)

效果图: 代码:<template><div id="waterfallContainer" class="waterfall-container"><div v-for="(column, columnIndex) in columns" :key="columnIndex" class="waterfall-column"><div v-for=&q…

如何用反射调用泛型类的方法

例子一:泛型类不含构造函数using System; using System.Reflection;namespace 使用反射调用泛型类的方法 {class Program{static void Main(string[] args){//定义要使用的类型参数(就是调用方法时要传入的参数类型,例如int)Type genericTypeArgument = typeof(int);//获取…

[日志分析篇]-利用ELK分析jumpserver日志-日志拆分篇

需要通过elk日志分析平台接收jumpserver日志,对日志进行过滤和拆分。并通过Grafana进行企业微信告警推送和大屏展示1.系统介绍名称 软件版本jumpserver jumpserver-3.10.13-tlselasticsearch elasticsearch-8.12.2kibana kibana-8.12.2logstash logstash-8.12.2granfa Grafana…

IDEA运行不了代码

同样的代码在eclipse可以正常运行,创建的方法也保持一样,为什么在IDEA就报错,本人是小白看不懂下面的报错

扩展被恢复分区挡住的 C 盘

之前装系统的时候想着以后要不要装个 Ubuntu 以作备用,所以给 SSD 分区的时候留了一小部分,没有全分给 C 盘。结果后来用 WSL 用得乐不思蜀了,觉得剩下的空间留着没必要,于是想把剩下的空间扩容给 C 盘。结果操作的时候发现 C 盘后面跟了一个恢复分区!无法给 C 盘扩容了。…

若依开启注册功能

若依开启用户注册功能 1、修改数据库,如下:2、修改前端:参考鸣谢: https://blog.csdn.net/weixin_43684214/article/details/121609310

OCR视图识别(Tess4J)

1.概述 图片文字识别 OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程 2.Tess4j快速入门 1.导入依赖<dependencies><…

『模拟赛』多校A层冲刺NOIP2024模拟赛12

『模拟赛记录』多校A层冲刺NOIP2024模拟赛12Rank 挂了不少,还行A. Alice 和璀璨花 签。 一眼最长上升子序列,昨天在 AT 专题里刚见过,不过赛时没想到离散化之后树状数组,所以打的动态开点,结果细节挂了 30pts。 和最长上升子序列思路基本一致,直接区间查询 \([1,a_i-1]\)…

分享一些利用商品详情数据挖掘潜在需求的成功案例

以下是一些利用商品详情数据挖掘潜在需求的成功案例: 一、亚马逊的个性化推荐系统:案例背景:亚马逊是全球知名的电商平台,拥有海量的商品和庞大的用户群体。为了提高用户的购物体验和增加销售额,亚马逊投入大量资源开发个性化推荐系统。 数据挖掘过程:亚马逊通过分析用户…