1. Question:Determining if a number is prime in c 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int num,i,count=0;
        printf("Enter a number: ");
        scanf("%d",&num);
        for(i=2;i<=num/2;i++){
            if(num%i==0){
             count++;
                break;
            }
        }
       if(count==0 && num!= 1)
            printf("%d is a prime number",num);
       else
          printf("%d is not a prime number",num);
       return 0;
    }
    Sample output: Enter a number: 5 5 is a prime number

    1. Report
  2. Question:How to find prime numbers from 1 to 100 in c 

    Answer
    #include<stdio.h>
    
    int main(){
        int num,i,count;
      
        for(num = 1;num<=100;num++){
             count = 0;
    
             for(i=2;i<=num/2;i++){
                 if(num%i==0){
                     count++;
                     break;
                 }
            }
            
             if(count==0 && num!= 1)
                 printf("%d ",num);
        }
      
       return 0;
    }
    Output: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

    1. Report
  3. Question:Write a c program to generate n prime numbers 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int num,i,count,n;
        printf("Enter max range: ");
        scanf("%d",&n);
    
        for(num = 1;num<=n;num++){
    
             count = 0;
    
             for(i=2;i<=num/2;i++){
                 if(num%i==0){
                     count++;
                     break;
                 }
            }
            
             if(count==0 && num!= 1)
                 printf("%d ",num);
        }
      
       return 0;
    }
    Sample output: Enter max range: 50 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

    1. Report
  4. Question:C code to display prime numbers within a range 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int num,i,count,min,max;
    
         printf("Enter min range: ");
         scanf("%d",&min);
    
        printf("Enter max range: ");
        scanf("%d",&max);
    
        for(num = min;num<=max;num++){
    
             count = 0;
    
             for(i=2;i<=num/2;i++){
                 if(num%i==0){
                     count++;
                     break;
                 }
            }
            
             if(count==0 && num!= 1)
                 printf("%d ",num);
        }
      
       return 0;
    }
    Alternate with while loop
    #include<stdio.h>
    
    int main(){
    
       int num,i,count,min,max;
    
    printf("Enter min range: ");
        scanf("%d",&min);
    
        printf("Enter max range: ");
        scanf("%d",&max);
    
        num = min;
        while(num<=max){
    
             count = 0;
             i=2;
    
             while(i<=num/2){
                 if(num%i==0){
                     count++;
                     break;
                 }
                 i++;
            }
            
             if(count==0 && num!= 1)
                 printf("%d ",num);
    
             num++;
        }
      
       return 0;
    }
    Sample output: Enter min range: 10 Enter max range: 50 11 13 17 19 23 29 31 37 41 43 47

    1. Report
  5. Question:C program to find sum of prime numbers within a range. 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int num,i,count,min,max,sum=0;
    
         printf("Enter min range: ");
         scanf("%d",&min);
    
        printf("Enter max range: ");
        scanf("%d",&max);
    
        for(num = min;num<=max;num++){
    
             count = 0;
    
             for(i=2;i<=num/2;i++){
                 if(num%i==0){
                     count++;
                     break;
                 }
            }
            
             if(count==0 && num!= 1)
                 sum = sum + num;
        }
    
        printf("Sum of prime numbers is: %d ",sum);
      
       return 0;
    }
    Sample output: Enter min range: 50 Enter max range: 100 Sum of prime numbers is: 732

    1. Report
  6. Question:C program to check even or odd 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int number;
      
        printf("Enter any integer: ");
        scanf("%d",&number);
    
        if(number % 2 ==0)
             printf("%d is even number.",number);
        else
             printf("%d is odd number.",number);
      
        return 0;
    
    }
    Sample output: Enter any integer: 5 5 is odd number.

    1. Report
  7. Question:Display odd numbers in c within a range 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int number;
        int min,max;
      
        printf("Enter the minimum range: ");
        scanf("%d",&min);
    
        printf("Enter the maximum range: ");
        scanf("%d",&max);
    
        printf("Odd numbers in given range are: ");
        for(number = min;number <= max; number++)
    
             if(number % 2 !=0)
                 printf("%d ",number);
      
        return 0;
    
    }
    Sample output: Enter the minimum range: 1 Enter the maximum range: 20 Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17 19

    1. Report
  8. Question:C program to display separate even and odd numbers list within a range. 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int number;
        int min,max;
      
        printf("Enter the minimum range: ");
        scanf("%d",&min);
    
        printf("Enter the maximum range: ");
        scanf("%d",&max);
    
        printf("Odd numbers in given range are: ");
        for(number = min;number <= max; number++)
    
             if(number % 2 !=0)
                 printf("%d ",number);
    
        printf("\nEven numbers in given range are: ");
        for(number = min;number <= max; number++)
    
             if(number % 2 ==0)
                 printf("%d ",number);
      
        return 0;
    }
    Sample output: Enter the minimum range: 1 Enter the maximum range: 20 Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17 19 Even numbers in given ranges are: 2 4 6 8 10 12 14 16 18 20

    1. Report
  9. Question:Sum of odd numbers in c within a range. 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int number;
        int min,max;
        long sum =0;
      
        printf("Enter the minimum range: ");
        scanf("%d",&min);
    
        printf("Enter the maximum range: ");
        scanf("%d",&max);
    
        for(number = min;number <= max; number++)
             if(number % 2 !=0)
                 sum = sum + number;
    
        printf("Sum of odd numbers in given range is: %ld",sum);
      
        return 0;
    
    }
    Sample output: Enter the minimum range: 1 Enter the maximum range: 100 Sum of odd numbers in given range is: 2500

    1. Report
  10. Question:c program to sum odd and even numbers within a range 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int number;
        int min,max;
        long odd_sum =0,even_sum = 0;
      
        printf("Enter the minimum range: ");
        scanf("%d",&min);
    
        printf("Enter the maximum range: ");
        scanf("%d",&max);
    
        for(number = min;number <= max; number++)
             if(number % 2 != 0)
                 odd_sum = odd_sum + number;
             else
                 even_sum = even_sum + number;
    
        printf("Sum of even numbers in given range is: %ld\n",even_sum);
        printf("Sum of odd numbers in given range is: %ld",odd_sum);
      
        return 0;
    
    }
    Sample output: Enter the minimum range: 1 Enter the maximum range: 10 Sum of even numbers in given range is: 30 Sum of odd numbers in given range is: 25

    1. Report
Copyright © 2024. Powered by Intellect Software Ltd