sum of n numbers in c using for loop

 Here is example of a C program that uses a for loop to calculate the sum of 'n' numbers:

  1. #include <stdio.h>

  2. int main() {
  3.     int n, i, num, sum = 0;

  4.     printf("Enter the value of n: ");
  5.     scanf("%d", &n);

  6.     for (i = 1; i <= n; i++) {
  7.         printf("Enter the number: ");
  8.         scanf("%d", &num);
  9.         sum += num;
  10.     }

  11.     printf("The sum of %d numbers is %d\n", n, sum);

  12.     return 0;
  13. }

In this program, first, the user is prompted to enter the value of 'n' (the number of numbers to be added). Then, a for loop is used to iterate 'n' times and prompt the user to enter a number in each iteration. The variable 'sum' is initialized to 0 and it's being used to keep the sum of all numbers entered by the user. In each iteration, the value of 'num' is added to 'sum' using the += operator. Finally, the program prints the sum of 'n' numbers.

In this example, the for loop starts with i=1, and it will run as long as i <= n, with i being incremented by 1 in each iteration.

It's important to note that, if you want to input the n numbers at once, you can use an array and use a for loop to iterate over the array and add the numbers to the sum variable.

This C program uses a for loop to calculate the sum of 'n' numbers by prompting the user to enter a number in each iteration of the loop, adding it to a running sum, and finally printing the total sum at the end. This is a simple and efficient way to calculate the sum of multiple numbers.

c program for addition, subtraction, multiplication and division using switch

 C program for addition, subtraction, multiplication and division using switch


  1. #include <stdio.h>

  2. int main() {
  3.     int num1, num2, choice;
  4.     float result;
  5.     printf("Enter two numbers: ");
  6.     scanf("%d %d", &num1, &num2);
  7.     printf("Enter your choice: \n1 for addition\n2 for subtraction\n3 for multiplication\n4 for division\n");
  8.     scanf("%d", &choice);

  9.     switch(choice) {
  10.         case 1:
  11.             result = num1 + num2;
  12.             printf("Addition: %.2f\n", result);
  13.             break;
  14.         case 2:
  15.             result = num1 - num2;
  16.             printf("Subtraction: %.2f\n", result);
  17.             break;
  18.         case 3:
  19.             result = num1 * num2;
  20.             printf("Multiplication: %.2f\n", result);
  21.             break;
  22.         case 4:
  23.             result = (float)num1 / num2;
  24.             printf("Division: %.2f\n", result);
  25.             break;
  26.         default:
  27.             printf("Invalid choice!\n");
  28.     }
  29.     return 0;
  30. }


  • the user is prompted to enter two numbers and then a choice for the operation to be performed. The choice is taken as an integer input and is used in the switch statement. 
  • Depending on the user's choice, the program performs the corresponding operation using the two numbers as input.
  • It's worth noting that, in the division operation, if the second number is zero, it will cause a runtime error because division by zero is not defined. 
  • I have used type casting to convert the int variables to float so as to get the decimal value. Also, I have used the format specifier %.2f to print the result with 2 decimal places.
  • the user is prompted to enter two numbers and then a choice for the operation to be performed. The choice is taken as an integer input using the scanf function and is used in the switch statement. Depending on the user's choice, the program performs the corresponding operation using the two numbers as input.
  • you can try same program try to practice this using function:  c program for addition subtraction, multiplication and division using function

  • The switch statement checks for the value of the variable choice. Depending on the value, the program enters the corresponding case and performs the operation. The break statement is used at the end of each case to exit the switch statement and prevent the program from executing the next case.
  • In the case of the division operation, I have used type casting to convert the int variables to float so as to get the decimal value. Also, I have used the format specifier %.2f to print the result with 2 decimal places.

  • In the default case, if the user enters any value other than 1, 2, 3, or 4, the program displays an "Invalid choice!" message. This is to handle the scenario when the user enters an unexpected value.

