Different programs for different questions.

Different programs for different questions. - C - Programmation

Marsh Posté le 13-09-2019 à 08:52:20    

Syntax of for loop:
for (initializationStatement; testExpression; updateStatement)
{
    // statements inside the body of loop
}
 
// To print numbers from 1 to 10
#include <stdio.h>
int main()  
{
  int i;
  for (i = 1; i < 11; ++i)
  {
    printf("%d ", i);
  }
  return 0;
}
 
 
// Program to calculate the sum of first n natural numbers
#include <stdio.h>
int main()
{
    int num , count, sum = 0;
    printf("Enter a positive integer: " );
    scanf("%d", & num);
    // for loop terminates when num is less than count
    for(count = 1; count <= num; ++count)
    {
        sum += count;
    }
    printf("Sum = %d", sum);
    return 0;
}

Reply

Marsh Posté le 13-09-2019 à 08:52:20   

Reply

Marsh Posté le 13-09-2019 à 09:29:24    

1. What is the purpose of this topic ?
2. There's a shorter way to calculate the sum of first n natural numbers :
 

Code :
  1. sum = ((n*n)+n)/2


---------------
J'ai un string dans l'array (Paris Hilton)
Reply

Marsh Posté le 14-09-2019 à 08:52:33    

Vu la signature tronquée et invisible, je dirais que c'est de poster des liens de spams mais en n'étant pas doué :o


---------------
Réalisation amplis classe D / T      Topic .Net - C# @ Prog
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed