+ 1

If i want to add first positive 15 numbers what should i code using while loop ()

Using while loop If some one can then help me thanks

3rd Feb 2021, 12:52 PM
Muhammad Hassan
Muhammad Hassan - avatar
4 Réponses
+ 3
Yes , you can use while/for loop for that ....
3rd Feb 2021, 12:57 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
Declare one variable for storing sum and one variable for control while loop and that same variable can be used to calculate sum of first 15 numbers. C program for adding first 15 positive number is like this #include<stdio.h> int main() { int sum=0; //for storing addition int i=1; //for control while loop while(i<16) { sum=sum+i; i++; } printf ("%d",sum) return 0; }
3rd Feb 2021, 1:03 PM
Hrutik Bhalerao
Hrutik Bhalerao - avatar
+ 3
For loop a bit more straightforward for this but while loop can do it.
3rd Feb 2021, 1:07 PM
Sonic
Sonic - avatar
+ 2
int i=0 int sum=1 while(i<=15) { sum=sum+i; i++; }
3rd Feb 2021, 1:00 PM
Abhay
Abhay - avatar