c program addition subtraction, multiplication and division using function

 c program for addition subtraction, multiplication and division using function

  1. #include <stdio.h>

  2. int add(int a, int b) {
  3.     return a + b;
  4. }

  5. int subtract(int a, int b) {
  6.     return a - b;
  7. }

  8. int multiply(int a, int b) {
  9.     return a * b;
  10. }

  11. float divide(int a, int b) {
  12.     return (float)a / b;
  13. }

  14. int main() {
  15.     int a = 10, b = 5;
  16.     int add_result = add(a, b);
  17.     int subtract_result = subtract(a, b);
  18.     int multiply_result = multiply(a, b);
  19.     float divide_result = divide(a, b);

  20.     printf("Addition: %d + %d = %d\n", a, b, add_result);
  21.     printf("Subtraction: %d - %d = %d\n", a, b, subtract_result);
  22.     printf("Multiplication: %d * %d = %d\n", a, b, multiply_result);
  23.     printf("Division: %d / %d = %f\n", a, b, divide_result);

  24.     return 0;
  25. }


  • In this program, four functions are defined for addition, subtraction, multiplication, and division respectively. Each function takes two integer arguments a and b, performs the corresponding operation, and returns the result. 
  • In the main function, variables a and b are initialized with the values 10 and 5 respectively.
  • The functions are called and the results are stored in variables add_result, subtract_result, multiply_result, and divide_result. 
  • These functions take two integer arguments and return the result of the operation. In the main() function, two integers are taken as input, and then the functions are called with these inputs to perform the operations and display the results.
  • In the division operation, if the second number is zero, it will cause a runtime error because division by zero is not defined. 
  • It's also important to consider the case where the division operation results in decimal values, In that case, you should use float or double instead of int.

Ascii values in c

 In C, you can use the int data type to represent ASCII values. Each character in the ASCII table is assigned a unique number between 0 and 127. To get the ASCII value of a character, you can simply cast it to an int. Here's an example:

char c = 'A';

int asciiValue = (int)c;

printf("The ASCII value of '%c' is %d\n", c, asciiValue);

This will output:

The ASCII value of 'A' is 65

Alternatively you can also use the 'C' library function (int)c to get the ASCII value of a character.

char c = 'A';

int asciiValue = (int)c;

printf("The ASCII value of '%c' is %d\n", c, asciiValue);

will give you the same output.

You can also find the ASCII value of a character with a function like getchar(), that returns an int value of the char type that it reads from the keyboard, or with a function like scanf() that reads a character input.

Additionally, if you want to find the ASCII value of a string, you can use a for loop to iterate through each character of the string and then get the ASCII value of each character by casting it to an int.



C program to print table of 2 using for loop


#include <stdio.h>

