programming like c, c++, java,python learn in free

Thursday, June 4, 2020

C programming Question and Answer Day-12

56. Add 'n' numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
   int n, sum=0, i, value;
   clrscr();

   printf("Enter total numbers you want to add : ");
   scanf("%d", &n);

   for (i=1; i<=n; i++)
   {
      printf("Enter number %d : ", i);
      scanf("%d", &value);
      sum = sum + value;
   }

   printf("Sum of entered numbers : %d", sum);
   getch();
}

57. Add 'n' numbers using array.

#include<stdio.h>
#include<conio.h>
void main()
{
    int n, sum = 0, i, array[100];
    clrscr();
    printf("Enter total numbers you want to add : ");
    scanf("%d", &n);

    for (i = 1; i <= n; i++)
    {
        printf("Enter number %d : ", i);
        scanf("%d", &array[i]);
        sum = sum + array[i];
    }
    printf("Sum : %d\n", sum);
    getch();
}

58. Program to accept a number and add the digits of that number.

#include<stdio.h>
#include<conio.h>
void main()
 {
 int n, sum = 0, remainder;
 clrscr();

 printf("Enter the number : ");
 scanf("%d", &n);

 while (n != 0) 
 {
  remainder = n % 10;
  sum = sum + remainder;
  n = n / 10;
 }

 printf("Sum of digits of entered number : %d", sum);
 getch();
}

59. Program to accept a number and add the digits of that number using recursion.

#include<stdio.h>
#include<conio.h>

int add_digits(int);

void main()
{
    int n, result;
    clrscr();
    printf("Enter a number : ");
    scanf("%d", &n);

    result = add_digits(n);

    printf("Sum : %d", result);
    getch();
}

int add_digits(int n)
{
    static int sum = 0;
    if (n == 0)
    {
        return 0;
    }
    sum = n % 10 + add_digits(n / 10);
    return sum;
}

60. Average of numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
    int n, i;
    float sum=0, x, avg;
    clrscr();
    printf("Enter total Numbers : ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        printf("\nNumber %d : ", i );
        scanf("%f", &x);
        sum += x;
    }
    avg = sum / n;
    printf("\nThe Average is : %0.2f", avg);    
    getch();
}
Share:

0 comments:

Post a Comment

Unordered List

recentposts1
Powered by Blogger.

Text Widget

Featured Post

C programming Question and Answer Day-15

71. Program to check Niven number (Harshad number). #include<stdio.h> #include<conio.h> void main() {     int n, d, a, su...

Search This Blog

recent comments

recentcomments

Labels

[slideshow][technology]

vertical posts

[verticalposts][technology]

business

[business][grids]

ad space

ads 600

vehicles

[cars][stack]
[verticalposts][food]

our facebook page

about us

logo

Jupiter is a magazine responsive Blogger template. It has everything you need to make your blog stand out. This template is fully customizable and very flexible and we believe you will love it as much as we do.

Slide show

[people][slideshow]

health

[health][btop]

recent posts

recentposts1

Subscribe Us

random posts

randomposts2

Blog Archive

Recent Posts

top ads

Unordered List

recent

Pages

Theme Support

[socialcounter] [facebook][#][215K] [twitter][#][115K] [youtube][#][215,635] [rss][#][23M] [linkedin][#][21.5K] [instagram][#][600,300]