写一个简单的程序

写一个简单的程序

本文介绍了写一个简单的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写程序以输入名称和卷号以及两个主题的标记,然后找到总数和百分比,然后打印名称,卷号,总数和百分比。



我尝试了什么:



Write a Program to enter name and roll number and marks of two subject and then find the total and percentage then print the name , roll number, total and percent.

What I have tried:

/* Program Of School */
#include <stdio.h>
void main ()
{
 char Name[50];
 Int Roll, Mark1, Mark2;
 float per;
Printf("enter the name and roll number\n");
scanf("%c %d" , &name, &Roll);
printf("enter the marks of two subject");
scanf("%d %d" , &mark1, &mark2);
sum=mark1+mark2:
per=(mark1+mark2)/2.0;
printf("the name is %c and roll mo is %d, &name, &roll number);
printf("total marks are %d and percentage are %f, &sum, &per);
getch();
}

推荐答案



  1. scanf()您必须将指针作为参数传递以获取存储的值。但是使用 printf(),你必须传递整数值,如 int float )按值。

  2. %c格式序列适用于单个字符。使用%s获取字符串。

  3. 使用字符串( char arrays)对象已经是一个指针。所以你必须传递普通名称或第一个元素的地址(名称& Name [0] )。

  4. 您的百分比计算错误。通用公式是 100 * part / total


  1. With scanf() you have to pass pointers as arguments to get the values stored. But with printf() you have to pass integral values like int and float) by value.
  2. The "%c" format sequence is for a single character. Use "%s" for strings.
  3. With strings (char arrays) the object is already a pointer. So you have to pass the plain name or the address of the first element (Name or &Name[0]).
  4. Your percentage calculation is wrong. The general formula is 100 * part / total.


#include <stdio.h>
int main ()
{
  char name[50];
  int roll, mark1, mark2;
  int sum;
  double avg;
  printf("enter the name and roll number\n");
  scanf("%s %d" , name, &roll);
  printf("enter the marks of two subject\n");
  scanf("%d %d" , &mark1, &mark2);
  sum = mark1 + mark2;
  avg = (mark1 + mark2)/2.0;
  printf("the name is %s and roll mo is %d\n", name, roll );
  printf("total marks are %d and the average is %g\n", sum, avg);
  getchar();
  return 0;
}



这篇关于写一个简单的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:12