Sum of even numbers in c

  • Please check this below program which prints even numbers from 1 to n.
  • Print even numbers in c 
  • To print sum of even numbers from 1 to n first we need to get all the even numbers and add them.
  • Using for or while loop iterate from 1 to n number and get all even numbers
  • sum=sum+evenNumber
  • C program to find sum of even numbers using while loop

sum of even numbers in c

C program to find sum of even numbers using while loop

  1. #include<stdio.h> 
  2. //write a program to print even numbers in c using while loop
  3. //www.instanceofjava.com
  4. int main()
  5. {
  6.     // declare variables 
  7.     int number,i,sum=0;
  8.     //read input from user 
  9.     printf("Enter a number: ");
  10.     scanf("%d", &number);

  11.    //iterate and check if it a even number or not 
  12.      i=1;
  13.        
  14.         while(i<=number){
  15.                 
  16.         if(i%2 ==0){
  17.         
  18.           printf("%d\n" ,i);
  19.            sum=sum+i;    
  20.         }
  21.           i++  ;  
  22.         }
  23.          printf("sum of all even numbers = %d",sum);
  24.          
  25.    getch();
  26. }


Output:
  1. Enter a number:8
  2. 2
  3. 4
  4. 6
  5. 8
  6. sum of all even numbers= 20


Print even numbers in c

  • Lets see how to write a c program to print even numbers using c programming.\
  • Its a basic example to print only even numbers 
  • Use for loop and iterate from 1 to n.
  • In each iteration check if number if divisible by 2 (n%2 == 0). if yes then its a even number otherwise it is a odd number.
  • C program to print even numbers from 1 to n. 
print even numbers in c


  1. #include<stdio.h> 
  2. //write a program to print even numbers in c 
  3. //www.instanceofjava.com
  4. int main()
  5. {
  6.     // declare variables 
  7.     int number,i;
  8.     //read input from user 
  9.     printf("Enter a number: ");
  10.     scanf("%d", &number);

  11.    //iterate and check if it a even number or not 
  12.   
  13.     for (i=1; i<=number; i++){
  14.         
  15.         if(i%2 ==0){
  16.           printf("%d\n" ,i);
  17.                
  18.         }
  19.               
  20.         }
  21.         
  22.    getch();
  23. }


Output:
  1. Enter a number: 12
  2. 2
  3. 4
  4. 6
  5. 8
  6. 10
  7. 12


#2 print even numbers in c using while loop

  • Now we will try to print even numbers using while loop
  • Only difference is replace for with while loop
  • Variable initialization will be done before loop
  • condition will be inside while(condition)
  • increment or decrement will be last line of while block 
  • Lets print even numbers using while loop from 1 to n.


  1. #include<stdio.h> 
  2. //write a program to print even numbers in c using while loop
  3. //www.instanceofjava.com
  4. int main()
  5. {
  6.     // declare variables 
  7.     int number,i;
  8.     //read input from user 
  9.     printf("Enter a number: ");
  10.     scanf("%d", &number);

  11.    //iterate and check if it a even number or not 
  12.      i=1;
  13.        
  14.         while(i<=number){
  15.                 
  16.         if(i%2 ==0){
  17.           printf("%d\n" ,i);
  18.                
  19.         }
  20.           i++  ;  
  21.         }
  22.         
  23.    getch();
  24. }


Output:
  1. Enter a number: 20
  2. 2
  3. 4
  4. 6
  5. 8
  6. 10
  7. 12
  8. 14
  9. 16
  10. 18
  11. 20


Sum of digits of a number in c

  • To find sum of digits of a number in c , we need to get individual numbers and add those numbers.
  • First of all read a number from user using scanf.
  • Using while loop get individual number 
  • Divide the number with 10 so we will get last digit of that number as remainder , once we get that last digit then add it to one variable and divide that number with 10 so it will become one digit less.
  • like this we can get all digits and add all those numbers to get sum of digits 
  • Sum of individual digits in c program
  • c program to find sum of digits of a 3 digit number
sum of digits in c

correction: here n =number .

# program to find sum of digits of a number in c

  1. #include<stdio.h> // include stdio.h
  2. //c program to find sum of digits of a number using while loop
  3. //write a program to find sum of digits of a number
  4. //www.instanceofjava.com
  5. int main()
  6. {
  7.     // declare variables 
  8.     int number, remainder, sum = 0;
  9.     //read input from user 
  10.     printf("Enter a number: ");
  11.     scanf("%d", &number);

  12.    //iterate until number not equal to zero
  13.    // get individual digits and add all those digits to get sum
  14.     while(number != 0)
  15.     {
  16.         remainder = number % 10;
  17.         sum += remainder;
  18.         number = number / 10;
  19.     }
  20.   //print sum of digits in c
  21.     printf("sum = %d", sum);

  22.     getch();
  23. }


