Some simple C programs

1.

#include<stdio.h>/*include standard library stdio.h for use of and printf()*/
#include<string.h>/*include standard library string.h for use of strlen()and gets() putchar()*/
#include<ctype.h>/*include standard library ctype.h for use of toupper()*/
int main()/* define a function named main */
{
    int i,j,n,p,sum=0;
    char name[100];
    printf("please enter your full name\n");
    gets(name);/*Read a full name with spaces from the keyboard*/
    printf("%s\n",name);
    n=strlen(name);/*Determine the length of the string*/
    for(i=0;i<n;i++)/*Determine the length of the string excluding white space*/
    {
        if(name[i]==' ')
        {
            for(j=i;j<n-1;j++)
                name[j]=name[j+1];
                name[n-1]='\0';
                n--;
        }
    }
   printf("The string has %d characters not including spaces: %s\n",n,name);
   for(p=n-1;p>=0;p--)/*Print the string on the screen in reverse*/
    putchar(toupper(name[p]));
    printf("\n");
    for(int p=0;p<=n;p++)/*Add and subtract alternate ASCII values of each character */
    {
       if(p%2==0){
        name[p]=toupper(name[p]);
        printf("+");}
       else
        name[p]=-toupper(name[p]);
       sum+=name[p];
    printf("%d",name[p]);

    }
    printf("=%d",sum);
    return 0;
}

 2.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,sum=0,m,j;
    char array[8];
    printf("please enter an 8-bit binary number\n");
        gets(array);/*Read an 8-bit binary number from the keyboard*/
        for(i=0;i<8;i++){/*Convert the binary to its decimal equivalent.*/
            m=1;
            if(array[i]=='1'){
                for(j=1;j<=8-i-1;j++)
                    m*=2;
                sum+=m;
            }
        }
        printf("Your input 8-bit binary number is:");
        for(int p=0;p<8;p++)
            printf("%c",array[p]);
    printf("\nThe binary value is %d in decimal\n",sum);
    printf("The binary value is %X in hexadecimal",sum);/*Convert the binary to its hexadecimal equivalent.*/
    return 0;
}

 3.

/*Name:Simple Program for Average, Standard Deviation, the array in ascending order
File Name:E3.c
Copyright:Free
Author:Wuwei Han
Description:Compute and display the Average, Standard Deviation, the array in ascending order
*/
#include<math.h>/*include standard library math.h for use of pow(x,y),sqrt()*/
#include <stdio.h>/*include standard library stdio.h for use of scanf() and printf()*/
#include <stdlib.h>
float calculateSD(float a[]);/*define a function calculateSD with assignment a[i]*/
int main()/* define a function named main */
{
    float a[5],sum1=0.0,temp;/* declare three variables of type of float namely a[5],sum1=0.0,temp*/
    printf("please write 5 real numbers between 0 and 10, but not 0\n");/* ask user to input a real number representing the 5 input real number*/
    for(int i=0;i<5;i++){
    scanf("%f",&a[i]);/* store the value of the input real number into
variable a[i] using the scanf function and for loop*/
    sum1+=a[i];
    }


    printf("The average value is:%.2f\n",sum1/5);/*display the average value at the given input*/
    printf("The standard deviation is:%.2f\n",calculateSD(a));/*display the standard deviation at the given input*/
    for(int j=0;j<5-1;j++)/*The first n-1-i are compared in each round, i.e. the last i already sorted are not compared*/
        {
        for(int p=0;p<5-1-j;p++){
            if(a[p]>a[p+1]){
                temp=a[p];
                a[p]=a[p+1];
                a[p+1]=temp;/*Sorting input arrays*/
            }
        }
    }
printf("The sorted array is:%g\n",a[0]);
    for(int j=1;j<5;j++)
        printf("                    \%g\t\n",a[j]);/*display the sorted array at the given input*/

}
float calculateSD(float a[])/*an explanation of the function calculateSD*/
{
    float sum=0.0,mean,standardDeviation=0.0;
    for(int m=0;m<5;++m)
        sum+=a[m];
    mean=sum/5;
    for(int q=0;q<5;++q)
        standardDeviation+=pow(a[q]-mean,2);
     return sqrt(standardDeviation/5);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值