Lab 5 (C Programming) Solution

Feb 22, 2019

 programs written here are compiled and run in gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0 

1)Write a program to read two numbers from user and find the sum of numbers between these two numbers(included) except the numbers divisible by 7.

 

Source Code: 

#include <stdio.h>

int main()
{
    int i,num1, num2, sum = 0;
    printf("Enter the number to check:");
    scanf(" %d %d", &num1, &num2);
    for(i = num1; i <= num2; i++){
        if(i%7 != 0){
            sum += i;
        }
    }
    printf("Sum = %d", sum);
    return 0;
} 

 

Output:

Enter the number to check:3
60
Sum = 1575

  

 

2)Write a program to check if the number entered by the user is prime or not.

 

Source Code: 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,p_flag = 0,i;
    printf("Enter the positive number to check prime:");
    scanf("%d", &num);
    if(num <= 0){
        printf("Pleae Enter positive number.");
        exit(0);
    }
    if(num == 1){
        printf("It is NOT PRIME.");
        exit(0);
    }
    for(i = 2; i <= num/2; i++){
        if(num % i == 0){
            p_flag = 1;
            break;
        }
    }
    if(p_flag == 0){
        printf("It is PRIME.\n");
    }else{
        printf("It is NOT PRIME.\n");
    }
    return 0;
}

 

 

Output:

Enter the positive number to check prime:29
It is PRIME.

 

3)Write a program to display the Fibonacci series up to the number less than or equal to the number entered by the user.

 

Source Code: 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num, fnum = 0, snum = 1, temp = fnum + snum;
    printf("Enter the number to print up to:");
    scanf("%d", &num);

    printf("\n %d,\t", fnum);
    while(temp <= num){
        printf("%d,\t", temp);
        temp = fnum + snum;
        fnum = snum;
        snum = temp;
    }
    return 0;
}

 

 

Output:

Enter the number to print up to:1000

 0,	1,	1,	2,	3,	5,	8,	13,	21,	34,	55,	89,	144,	233,	377,	610,	987,	. 

  

4)Write a program to find the sum of the following series. 1^2 + 3^2 + 5^2 + ………….. up to the odd number entered by the user

 

Source Code: 

#include <stdio.h>

int main()
{
    int num, sum = 0, i;
    printf("Enter the positive odd number:");
    scanf("%d", &num);
    for(i = 1; i <= num; i += 2){
        sum += i*i;
    }
    printf("SUM = %d\n", sum);
    return 0;
}

 

 

Output:

Enter the positive odd number:25 SUM = 2925 

 

 

5)Write a program to display the following patterns.(row number entered by the user)

 

Source Code(i): 

#include <stdio.h>

int main()
{
    int row_num, i, j;
    printf("Enter odd row number:");
    scanf("%d", &row_num);
    for(i = 0; i < row_num; i++){
        for(j = 0; j < row_num; j++){
            if(i == j || i == row_num - j - 1){
                printf("*");
            }else{
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}

 

Output:

Enter odd row number:15
*             *
 *           * 
  *         *  
   *       *   
    *     *    
     *   *     
      * *      
       *       
      * *      
     *   *     
    *     *    
   *       *   
  *         *  
 *           * 
*             * 

 

 

 

Source Code(ii): 

#include <stdio.h>

int main()
{
    int row_num, i, j;
    printf("Enter row number:");
    scanf("%d", &row_num);
    for(i = 0; i < row_num; i++){
        for(j = 0; j < row_num; j++){
            if(i >= row_num - j -1){
                printf("*");
            }else{
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
} 

 

Output:

Enter row number:20
                   *
                  **
                 ***
                ****
               *****
              ******
             *******
            ********
           *********
          **********
         ***********
        ************
       *************
      **************
     ***************
    ****************
   *****************
  ******************
 *******************
********************

 

 

  

Source Code(iii): 

#include <stdio.h>

int main()
{
    int row_num, i, j;
    printf("Enter row number:");
    scanf("%d", &row_num);
    for(i = 0; i < row_num; i++){
        for(j = 0; j < row_num; j++){
            if(i >= j){
                printf("%d\t", j+1);
            }else{
                printf(" \t");
            }
        }
        printf("\n");
    }
    return 0;
}

 

Output:

Enter row number:10
1	 	 	 	 	 	 	 	 	 	
1	2	 	 	 	 	 	 	 	 	
1	2	3	 	 	 	 	 	 	 	
1	2	3	4	 	 	 	 	 	 	
1	2	3	4	5	 	 	 	 	 	
1	2	3	4	5	6	 	 	 	 	
1	2	3	4	5	6	7	 	 	 	
1	2	3	4	5	6	7	8	 	 	
1	2	3	4	5	6	7	8	9	 	
1	2	3	4	5	6	7	8	9	10	 

 

 

Source Code(iv) (Question 5-iv is changed): 

#include <stdio.h>

int main()
{
    int row_num, i, j, temp = 1;
    printf("Enter row number:");
    scanf("%d", &row_num);
    for(i = 0; i < row_num; i++){
        for(j = 0; j < row_num; j++){
            if(j < row_num - i){
                printf("%d\t", temp);
                temp++;
            }else{
                printf(" \t");
            }
        }
        printf("\n");
    }
    return 0;
}

 

Output:

1	2	3	4	5	6	7	8	9	10	
11	12	13	14	15	16	17	18	19	 	
20	21	22	23	24	25	26	27	 	 	
28	29	30	31	32	33	34	 	 	 	
35	36	37	38	39	40	 	 	 	 	
41	42	43	44	45	 	 	 	 	 	
46	47	48	49	 	 	 	 	 	 	
50	51	52	 	 	 	 	 	 	 	
53	54	 	 	 	 	 	 	 	 	
55