C-Control Statement Codes (52 Questions Solved)

 1.Write a program to find the given number is positive or negative

#include <stdio.h>

int main() {
    int a;
    printf("Enter the number, a: ");
    scanf("%d", &a);
    
    if (a > 0) {
        printf("Positive Number");
    } else {
        printf("Negative Number");
    }
    
    return 0;
}

2. Write a program to find the given number is odd or even

#include <stdio.h>

int main() {
    int a, R;
    printf("Enter the number, a: ");
    scanf("%d", &a);
    
    R = a % 2;
    
    if (R == 0) {
        printf("Even Number");
    } else {
        printf("Odd Number");
    }
    
    return 0;
}

3. WAP to read a number from the user and whether a given number is prime or composite.

#include <stdio.h>

int main() {
    int n, i = 2;
    printf("Enter the number: ");
    scanf("%d", &n);

    while (i < n) {
        if (n % i == 0) {
            printf("Composite Number");
            break;
        }
        i++;
    }

    if (i == n)
        printf("Prime Number");

    return 0;
}

4. Write a program to check whether a first number is divisible by second or not

#include <stdio.h>

int main() {
    int a, b, R;
    printf("Enter the two numbers: ");
    scanf("%d%d", &a, &b);
    
    R = a % b;
    
    if (R == 0) {
        printf("The first number %d is divisible by the second %d", a, b);
    } else {
        printf("The first number %d is not divisible by the second %d", a, b);
    }
    
    return 0;
}

5. Write a program to find the given number is exactly divisible by 3 or not

#include <stdio.h>

int main() {
    int a, R;
    printf("Enter the number, a: ");
    scanf("%d", &a);
    
    R = a % 3;
    
    if (R == 0) {
        printf("Divisible by 3");
    } else {
        printf("Not divisible by 3");
    }
    
    return 0;
}


6. WAP a program to read a number from keyboard and test whether the number is exactly divisible by 3 but not by 11 or not.

#include <stdio.h>

int main() {
    int a;
    printf("Enter the number, a: ");
    scanf("%d", &a);
    
    if (a % 3 == 0 && a % 11 != 0) {
        printf("Divisible by 3 but not divisible by 11");
    } else {
        printf("Not divisible by 3 or divisible by 11");
    }
    
    return 0;
}


7. Write a program to find the greatest among three numbers using nested if… else statement

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter the numbers: ");
    scanf("%d%d%d", &a, &b, &c);
    
    if (a > c && a > b) {
        printf("Greatest is %d", a);
    } else if (b > a && b > c) {
        printf("Greatest is %d", b);
    } else {
        printf("Greatest is %d", c);
    }
    
    return 0;
}

8. Write a program to find the greatest among three numbers using nested if... else if statement

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter the numbers: ");
    scanf("%d%d%d", &a, &b, &c);
    
    if (a > b) {
        if (a > c) {
            printf("Greatest number is %d", a);
        } else {
            printf("Greatest number is %d", c);
        }
    } else {
        if (b > c) {
            printf("Greatest number is %d", b);
        } else {
            printf("Greatest number is %d", c);
        }
    }
    
    return 0;
}

9. Write a program to find the greatest among four numbers

#include <stdio.h>

int main() {
    int a, b, c, d;
    printf("Enter the numbers: ");
    scanf("%d%d%d%d", &a, &b, &c, &d);
    
    if (a > b && a > c && a > d) {
        printf("Greatest is %d", a);
    } else if (b > a && b > c && b > d) {
        printf("Greatest is %d", b);
    } else if (c > a && c > b && c > d) {
        printf("Greatest is %d", c);
    } else {
        printf("Greatest is %d", d);
    }
    
    return 0;
}

10. Write a program to find the second largest number among three numbers

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter the numbers: ");
    scanf("%d%d%d", &a, &b, &c);
    
    if ((a > b && a < c) || (a > c && a < b)) {
        printf("Second largest is %d", a);
    } else if ((b > a && b < c) || (b > c && b < a)) {
        printf("Second largest is %d", b);
    } else {
        printf("Second largest is %d", c);
    }
    
    return 0;
}

11. Write a program to convert uppercase to lowercase and lowercase to uppercase

#include <stdio.h>

