Topics

Search This Blog

Thursday, March 20, 2025

VVI: C Programming questions solution

 

C Programming questions solution

1. Write C program to display first 10 natural numbers (1 to 10)

Solution:

#include <stdio.h>

int main()

{

    int i;

    printf("Natural numbers from 1 to 10:\n");

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

        printf("%d ", i);

    printf("\n");

    return 0;

}

 

2. Write a C programming to display first 20 natural numbers (1 to 20)

Solution:

#include <stdio.h>

int main()

{

    int i;

    printf("Natural numbers from 1 to 20:\n");

    for (i = 1; i <= 20; i++)

        printf("%d ", i);

    printf("\n");

    return 0;

}

 

3. Write a program to print natural numbers from 1 to 50.

Solution:

#include <stdio.h>

int main()

{

    int i;

    printf("Natural numbers from 1 to 50:\n");

    for (i = 1; i <= 50; i++)

        printf("%d ", i);

    printf("\n");

    return 0;

}

 

4. Write a program using C language to display reverse order from the entered number.

[ex: Enter 5 and display the result: 5 4 3 2 1)

Solution:

#include <stdio.h>

int main() {

    int n, i;

    printf("Enter the value of n: ");

    scanf("%d", &n);

    printf("Numbers in reverse order from %d to 1:\n", n);

    for (i = n; i >= 1; i--)

    printf("%d ", i);

    printf("\n");

    return 0;

}

 

5. Write C program to get the output for natural numbers from 5 to 15.

Solution:

#include <stdio.h>

int main()

{

    int i;

    printf("Natural numbers from 5 to 15:\n");

    for (i = 5; i <= 15; i++)

        printf("%d ", i);

    printf("\n");

    return 0;

}

 

6. Write C programming to calculate the sum of first 10 natural numbers.

Solution:

#include <stdio.h>

int main()

{

    int i, sum = 0;

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

        sum += i;

    printf("The sum of the first 10 natural numbers is: %d\n", sum);

    return 0;

}

 

7. Write C program to display only multiples of 5 from 1 to 50.

Solution:

#include <stdio.h>

int main()

{

    int i;

    printf("Multiples of 5 from 1 to 50:\n");

    for (i = 1; i <= 50; i++)

        if (i % 5 == 0) 

            printf("%d ", i);

    return 0;

}

 

8. Write C programming to display odd numbers from 1 to 19.

Solution:

#include <stdio.h>

int main()

{

    int i;

    printf("1 to 19 odd numbers:\n");

    for (i = 1; i <= 19; i += 2)

        printf("%d ", i);

    printf("\n");

    return 0;

}

 

9. Write C programming to display first 15 odd numbers.

Solution:

#include <stdio.h>

int main()

{

    int i;

    printf("First 15 even numbers:\n");

    for (i = 2; i <= 30; i += 2)

        printf("%d ", i);

    printf("\n");

    return 0;

}

 

10. Write a C program to count and print all odd numbers between 1 and 50.

Solution:

#include <stdio.h>

int main()

{

    int i, sum = 0;

    printf("Odd numbers between 1 and 50 are:\n");

    for (i = 1; i <= 50; i += 2)

    {

        printf("%d ", i);

            sum += i;

    }

        printf("\nSum of odd numbers between 1 and 50: %d\n", sum);

    return 0;

}

 

11. To display the reverse order of odd numbers from 19 to 1 write a programming using C language.

Solution:

#include <stdio.h>

int main()

{

    int i;

    printf("Odd numbers from 19 to 1:\n");

    for (i = 19; i >= 1; i -= 2)

        printf("%d\n", i);

    return 0;

}

 

12. Extend the program to calculate the sum of the first 15 odd numbers.

Solution:

#include <stdio.h>

int main()

{

    int i, sum = 0;

    printf("Odd numbers between 1 and 15 are:\n");

    for (i = 1; i <= 15; i += 2)

    {

        printf("%d\n", i);

            sum += i;

    }

        printf("\nSum of odd numbers between 1 and 15: %d\n", sum);

    return 0;

}

 

13. Write C programming to print the first 15 odd numbers with their squares.

Solution:

#include <stdio.h>

int main()

{

    int i;

    printf("Square of odd numbers between 1 and 15 are:\n");

    for (i = 1; i <= 15; i += 2)

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

    return 0;

}

OR

#include <stdio.h>

int main()

{

    int i;

    printf("Square of odd numbers between 1 and 15 are:\n");

    for (i = 1; i <= 15; i += 2)

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

    return 0;

}

 

14. Write a Program to display even numbers from 1 to 20.

Solution:

#include <stdio.h>

int main()

{

    int i;

    printf("Even numbers from 1 to 20:\n");

    for (i = 2; i <= 20; i += 2)

        printf("%d ", i);

    return 0;

}

 

15. Write C programming to display the sum of the first 15 even numbers.

Solution:

#include <stdio.h>

int main()

{

    int i, sum = 0;

    printf("Even numbers between 1 and 30 are:\n");

    for (i = 2; i <= 30; i += 2)

    {

        printf("%d ", i);

            sum += i;

    }

        printf("\nSum of even numbers between 2 and 30: %d\n", sum);

    return 0;

}

 

16. Write a C program to identify if a given number is even or odd.

Solution:

#include <stdio.h>

int main()

{

    int num;

    printf("Enter a number: ");

    scanf("%d", &num);

    if (num % 2 == 0)

        printf("%d is an even number.\n");

    else

        printf("%d is an odd number.\n");

    return 0;

}

No comments:

Post a Comment

Map of Nepal

 नेपालको नक्सा:  नेपालको नक्सा: नेपालको नक्सा: नेपालको नक्सा: नेपालको नक्सा: नेपालको नक्सा: नेपालको नक्सा:

Most viewed!