Skip to main content

Posts

Questions of C Program

Recent posts

To Find Factorial Of A Number Using C Program

  Program 1: Factorial program in c using for loop #include <stdio.h> int main (){ int i , f = 1 , num ;   printf ( "Enter a number: " ); scanf ( "%d" ,& num );   for ( i = 1 ; i <= num ; i ++) f = f * i ;   printf ( "Factorial of %d is: %d" , num , f ); return 0 ; } Result Enter a number: 8 Factorial of 8 is: 40320 Program 2: Factorial program in c using pointers #include <stdio.h>   void findFactorial ( int , int *); int main (){ int i , factorial , num ;   printf ( "Enter a number: " ); scanf ( "%d" ,& num );   findFactorial ( num ,& factorial ); printf ( "Factorial of %d is: %d" , num ,* factorial );   return 0 ; }   void findFactorial ( int num , int * factorial ){ int i ;   * factorial = 1 ;   for ( i = 1 ; i <= num ; i ++) * factorial =* factorial * i ; } Result Enter a number: 8 Factorial of 8 is: 40320 Program 3: Factorial progra...

C Program to Check Whether a Number is Prime or Not

  In this example, you will learn to check whether an integer entered by the user is a prime number or not. To understand this example, you should have the knowledge of the following  C programming  topics: C if...else Statement C for Loop C break and continue A prime number is a positive integer that is divisible only by  1  and itself. For example: 2, 3, 5, 7, 11, 13, 17 Program to Check Prime Number # include <stdio.h> int main () { int n, i, flag = 0 ; printf ( "Enter a positive integer: " ); scanf ( "%d" , &n); for (i = 2 ; i <= n / 2 ; ++i) { // condition for non-prime if (n % i == 0 ) { flag = 1 ; break ; } } if (n == 1 ) { printf ( "1 is neither prime nor composite." ); } else { if (flag == 0 ) printf ( "%d is a prime number." , n); else printf ( "%d is not a prime number." ,...

C program to find prime factors of a number

  Write a C program to input a number from user and find Prime factors of the given number using loop. C program to list all prime factors of a given number. Logic to find prime factors of a number in C programming. Example Input Input any number: 10 Output Prime factors of 10: 2, 5 Required knowledge Basic C programming ,  If statement ,  For loop ,  Nested loop What is Prime factor? Factors of a number  that are  prime numbers  are called as Prime factors of that number. For example: 2 and 5 are the prime factors of 10. Logic to check prime factors of a number Step by step descriptive logic to find prime factors. Input a number from user. Store it in some variable say  num . Run a loop from  2  to  num/2 , increment 1 in each iteration. The loop structure should look like  for(i=2; i<=num/2; i++) . You may think why loop from 2 to  num/2 ? Because prime number starts from 2 and any factor of a number  n  is ...