int main() {
    char ch;
    printf("Enter the desired character, ch: ");
    scanf("%c", &ch);
    
    if (ch >= 65 && ch <= 90) {
        printf("The character is uppercase\n");
        ch = ch + 32;
        printf("Lowercase character is %c", ch);
    } else if (ch >= 97 && ch <= 122) {
        printf("Character is lowercase\n");
        ch = ch - 32;
        printf("Uppercase character is %c", ch);
    } else {
        printf("Invalid character");
    }
    
    return 0;
}

12. Write a program to find whether the given number is loop year or not

#include <stdio.h>

int main() {
    int year;
    printf("Enter the year: ");
    scanf("%d", &year);
    
    if (year % 4 == 0 && year % 100 != 0) {
        printf("Leap year");
    } else if (year % 400 == 0) {
        printf("Leap year");
    } else {
        printf("Not a leap year");
    }
    
    return 0;
}

13. Write a program to find all the roots of quadratic equation

#include <stdio.h>
#include <math.h>

int main() {
    float a, b, c, D, R1, R2, R, I;
    printf("Enter the value of a, b, c: ");
    scanf("%f%f%f", &a, &b, &c);
    
    D = b * b - 4 * a * c;
    
    if (D == 0) {
        R1 = -b / (2 * a);
        printf("The roots are real and equal = %f, %f", R1, R1);
    } else if (D > 0) {
        R1 = (-b + sqrt(D)) / (2 * a);
        R2 = (-b - sqrt(D)) / (2 * a);
        printf("The roots are real and unequal = %f, %f", R1, R2);
    } else {
        R = -b / (2 * a);
        I = sqrt(-D) / (2 * a);
        printf("The roots are imaginary = %.2f+%.2fi, %.2f-%.2fi", R, I, R, I);
    }
    
    return 0;
}

14. Write a program to check three sides of a triangle and check the validity of the triangle as well as display if it is scalene, isosceles, equivalent or right-angled triangle.

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter the three sides of the triangle: ");
    scanf("%d%d%d", &a, &b, &c);
    
    if (a <= 0 || b <= 0 || c <= 0 || (a + b <= c) || (a + c <= b) || (b + c <= a)) {
        printf("Invalid triangle");
    } else {
        if (a == b && b == c) {
            printf("It is an equilateral triangle.");
        } else if (a == b || b == c || a == c) {
            printf("It is an isosceles triangle.");
        } else {
            printf("It is a scalene triangle.");
        }
        
        if ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (b * b + c * c == a * a)) {
            printf(" The triangle is a right-angled triangle.");
        }
    }
    
    return 0;
}

15. The monthly electricity bill is to calculated as: a) Minimum Rs. 80 for up to 20 units. b) Rs. 7.30 per unit for next 100 units. c) Rs. 9.00 per unit for any units beyond 120 units. Write a program to calculate the monthly bill for a given number of units consumed by a customer.

#include <stdio.h>

int main() {
    float unit, Price;
    printf("Enter the consumed unit: ");
    scanf("%f", &unit);
    
    if (unit <= 20) {
        printf("The price is Rs.80");
    } else if (unit <= 100 && unit > 20) {
        Price = 80 + (unit - 20) * 7.3;
        printf("The price is Rs.%.2f", Price);
    } else {
        Price = 80 + (unit - 20) * 9.0;
        printf("The price is Rs.%.2f", Price);
    }
    
    return 0;
}

16. Write a program to find the arithmetic operation using the switch statement

#include <stdio.h>

int main() {
    float a, b, R;
    char x;
    printf("Enter the numbers: ");
    scanf("%f%f", &a, &b);
    printf("Enter the operation (+, -, *, /): ");
    scanf(" %c", &x);
    
    switch (x) {
        case '+':
            R = a + b;
            printf("The sum is %.2f", R);
            break;
        case '-':
            R = a - b;
            printf("The difference is %.2f", R);
            break;
        case '*':
            R = a * b;
            printf("The product is %.2f", R);
            break;
        case '/':
            if (b != 0) {
                R = a / b;
                printf("The division is %.2f", R);
            } else {
                printf("Error: Division by zero");
            }
            break;
        default:
            printf("Invalid input");
    }
    
    return 0;
}

17. Write a program to print the names of the days using switch statement. Our output must show Sunday if the user enters 1 and so on.

#include <stdio.h>

int main() {
    int num;
    printf("Enter the number: ");
    scanf("%d", &num);
    
    switch (num) {
        case 1:
            printf("Sunday");
            break;
        case 2:
            printf("Monday");
            break;
        case 3:
            printf("Tuesday");
            break;
        case 4:
            printf("Wednesday");
            break;
        case 5:
            printf("Thursday");
            break;
        case 6:
            printf("Friday");
            break;
        case 7:
            printf("Saturday");
            break;
        default:
            printf("Invalid input");
    }
    
    return 0;
}

18. Write a program to display whether the entered charactered by the user is either vowel, consonant or digits using switch statement.

#include <stdio.h>

int main() {
    char a;
    printf("Enter the alphabet or digit: ");
    scanf(" %c", &a);
    
    switch (a) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            printf("It is a vowel");
            break;
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            printf("It is a digit");
            break;
        default:
            printf("It is a consonant");
    }
    
    return 0;
}

19. Write a program to check whether a input character is vowel or consonant

#include <stdio.h>

int main() {
    char x;
    printf("Enter the character: ");
    scanf("%c", &x);
    
    if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u') {
        printf("The entered character is a vowel");
    } else {
        printf("The entered character is a consonant");
    }
    
    return 0;
}

20. Write a program to find whether the given number is odd or even using switch statement

#include <stdio.h>

int main() {
    int a, R;
    printf("Enter the number: ");
    scanf("%d", &a);
    
    R = a % 2;
    
    switch (R) {
        case 0:
            printf("The number is even");
            break;
        default:
            printf("The number is odd");
    }
    
    return 0;
}

21. Write a program to check whether a given number is prime or composite using switch statement

#include <stdio.h>

int main() {
    int n, i = 2;
    printf("Enter the number: ");
    scanf("%d", &n);
    
    switch (n) {
        case 0:
        case 1:
            printf("Not a Prime Number");
            break;
        case 2:
            printf("Prime Number");
            break;
        default:
            while (i < n) {
                if (n % i == 0) {
                    printf("Composite Number");
                    break;
                }
                i++;
            }
            if (i == n) {
                printf("Prime Number");
            }
            break;
    }
    
    return 0;
}

22. Write a program to input digit decimal number and display its digits in words using switch statement.

#include <stdio.h>

int main() {
    int n, R, reverse = 0;
    printf("Enter a decimal number: ");
    scanf("%d", &n);
    
    printf("Digits in words: ");
    
    if (n == 0) {
        printf("Zero");
        return 0;
    }
    
    // Reverse the number
    do {
        R = n % 10;
        reverse = reverse * 10 + R;
        n = n / 10;
    } while (n != 0);
    
    // Print digits in words
    while (reverse != 0) {
        R = reverse % 10;
        reverse = reverse / 10;
        switch (R) {
            case 0:
                printf("Zero ");
                break;
            case 1:
                printf("One ");
                break;
            case 2:
                printf("Two ");
                break;
            case 3:
                printf("Three ");
                break;
            case 4:
                printf("Four ");
                break;
            case 5:
                printf("Five ");
                break;
            case 6:
                printf("Six ");
                break;
            case 7:
                printf("Seven ");
                break;
            case 8:
                printf("Eight ");
                break;
            case 9:
                printf("Nine ");
                break;
            default:
                printf("Invalid Number ");
        }
    }
    
    printf("\n");
    return 0;
}

23. Write a program to read two numbers from the user (n1<n2) and display the natural numbers between them

#include <stdio.h>

int main() {
    int n1, n2, i = 1;
    printf("Enter the two numbers: ");
    scanf("%d%d", &n1, &n2);
    
    while (i <= (n2 - n1 - 1)) {
        printf("The numbers in between %d and %d are %d\n", n1, n2, n1 + i);
        i++;
    }
    
    return 0;
}

24. Write a program to read two numbers from the user (n1<n2) and display the numbers which are divisible by 5

#include <stdio.h>

int main() {
    int n1, n2, i = 1;
    printf("Enter the two numbers: ");
    scanf("%d%d", &n1, &n2);
    
    while (i <= (n2 - n1)) {
        if ((n1 + i) % 5 == 0) {
            printf("The numbers in between %d and %d that are divisible by 5 are %d\n", n1, n2, n1 + i);
        }
        i++;
    }
    
    return 0;
}

25. Write a program to find the factorial of a given number

#include <stdio.h>

int main() {
    int n, i;
    long fact = 1;
    
    printf("Enter the number: ");
    scanf("%d", &n);
    
    for (i = 1; i <= n; i++) {
        fact = fact * i;
    }
    
    printf("The factorial of %d is %ld", n, fact);
    
    return 0;
}

26. Write a program to find the sum of first n natural number

#include <stdio.h>

int main() {
    int n, sum = 0, i;
    
    printf("Enter the number: ");
    scanf("%d", &n);
    
    for (i = 1; i <= n; i++) {
        sum = sum + i;
    }
    
    printf("The sum of natural numbers up to %d = %d", n, sum);
    
    return 0;
}

27. WAP to print the ASCII values of all characters.

#include <stdio.h>

int main() {
    int i;
    printf("ASCII values of all characters:\n");
    
    for (i = 0; i <= 127; i++) {
        printf("Character: %c, ASCII Value: %d\n", i, i);
    }
    
    return 0;
}

28. Write a program to display the series: 1/2 2/3 3/4 4/5 … (n-1)/n

#include <stdio.h>

int main() {
    int i = 1, n;
    
    printf("Enter the number: ");
    scanf("%d", &n);
    
    while (i <= n) {
        printf("%d/%d\t", i, i + 1);
        i++;
    }
    
    return 0;
}

29. WAP to evaluate the series: S = 1 + 2 x 1 + 3 x 2 + … N x N-1 up to N terms

#include <stdio.h>

int main() {
    int i = 1, n, sum = 1;
    
    printf("Enter the number of terms (n): ");
    scanf("%d", &n);
    
    while (i <= n) {
        sum = sum + i * (i - 1);
        i++;
    }
    
    printf("The sum of the series 1 + 2 x 1 + 3 x 2 + … N x (N-1) up to N terms is %d", sum);
    
    return 0;
}

30. Write a program to read two numbers and find and display the sum. The program must ask next two numbers and add until user wants.

#include <stdio.h>

int main() {
    int a, b, sum;
    char i = 'y';
    
    while (i == 'y' || i == 'Y') {
        printf("Enter the two numbers: ");
        scanf("%d%d", &a, &b);
        
        sum = a + b;
        printf("The sum is %d\n", sum);
        
        printf("Do you want to add more? If yes, press y: ");
        scanf(" %c", &i);
    }
    
    return 0;
}

31. Write a program to read a number from the user until a zero or negative number is keyed in. Finally calculate the sum and average of entered numbers.

#include <stdio.h>

int main() {
    int n, count = 0, sum = 0;
    
    printf("Enter numbers and enter zero or a negative number to stop:\n");
    
    while (1) {
        printf("Enter a number: ");
        scanf("%d", &n);
        
        if (n <= 0) {
            break;
        }
        
        sum = sum + n;
        count++;
    }
    
    if (count > 0) {
        float average = (float)sum / count;
        printf("Sum of entered numbers: %d\n", sum);
        printf("Average of entered numbers: %.2f\n", average);
    } else {
        printf("No valid numbers were entered.\n");
    }
    
    return 0;
}

32. Write a program to find the multiplication table a given number using for loop

#include <stdio.h>

int main() {
    int n, i;
    
    printf("Enter the number: ");
    scanf("%d", &n);
    
    for (i = 1; i <= 10; i++) {
        printf("%d x %d = %d\n", n, i, n * i);
    }
    
    return 0;
}

33. Write a program to display the multiplication table from 5-10.

#include <stdio.h>

int main() {
    int i, j;
    
    for (j = 5; j <= 10; j++) {
        for (i = 1; i <= 10; i++) {
            printf("%d x %d = %d ", j, i, j * i);
        }
        printf("\n");
    }
    
    return 0;
}

34. Write a program to count the number of digits in a given integer number.

#include <stdio.h>

int main() {
    long long n;
    int count = 0;
    
    printf("Enter the number: ");
    scanf("%lld", &n);
    
    if (n == 0) {
        count = 1;
    } else {
        while (n != 0) {
            n = n / 10;
            count++;
        }
    }
    
    printf("Number of digits are %d\n", count);
    
    return 0;
}

35. Write a program to find the sum of the digits of a given integer number.

#include <stdio.h>

int main() {
    long n;
    int sum = 0, R;
    
    printf("Enter the number: ");
    scanf("%ld", &n);
    
    while (n != 0) {
        R = n % 10;
        sum = sum + R;
        n = n / 10;
    }
    
    printf("The sum of digits is %d", sum);
    
    return 0;
}

36. Write a program to find the reverse of the given positive number

#include <stdio.h>

int main() {
    long n, result = 0;
    int R;
    
    printf("Enter the positive number: ");
    scanf("%ld", &n);
    
    do {
        R = n % 10;
        result = result * 10 + R;
        n = n / 10;
    } while (n != 0);
    
    printf("The reversed number is %ld", result);
    
    return 0;
}

37. Write a program to check whether a given number is Palindrome or not

#include <stdio.h>

int main() {
    long n, num, result = 0;
    int remainder;
    
    printf("Enter the number: ");
    scanf("%ld", &num);
    
    n = num;
    
    do {
        remainder = num % 10;
        result = result * 10 + remainder;
        num = num / 10;
    } while (num != 0);
    
    if (n == result) {
        printf("The number is a palindrome");
    } else {
        printf("The number is not a palindrome");
    }
    
    return 0;
}

38. Write a program to check whether the given number is Armstrong or not

#include <stdio.h>

int main() {
    int n, R, num, result = 0;
    
    printf("Enter the number: ");
    scanf("%d", &n);
    
    num = n;
    
    while (n != 0) {
        R = n % 10;
        result = result + R * R * R;
        n = n / 10;
    }
    
    if (result == num) {
        printf("Entered number is an Armstrong number");
    } else {
        printf("Not an Armstrong Number");
    }
    
    return 0;
}

39. WAP to check whether a given number is strong or not.

#include <stdio.h>

int main() {
    int n, sum = 0, num, R, i, fact;
    
    printf("Enter the number: ");
    scanf("%d", &n);
    
    num = n;
    
    while (n != 0) {
        R = n % 10;
        fact = 1;
        for (i = 1; i <= R; i++) {
            fact = fact * i;
        }
        sum = sum + fact;
        n = n / 10;
    }
    
    if (sum == num) {
        printf("Strong Number");
    } else {
        printf("Not a Strong Number");
    }
    
    return 0;
}

40. Write a program to check whether a given number is Perfect or not.

#include <stdio.h>

int main() {
    int n, R, i, sum = 0;
    
    printf("Enter the number: ");
    scanf("%d", &n);
    
    for (i = 1; i < n; i++) {
        R = n % i;
        if (R == 0) {
            sum = sum + i;
        }
    }
    
    if (sum == n) {
        printf("Perfect Number");
    } else {
        printf("Not a Perfect Number");
    }
    
    return 0;
}

41. Write a program to find the perfect numbers between entered two numbers.

#include <stdio.h>

int main() {
    int n1, n2, sum, i, n;
    
    printf("Enter the two numbers: ");
    scanf("%d%d", &n1, &n2);
    
    printf("The perfect numbers between %d and %d are:\n", n1, n2);
    
    for (n = n1; n <= n2; n++) {
        sum = 0;
        for (i = 1; i < n; i++) {
            if (n % i == 0) {
                sum = sum + i;
            }
        }
        if (sum == n) {
            printf("%d\t", n);
        }
    }
    
    return 0;
}

42. Write a program to find the Armstrong numbers between given two ranged numbers

#include <stdio.h>

int main() {
    int n1, n2, result, num, R, i;
    
    printf("Enter the two numbers: ");
    scanf("%d%d", &n1, &n2);
    
    printf("Armstrong numbers between %d and %d are:\n", n1, n2);
    
    for (i = n1; i <= n2; i++) {
        result = 0;
        num = i;
        while (num != 0) {
            R = num % 10;
            result = result + R * R * R;
            num = num / 10;
        }
        if (result == i) {
            printf("%d\t", i);
        }
    }
    
    return 0;
}

43. Write a program to generate the Fibonacci series of n terms enter by user using loop

#include <stdio.h>

int main() {
    int i, n, first = 0, second = 1, next;
    
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    
    for (i = 0; i < n; i++) {
        if (i <= 1) {
            next = i;
        } else {
            next = first + second;
            first = second;
            second = next;
        }
        printf("%d ", next);
    }
    
    return 0;
}

44. Write a program to check whether an entered number is the term of Fibonacci Series or not.

#include <stdio.h>

int main() {
    int i, n, first = 0, second = 1, next;
    
    printf("Enter the number: ");
    scanf("%d", &n);
    
    while (second < n) {
        next = first + second;
        first = second;
        second = next;
    }
    
    if (second == n) {
        printf("It is a term of the Fibonacci series");
    } else {
        printf("It is not a term of the Fibonacci series");
    }
    
    return 0;
}

45. Write a program to find the HCF (GCD) and LCM of given two numbers.

