Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Exercises:

Certainly! Here are some simple exercises in C language along with examples:

  1. Calculate the Sum of Two Numbers:
  2. Write a program that takes two numbers as input from the user and calculates their sum.

    #include <stdio.h>

    int main() {
    int num1, num2, sum;

    printf("Enter first number: ");
    scanf("%d", &num1);

    printf("Enter second number: ");
    scanf("%d", &num2);

    sum = num1 + num2;
    printf("Sum = %d\n", sum);

    return 0;
    }

  3. Check if a Number is Even or Odd:
  4. Write a program that checks whether a given number is even or odd.

    #include <stdio.h>

    int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (num % 2 == 0) {
    printf("%d is even.\n", num);
    } else {
    printf("%d is odd.\n", num);
    }

    return 0;
    }

  5. Calculate Factorial of a Number:
  6. Write a program to calculate the factorial of a given number.

    #include <stdio.h>

    int main() {
    int num, fact = 1, i;

    printf("Enter a number: ");
    scanf("%d", &num);

    for (i = 1; i <= num; i++) {
    fact *= i;
    }

    printf("Factorial of %d = %d\n", num, fact);

    return 0;
    }

  7. Check if a Year is Leap Year:
  8. Write a program to check whether a given year is a leap year or not.

    #include <stdio.h>

    int main() {
    int year;

    printf("Enter a year: ");
    scanf("%d", &year);

    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
    printf("%d is a leap year.\n", year);
    } else {
    printf("%d is not a leap year.\n", year);
    }

    return 0;
    }

  9. Print Fibonacci Series:
  10. Write a program to print the Fibonacci series up to a given number of terms.

    #include <stdio.h>

    int main() {
    int num, first = 0, second = 1, next, i;

    printf("Enter the number of terms: ");
    scanf("%d", &num);

    printf("Fibonacci Series: ");
    for (i = 0; i < num; i++) {
    printf("%d, ", first);
    next = first + second;
    first = second;
    second = next;
    }

    return 0;
    }

    These exercises cover basic concepts such as input/output, arithmetic operations, conditional statements, loops, and functions in C language. They're suitable for beginners to practice and enhance their programming skills.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java