Computer Science » C Program » Find sum of Two Integer Numbers Program : [php]#include<stdio.h> int main() { int a, b, sum; printf("Enter the two numbers : "); scanf("%d %d", &a, &b); sum = a + b; printf("nSum of %d and %d is : %d", a,b,sum); return(0); }[/php] Output : [php]Enter the two numbers : 2 4 Sum of 2 and 4 is : 6[/php]
Computer Science » C Program » Fibonacci Series Starting from Any Two Numbers Program : [php]#include<stdio.h> int main() { int first, second, sum, num, counter = 0; printf("Enter the term : "); scanf("%d", &num); printf("nEnter First Number : "); scanf("%d", &first); printf("nEnter Second Number : "); scanf("%d", &second); printf("nFibonacci Series : %d %d ", first, second); while (counter < num) { sum = first + second; printf("%d ", sum); first = second; second = sum; counter++; } return (0); }[/php] Output : [php]Enter the term : 5 Enter First Number : 1 Enter Second Number : 3 Fibonacci Series : 1 2 3 5 8 13 21[/php]
Computer Science » C Program » Calculate Sum of Odd and Even Numbers Program : [php]#include <stdio.h> int main() { int i, n; int sum_odd = 0, sum_even = 0; printf("Enter the number up to which you want to calculate sum : "); scanf("%d", &n); for (i = 1; i <= n; i++) { if (i % 2 == 0) sum_even = sum_even + i; else sum_odd = sum_odd + i; } printf("nSum of all even numbers is : %d", sum_even); printf("nSum of all odd numbers is : %d", sum_odd); }[/php] Output : [php]Enter the number up to which you want to calculate sum : 100 Sum of all even numbers is : 2550 Sum of all odd numbers is : 2500[/php]
Computer Science » C Program » Calculate Sum and Average of an Array Program : [php]#include <stdio.h> int main() { int Arr[50]; int n, i, sum = 0; float average; printf("Enter the number of elements in Array : "); scanf("%d", &n); for (i = 0; i < n; i++) { printf("nEnter element %d : ", i + 1); scanf("%d", &Arr[i]); sum = sum + Arr[i]; } average = (float)sum/n; printf("nThe sum of the elements array is : %d", sum); printf("nThe average of the elements array is : %0.2f", average); return 0; }[/php] Output : [php]Enter the number of elements in Array : 5 Enter element 1 : 20 Enter element 2 : 30 Enter element 3 : 40 Enter element 4 : 50 Enter element 5 : 60 The sum of the elements array is : 200 The average of the elements array is : 40.00[/php]