#include <stdio.h>

int main() {
    int a, b, c, d, hcf, lcm, temp;
    
    printf("Enter the two numbers: ");
    scanf("%d%d", &a, &b);
    
    c = a;
    d = b;
    
    while (b != 0) {
        temp = b;
        b = a % b;
        a = temp;
    }
    
    hcf = a;
    lcm = (c * d) / hcf;
    
    printf("HCF and LCM of %d and %d are %d and %d\n", c, d, hcf, lcm);
    
    return 0;
}

46. Write a program to read two integers and display both the even numbers and odd numbers between those numbers separately. Also display the number of frequencies of both even and odd numbers

#include <stdio.h>

int main() {
    int a, b, Ocount = 0, Ecount = 0, i;
    
    printf("Enter the two numbers: ");
    scanf("%d%d", &a, &b);
    
    printf("Even numbers between %d and %d are:\n", a, b);
    for (i = a; i <= b; i++) {
        if (i % 2 == 0) {
            printf("%d\t", i);
            Ecount = Ecount + 1;
        }
    }
    
    printf("\nOdd numbers between %d and %d are:\n", a, b);
    for (i = a; i <= b; i++) {
        if (i % 2 != 0) {
            printf("%d\t", i);
            Ocount = Ocount + 1;
        }
    }
    
    printf("\nThe number of odd and even numbers between %d and %d are %d and %d\n", a, b, Ocount, Ecount);
    
    return 0;
}

47. Write a program to find the cubes and squares of first 10 natural numbers

#include <stdio.h>

int main() {
    int i = 1;
    
    while (i <= 10) {
        printf("The square and cube of %d are %d and %d\n", i, i * i, i * i * i);
        i++;
    }
    
    return 0;
}

48. Write a program to find the mean and standard deviation for the given set of numbers

#include <stdio.h>
#include <math.h>

int main() {
    int n, i;
    float sum = 0, mean, variance = 0, std;
    
    printf("Enter the number of data: ");
    scanf("%d", &n);
    
    float num[n];
    
    printf("Enter %d numbers:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%f", &num[i]);
        sum = sum + num[i];
    }
    
    mean = sum / n;
    
    for (i = 0; i < n; i++) {
        variance += pow(num[i] - mean, 2);
    }
    
    variance /= n;
    std = sqrt(variance);
    
    printf("Mean is %.2f\n", mean);
    printf("Standard Deviation is %.2f\n", std);
    
    return 0;
}

49. Write a program to find out the sum of Arithmetic Series

#include <stdio.h>

int main() {
    int a, d, sum, n;
    
    printf("Enter the first term, difference, and number of terms of the series (a, d, n): ");
    scanf("%d%d%d", &a, &d, &n);
    
    sum = (n / 2) * (2 * a + (n - 1) * d);
    
    printf("The sum of the given arithmetic series is %d\n", sum);
    
    return 0;
}

50. Write a program to find out the sum of Geometric Series.

#include <stdio.h>
#include <math.h>

int main() {
    float a, r, sum;
    int n;
    
    printf("Enter the first term, common ratio, and number of terms (a, r, n): ");
    scanf("%f%f%d", &a, &r, &n);
    
    sum = (a * (1 - pow(r, n))) / (1 - r);
    
    printf("The sum of the given geometric series is %.2f\n", sum);
    
    return 0;
}

51. Write a program to convert decimal number into binary number

#include <stdio.h>

int main() {
    long int deci, bin, R, Div, i = 1, sum = 0;
    
    printf("Enter the decimal number: ");
    scanf("%ld", &deci);
    
    do {
        Div = deci / 2;
        R = deci % 2;
        deci = Div;
        sum = sum + R * i;
        i = i * 10;
    } while (Div != 0);
    
    printf("The binary number is %ld\n", sum);
    
    return 0;
}

52. Write a program to convert binary number into decimal number

#include <stdio.h>

int main() {
    long int bin, Div, R, i = 1, sum = 0;
    
    printf("Enter the binary number: ");
    scanf("%ld", &bin);
    
    do {
        Div = bin / 10;
        R = bin % 10;
        bin = Div;
        sum = sum + R * i;
        i = i * 2;
    } while (Div != 0);
    
    printf("The decimal number is %ld\n", sum);
    
    return 0;
}

Comments

Popular posts from this blog

C-Codes using Recursion

C-User Defined Function Codes ( 24 Questions Solved)