Output:
  1. Enter a number
  2. 123
  3. sum = 6


C Program to find sum of two numbers

 Lets discuss a basic c program

  • Sum of two integers using c. This might be the first practice program for all users.
  • To check sum of two integers , read input from user and store in two corresponding integer variables.
  • Also take one more variable to store result of sum of two numbers
  • c= a+b
  • Print result using printf

sum of two numbers in c


  1. #include <stdio.h>
  2. // program to check sum of two numbers in c
  3. // write a c program to find sum of two integers
  4. // www.instanceofjava.com
  5. int main()
  6. {
  7.     int x, y, sum;
  8.     // read input from user
  9.     printf(" Please enter any two numbers: ");
  10.     scanf("%d %d", &x, &y);
  11.     // sum of two numbers
  12.     sum=x+y;
  13.     
  14.     printf("Sum of %d and %d is %d", x, y, sum);

  15.     getch();
  16. }
Output:

  1. Please enter any two numbers
  2. 10
  3. 20
  4. Sum of 10 and 20 is 30

Gcd of two numbers in c

  • To check GCD of two numbers first we need to read input from user. i.e ask user to enter two numbers
  • Store those two numbers in two integer variables
  • Check if number is factor of given two numbers by iterating in for loop.

gcd of two numbers in c


  1. #include <stdio.h>
  2. // program to check gcd of two numbers in c 
  3. // write a c program to find gcd of two integers
  4. // www.instanceofjava.com
  5. int main()
  6. {
  7.     int number1, number2, i, gcd;
  8.     // read input from user
  9.     printf(" please enter any two numbers: ");
  10.     scanf("%d %d", &number1, &number2);

  11.     for(i=1; i <= number1 && i <= number2; ++i)
  12.     {
  13.         // checking  if i is divisible by both numbers / factor of both numbers
  14.         if(number1%i==0 && number2%i==0)
  15.             gcd = i;
  16.     }

  17.     printf("GCD of %d and %d is %d", number1, number2, gcd);

  18.     getch();
  19. }


Output:

gcd of two integers in c




  1. #include <stdio.h>
  2. // Function to compute Greatest Common Divisor using Euclidean Algorithm
  3. int gcd(int a, int b) {
  4.     while (b != 0) {
  5.         int temp = b;
  6.         b = a % b;
  7.         a = temp;
  8.     }
  9.     return a;
  10. }

  11. int main() {
  12.     int num1, num2;

  13.     // enter  two numbers from the user
  14.     printf("Enter two numbers to find their GCD: ");
  15.     scanf("%d %d", &num1, &num2);

  16.     // Ensure numbers are positive
  17.     if (num1 < 0) num1 = -num1;
  18.     if (num2 < 0) num2 = -num2;

  19.     // Calculate and display the GCD
  20.     int result = gcd(num1, num2);
  21.     printf("The GCD of %d and %d is: %d\n", num1, num2, result);

  22.     return 0;
  23. }


Explanation:

  1. Input:
    • The user enters two numbers.
  2. Logic:
    • The findGCD function uses the Euclidean algorithm:
      • Replace aa with bb and bb with a%ba \% b until b=0b = 0.
      • The final value of aa is the GCD.
  3. Output:
    • The program prints the GCD of the two input numbers.



C program to check whether the triangle is right angled triangle

 c program to check whether the triangle is right angled triangle

  • To check right angle triangle or not first we need to check sum of all three angles 
  • If sum of all three angles is 180 then we it should satisfy second condition also
  • Second condition is on of the angle should be 90.
  • if both the conditions are passed then we can say it is a right angle triangle.
  • Now lets write a c program to check triangle is right angle triangle or not.
right angle triangle in c


  1. #include<stdio.h>
  2. // write a c program too check traingle is right angle triasngle or not
  3. //www.instanceofjava.com
  4. int main()
  5. {
  6.      int angle1,angle2,angle3;
  7.      printf("Enter Three Angles of Triangle");
  8.      printf("\n-------------------------------\n");
  9.      printf("Enter Angle1  : ");
  10.      scanf("%d", &angle1);
  11.      printf("\nEnter  Angle2 : ");
  12.      scanf("%d",&angle2);
  13.      printf("\nEnter  Angle2  : ");
  14.      scanf("%d",&angle3);
  15.      printf("--------------------------------\n");
  16.      if(angle1+angle2+angle3==180) 
  17.      {
  18.          
  19.           if(angle1==90 || angle2==90 || angle3==90)
  20.           {
  21.                printf("\nTriangle is Right Angle Triangle\n"); //
  22.           }
  23.          
  24.      }
  25.      else
  26.      {
  27.           printf("\nIts Not a Triangle ");
  28.      }
  29.     getch();
  30. }


Select Menu