int main() {

    int i;

The first line includes the standard input/output header file, which is needed for the printf function used later in the program.

The main function is the entry point for the program. It is the first function that is executed when the program runs.

The next line declares a variable i of type int. This variable will be used as the loop counter in the for loop.

    for (i = 1; i <= 10; i++) {

        printf("2 * %d = %d\n", i, 2 * i);

    }

This is the for loop. It starts with the for keyword, followed by parentheses that contain three statements separated by semicolons:

The initialization statement i = 1 sets the value of i to 1 before the loop starts.

The loop condition i <= 10 specifies that the loop will continue as long as the value of i is less than or equal to 10.

The iteration statement i++ increments the value of i by 1 after each iteration of the loop.

Inside the loop, the printf function is used to print a string to the console. The string contains two %d format specifiers, which are placeholders for two integer values that will be printed. The first %d is replaced by the value of i, and the second %d is replaced by the result of 2 * i. The \n at the end of the string is a newline character that causes the output to be printed on a new line.

Finally, the return 0; statement at the end of the main function indicates that the program has completed successfully.

C program to find the largest element in an array

C program that finds the largest element in an array :

  1. #include <stdio.h>
  2. int main() {
  3.   int n, i;
  4.   long long int t1 = 0, t2 = 1, nextTerm;

  5.   printf("Enter the number of terms: ");
  6.   scanf("%d", &n);

  7.   printf("Fibonacci Series: ");

  8.   for (i = 1; i <= n; ++i) {
  9.     printf("%lld, ", t1);
  10.     nextTerm = t1 + t2;
  11.     t1 = t2;
  12.     t2 = nextTerm;
  13.   }

  14.   return 0;
  15. }

  • This program first reads in n, the number of elements in the array. It then reads in n elements from the user and stores them in the array. 
  • Finally, it iterates through the array and compares each element to the first element (which is initially set to the largest). 
  • If it finds a larger element, it updates the value of the first element to be the larger value. At the end, the program prints out the largest element in the array.


  • This program first initializes an array a with 10 elements and assigns the value of the first element to the variable max.
  • It then loops through the array and compares each element with max. 
  • If an element is larger than max, max is updated with the new value. After the loop finishes, max will contain the largest element in the array.

Fibonacci series program in c

Here is a simple program that prints out the first n numbers in the Fibonacci series in C:

  1. #include <stdio.h>
  2. int main() {
  3.   int n, i;
  4.   long long int t1 = 0, t2 = 1, nextTerm;

  5.   printf("Enter the number of terms: ");
  6.   scanf("%d", &n);

  7.   printf("Fibonacci Series: ");

  8.   for (i = 1; i <= n; ++i) {
  9.     printf("%lld, ", t1);
  10.     nextTerm = t1 + t2;
  11.     t1 = t2;
  12.     t2 = nextTerm;
  13.   }

  14.   return 0;
  15. }

  • This program first prompts the user to enter the number of terms in the Fibonacci series to be printed. It then uses a for loop to iterate through the terms and prints each one. The loop starts at 1, and it continues until the value of i is greater than n. 
  • On each iteration of the loop, the next term in the series is calculated as the sum of the previous two terms, and the variables t1 and t2 are updated to store the previous two terms. The first two terms of the series are initialized to 0 and 1, respectively.
  • This program uses a for loop to iterate through the first 20 terms in the Fibonacci series. The variables t1 and t2 represent the previous two terms, and the variable nextTerm represents the next term in the series. 
  • The program prints out the value of t1 and then updates the values of t1, t2, and nextTerm for the next iteration of the loop.
Here is a C program that prints the first n numbers of the Fibonacci series:

fibonacci series in c

Enter the number of terms: 12
First 12 terms of Fibonacci series are:
0
1
1
2
3
5
8
13
21
34
55
89
  • This program prompts the user to enter a positive integer n and then generates the first n terms of the Fibonacci series using a loop.  with the first two terms being 0 and 1. The series starts with 0 and 1 and looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
  • This program prompts the user to enter the number of terms in the Fibonacci series they want to generate, then uses a loop to compute and print out each term. The first two terms of the series are hard-coded as 0 and 1, and the remaining terms are computed by adding the previous two terms together.
  • To understand how this program works, you can read the following explanation:
  • The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In this program, the first two numbers of the Fibonacci series are initialized to 0 and 1.
  • The for loop is used to iterate over the numbers of the series. The loop starts from 0 and ends at n-1. At each iteration, the next number in the series is calculated by adding the first and second numbers. The if statement is used to handle the case where the current iteration is 0 or 1, in which case the next number is simply the current iteration.
  • After the next number is calculated, the first and second numbers are updated for the next iteration. Finally, the next number is printed to the screen.
  • I hope this helps! Let me know if you have any questions.

Introduction to C language

Introduction to C


     C is a general purpose programming language. Programming language is the means to communicate with the system to get our thing done.

Need for a programming language:

  We all of us know system can only understand binary representation i.e. in the form of 0's and 1's.

Suppose we have to program which adds two numbers and save the result.

Steps involved:
  • First we should write the values in the memory.
  • Move those values into the registers.(registers are also a kind of memory where the CPU can perform operation on the values stored in the registers.)
  • Add the values in the registers.
  • Save the result in the register to the memory.

Machine Language                                                                                                                                                                                                                                                                               At the beginning machine code is used to write the program. 

Machine level programming consists of binary or hexadecimal instructions on which an processor can respond directly.  

Structure of machine language instruction.
Opcode   Operand 

Opcode represents the operation to be performed by the system on the operands.

In machine code:
Memory     opcode     operand 
location                                                                     
0000:           55            e4     10   // 55 represent move instruction, move 10 to location e4          
0001:           89            e5     20
0003:           83            e4     e5
0006:           54            c4     e4    

Its difficult to read and write opcode for every instruction. So instead of numerical value for instruction there should be an alternative, Then comes the assembly code.

Assembly language:

 Mnemonics are used to represent the opcode.
An Assembly code:
MOV  A, $20           // move value 20 to register A (MOV is a mnemonics)
MOV  B, $10           // move value 10 to register B
ADD   A, B              // Add value in register in A and B
MOV  #30, A           // move value in register A to address 30

Assemblers converts the assembly language into binary instructions.
Assembly language is referred to as low level language,
Assembler languages are simpler than opcodes. Their syntax is easier to read than machine language but as the harder to remember. So, their is a need for other language which is much more easy to read and write the code.
Then comes the C programming language.

C language

In C language:
 int main()
{
     int a,b,c;
     a=10;
     b=20;
     c=a+b;
}

Compiler converts the C language code into assembly language code.

C language is easier to write and read. 

C initialize an array

  • Array is collection of similar data items.
  • An array can hold multiple values of same type of data
  • In c programming language an array can be declared as datatype arrayName [ arraySize ]; 
  • In c , while declaring an array we need to specify type of array , name of array and size of the array.
  • One can directly initialize an array while declaring itself with some values
  • int arrayVariable[2]=[10,20];
  • int count[]=[1,2,3,4,5]
  • directly initialize an array of particular : count[3]=10
  • Let us discuss about how to initialize an array with some default values.
  • Using for loop or while loop we can iterate an array and by accessing each index we can assign values 
  • If we are not initializing an array and try to print all the values then it will print some garbage values.
  • c initialize array of ints

#1Write a C program to declare an array and print default values
  1. #include <stdio.h>
  2. // write a c program to explain how to initialize an array
  3. int main () {

  4.    int number[ 15 ]; 
  5.    int i,k;
  6.    
  7.    
  8.    for (k = 0; k < 15; k++ ) {
  9.       printf("Array[%d] = %d\n", k, number[k] );
  10.    }
  11.  
  12.   getch();
  13. }


Output:
  1. Array[0] = -1551778920
  2. Array[1] = -2
  3. Array[2] = 1971427546
  4. Array[3] = 1971495162
  5. Array[4] = 5189464
  6. Array[5] = 5181048
  7. Array[6] = 2686776
  8. Array[7] = 1971494685
  9. Array[8] = 0
  10. Array[9] = 0
  11. Array[10] = 2686832
  12. Array[11] = 200
  13. Array[12] = 192
  14. Array[13] = 7161720
  15. Array[14] = 48


#2 write a C Program to initialize an array values to 0 (zeros).

initialize an array in c


Output:
  1. Array[0] = 0
  2. Array[1] = 0
  3. Array[2] = 0
  4. Array[3] = 0
  5. Array[4] = 0
  6. Array[5] = 0
  7. Array[6] = 0
  8. Array[7] = 0
  9. Array[8] = 0
  10. Array[9] = 0
  11. Array[10] = 0
  12. Array[11] = 0
  13. Array[12] = 0
  14. Array[13] = 0
  15. Array[14] = 0

C program to check whether a character is vowel or consonant

  • In this  program we will discuss about how to check whether a character is vowel or consonant.
  • In English language will be having 5 vowels and 21 consonants.
  • To check entered character is vowel or consonant, first Read input character from user using scanf.
  • There are only 5 vowels(A,E,I,O,U), so we need to check entered character is there in those 5 characters.
  • If it is present then we can say its an vowel, otherwise it is a consonant.
  • While checking we need to consider case sensitivity. So we need to check both uppercase vowels and lower case vowels.
  • Lets see an example program to check whether entered character is vowel or not.

check vowel or consonant in c


Write a program to determine whether the input character is a vowel or consonant or not an alphabet

  1. #include <stdio.h>
  2. //write a program to determine whether the input character is a vowel or consonant or not an alphabet
  3. //c program to check whether a character is vowel or consonant
  4. //www.InstanceofJava.com
  5. int main() {
  6.     char character;
  7.     int lowercaseVowel, uppercaseVowel;
  8.     printf("Enter a character  ");
  9.     scanf("%c", &character);

  10.     // assigns 1
  11.     lowercaseVowel = (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u');

  12.     // evaluates to 1 if variable c is a uppercase vowel
  13.     uppercaseVowel = (character == 'A' || character == 'E' || character == 'I' || character == 'O' || character == 'U');

  14.     // evaluates to 1 (true) if c is a vowel
  15.     if (lowercaseVowel || uppercaseVowel)
  16.         printf("%c is a vowel.", character);
  17.     else
  18.         printf("%c is a consonant.", character);
  19.    getch();
  20. }


Output:
  1. Enter a character
  2. I
  3. I is an vowel


Print prime numbers from 1 to 100 in c

  • Lets discuss a program on how to print prime numbers from 1 to 100.
  • Here by default we need to print all prime numbers from 1 to 100.
  • No need to ask user to enter a number because we need to end loop at 100.
  • Using for loop iterate from 1 to 100
  • Take one more loop and check each number is divisible by 1 and itself or not.
  • Program to print prime number between 1 to 100 in c

#1.C program to print prime numbers from 1 to 100

  1. #include <stdio.h>

  2. // c program to print prime numbers from 1 to 100
  3. // www.instanceofjava.com
  4. int main()
  5. {
  6.    int n, i,k, count = 0;
  7.  
  8.  printf("Prime numbers from 1 to 100\n");
  9. for(i=2;i<=100;i++)
  10.  {
  11.   for(k=2;k<i;k++)
  12.   {
  13.    if(i%k==0)
  14.    break;
  15.    else if(i==k+1)
  16.    printf("%d\n",i);
  17. }
  18. }
  19.  
  20.     getch();
  21.     
  22. }
  23.     



prime numbers from 1 to 100



Sum of two integers in c

  •  Check here for printing an integer in c 
  • We have 4 basic data types in c programming language
  • int, char, float and double
  • For storing numbers or integers we will use int data type.
  • Lets see an example program sum of two integers in c programming language.
  • To add two integers we need two int type variables and third variable to store result of sum of two integers.
  • Its a basic level example program for beginners to understand integer data types

sum of two integers in c


Write a C program to add two integers / find sum of two integers in c

  1. #include<stdio.h>
  2. // c program to print integer using %d
  3. // c print integer
  4. //www.instanceofjava.com
  5. int main()
  6. {
  7.             //declare an integer variable 
  8. int number1, number2, sumOfTwoIntegers;
  9.             // read input from user using scanf
  10. printf ("Enter any two integers: ");
  11. scanf (" %d%d", &number1,&number2);
  12. //sum of two integers in c
  13. sumOfTwoIntegers=number1+number2;
  14.    //print the same number using %d
  15. printf("Sum of two integers : %d", sumOfTwoIntegers);
  16. getch();
  17. }
Output:
  1. Enter any two integers:
  2. 10
  3. 20
  4. Sum of two integers : 30

Print an integer in c language

  • For declaring numbers we use int data type in c.
  • In order to read using scanf we use %d.
  • using printf we can print same variable. 

print interger in c


#Write a C Program to print an integer 

  1. #include<stdio.h>
  2. // c program to print integer using %d
  3. // c print integer
  4. //www.instanceofjava.com
  5. int main()
  6. {
  7.             //declare an integer variable 
  8. int number;
  9.             // read input from user using scanf
  10. printf ("Enter an Integer: ");
  11. scanf (" %d", &number);
  12.    //print the same number using %d
  13. printf("You Entered : %d", number);
  14. getch();
  15. }

Output:
  1. Enter an Integer: 3
  2. You Entered : 3

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


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. }


Write a c program to check a number is palindrome or not:


check palindrome number in c

  • If reverse number of a number is same then we can say its a palindrome number
  • we already know logic to reverse a number using remainder logic
  • read input from the user and store it in a variable
  • check reverse number of given number 
  • compare original number and reverse number if both are same then we can say it's a palindrome number 
  • C Program to Check Whether a Number is Palindrome or Not


Write a c program to check a number is palindrome or not:

  1. #include <stdio.h>
  2. int main() {
  3.     int n, revnumber = 0, ramindernum, number;
  4.     printf("Enter number: ");
  5.     scanf("%d", &n);
  6.     number = n;

  7.     // reversed integer is stored in reversedN
  8.     while (n != 0) {
  9.         ramindernum = n % 10;
  10.         revnumber = revnumber * 10 + ramindernum;
  11.         n /= 10;
  12.     }

  13.     //  if number  and revnumber are same then its a palindrome
  14.     if (number == revnumber)
  15.         printf("%d is a palindrome.", number);
  16.     else
  17.         printf("%d is not a palindrome.", number);

  18.     getch();
  19. }


Output:

palindrome in c



Fibonacci series in c without recursion

  • In Fibonacci series the first two numbers in the Fibonacci sequence are 0 and 1 and each subsequent number is the sum of the previous two. For example Fibonacci series is 0, 1, 1, 2, 3, 5, 8,13, 21
  • C Program to Display Fibonacci Sequence 
  • Fibonacci series means next number will be generated as sum of previous two numbers.

#1: C example program on Fibonacci series

  1. #include <stdio.h>

  2. void main()
  3. {
  4.     int  fib1 = 0, fib2 = 1, fib3, limit, count = 0;

  5.     printf("Enter the limit to generate the Fibonacci Series \n");
  6.     scanf("%d", &limit);
  7.     printf("Fibonacci Series is ...\n");
  8.     printf("%d\n", fib1);
  9.     printf("%d\n", fib2);
  10.     count = 2;
  11.     while (count < limit)
  12.     {
  13.         fib3 = fib1 + fib2;
  14.         count++;
  15.         printf("%d\n", fib3);
  16.         fib1 = fib2;
  17.         fib2 = fib3;
  18.     }
  19. }
Output:
  1. Enter the limit to generate the Fibonacci Series
  2. 20
  3. Fibonacci Series is ...
  4. 0
  5. 1
  6. 1
  7. 2
  8. 3
  9. 5
  10. 8
  11. 13
  12. 21
  13. 34
  14. 55
  15. 89
  16. 144
  17. 233
  18. 377
  19. 610
  20. 987
  21. 1597
  22. 2584
  23. 4181


Select Menu