Day 6 of Programming in C

Write a program to print the multiplication table of the number entered by the user.


#include <stdio.h>
int main(){

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

    for(int i=1;i<=10;i++){
        printf("%d * %d = %d\n",num,i,num*i);
    }
   return 0;
}
    
    
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function, which returns an integer.
Step 3: Declare an integer variable `num`.
Step 4: Print the string "Enter the number : " to the console.
Step 5: Read an integer from the input and store it in `num` using `scanf`.
Step 6: Start a `for` loop, initializing integer `i` to 1, continuing as long as `i` is less than or equal to 10, and incrementing `i` by 1 in each iteration.
Step 7: Inside the loop, print the value of `num`, followed by the string " * ", followed by the value of `i`, followed by the string " = ", followed by the product of `num` and `i`, followed by a newline character to the console.
Step 8: The `for` loop continues until `i` is greater than 10.
Step 9: Return 0 from the `main` function, indicating successful program execution.

Write a program to print 24 hours of day with suitable suffixes like AM, PM, Noon and Midnight.


#include <stdio.h>
int main()
{
   for(int hour = 0;hour<=24;hour++){
       (hour == 0)?(printf("Midnight\n")):
       (hour<12)?(printf("%d:00AM\n",hour)):
       (hour == 12)?(printf("Noon\n")): printf("%d:00PM\n",hour - 12);
    }
  return 0;
}

        
    
    
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function, which returns an integer.
Step 3: Start a `for` loop, initializing integer `hour` to 0, continuing as long as `hour` is less than or equal to 24, and incrementing `hour` by 1 in each iteration.
Step 4: Inside the loop, use a nested ternary operator: if `hour` is equal to 0, print "Midnight\n".
Step 5: Otherwise, if `hour` is less than 12, print the value of `hour`, followed by ":00AM\n".
Step 6: Otherwise, if `hour` is equal to 12, print "Noon\n".
Step 7: Otherwise, print the value of `hour` minus 12, followed by ":00PM\n".
Step 8: The `for` loop continues until `hour` is greater than 24.
Step 9: Return 0 from the `main` function, indicating successful program execution.

Write a program in C to display the sinx,cosx,tanx where x will be taken 10 times from the keyboard.


 #include <stdio.h>
 #include <math.h>
 int main(){
    
    float rad;
    for (int i = 1; i <= 10; i++)
    {
        printf("Enter the degree in radians : ");
        scanf("%f", &rad);
        printf("sin(%.2f) = %.2f\ncos(%.2f) = %.2f\ntan(%.2f) = %.2f", rad, sin(rad), rad, cos(rad), rad, tan(rad));
    }
    return 0;
}
     
    
    
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Include the math library `math.h`.
Step 3: Define the `main` function, which returns an integer.
Step 4: Declare a float variable `rad`.
Step 5: Start a `for` loop, initializing integer `i` to 1, continuing as long as `i` is less than or equal to 10, and incrementing `i` by 1 in each iteration.
Step 6: Inside the loop, print the string "Enter the degree in radians : " to the console.
Step 7: Read a float value from the input and store it in `rad` using `scanf`.
Step 8: Print the string "sin(", followed by the value of `rad` formatted to two decimal places, followed by ") = ", followed by the sine of `rad` calculated using `sin(rad)`, formatted to two decimal places, followed by a newline character.
Step 9: Print the string "cos(", followed by the value of `rad` formatted to two decimal places, followed by ") = ", followed by the cosine of `rad` calculated using `cos(rad)`, formatted to two decimal places, followed by a newline character.
Step 10: Print the string "tan(", followed by the value of `rad` formatted to two decimal places, followed by ") = ", followed by the tangent of `rad` calculated using `tan(rad)`, formatted to two decimal places.
Step 11: The `for` loop continues until `i` is greater than 10.
Step 12: Return 0 from the `main` function, indicating successful program execution.

Find factorial of a number.


#include <stdio.h>
int main(){
 int num,fac=1;
    printf("Enter the number of factorial you need : ");
    scanf("%d",&num);
    for(int i=1;i<=num;i++){
        fac *= i;
    }
    printf("%d! = %d",num,fac);
   return 0;
} 

        
    
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function, which returns an integer.
Step 3: Declare integer variables `num` and `fac`, initializing `fac` to 1.
Step 4: Print the string "Enter the number of factorial you need : " to the console.
Step 5: Read an integer from the input and store it in `num` using `scanf`.
Step 6: Start a `for` loop, initializing integer `i` to 1, continuing as long as `i` is less than or equal to `num`, and incrementing `i` by 1 in each iteration.
Step 7: Inside the loop, multiply `fac` by `i` and store the result back in `fac`.
Step 8: The `for` loop continues until `i` is greater than `num`.
Step 9: Print the value of `num`, followed by the string "! = ", followed by the value of `fac` to the console.
Step 10: Return 0 from the `main` function, indicating successful program execution.

Write a program to print date and time extracted from your local system.


#include <stdio.h>
#include <math.h>
int main(){
    time_t t = time(NULL);
    struct tm curr_time = *localtime(&t);
    printf("Current Time and Date is given as :\n");
    printf("%d/%d/%d %d:%d:%d",curr_time.tm_mday,curr_time.tm_mon + 1,curr_time.tm_year + 1900,curr_time.tm_hour,curr_time.tm_min,curr_time.tm_sec);
   return 0;
}


Step 1: Include the standard input/output library `stdio.h`.
Step 2: Include the math library `math.h`. Although not used in this code, it is included.
Step 3: Define the `main` function, which returns an integer.
Step 4: Declare a variable `t` of type `time_t` and assign it the current time using the `time(NULL)` function.
Step 5: Declare a variable `curr_time` of type `struct tm` and assign it the local time representation of `t` using `localtime(&t)`, dereferenced by `*`.
Step 6: Print the string "Current Time and Date is given as :\n" to the console.
Step 7: Print the day of the month (`curr_time.tm_mday`), the month of the year (`curr_time.tm_mon` plus 1), the year (`curr_time.tm_year` plus 1900), the hour (`curr_time.tm_hour`), the minute (`curr_time.tm_min`), and the second (`curr_time.tm_sec`) in the format "day/month/year hour:minute:second" to the console.
Step 8: Return 0 from the `main` function, indicating successful program execution.

Solve the following exp with a particular value of x :
ex, sin(x), cos(x).


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

long long factorial(int n) {
    long long fact = 1;
    for (int i = 2; i <= n; i++)
        fact *= i;
    return fact;
}

int main(){
    float f1,f2,f3,x;
    f1=f2=f3= 0;
    printf("Enter the value of x : ");
    scanf("%f",&x);
    for(int i=0;i<=10;i++){
        f1 += pow(x,i)/factorial(i);
    }
    printf("The value of f1 is %.4f\n",f1);
    for(int i=0;i<=10;i++){
        f2 += pow(-1,i)*pow(x,2*i+1)/factorial(2*i+1);
    }
    printf("The value of f2 is %.4f\n",f2);
    for(int i=0;i<=10;i++){
        f3 += pow(-1,i)*pow(x,2*i)/factorial(2*i);
    }
    printf("The value of f3 is %.4f\n",f3);
    return 0;
}


Step 1: Include the standard input/output library `stdio.h`.
Step 2: Include the math library `math.h`.
Step 3: Define a function named `factorial` that takes an integer `n` as input and returns a `long long` integer.
Step 4: Inside the `factorial` function, declare a `long long` variable `fact` and initialize it to 1.
Step 5: Start a `for` loop, initializing integer `i` to 2, continuing as long as `i` is less than or equal to `n`, and incrementing `i` by 1 in each iteration.
Step 6: Inside the loop, multiply `fact` by `i` and store the result back in `fact`.
Step 7: The `for` loop continues until `i` is greater than `n`.
Step 8: Return the value of `fact` from the `factorial` function.
Step 9: Define the `main` function, which returns an integer.
Step 10: Declare float variables `f1`, `f2`, `f3`, and `x`.
Step 11: Initialize `f1`, `f2`, and `f3` to 0.
Step 12: Print the string "Enter the value of x : " to the console.
Step 13: Read a float value from the input and store it in `x` using `scanf`.
Step 14: Start a `for` loop, initializing integer `i` to 0, continuing as long as `i` is less than or equal to 10, and incrementing `i` by 1 in each iteration.
Step 15: Inside the first loop, calculate `pow(x, i) / factorial(i)` and add it to `f1`.
Step 16: The first `for` loop continues until `i` is greater than 10.
Step 17: Print the string "The value of f1 is ", followed by the value of `f1` formatted to four decimal places, followed by a newline character to the console.
Step 18: Start a second `for` loop, initializing integer `i` to 0, continuing as long as `i` is less than or equal to 10, and incrementing `i` by 1 in each iteration.
Step 19: Inside the second loop, calculate `pow(-1, i) * pow(x, 2 * i + 1) / factorial(2 * i + 1)` and add it to `f2`.
Step 20: The second `for` loop continues until `i` is greater than 10.
Step 21: Print the string "The value of f2 is ", followed by the value of `f2` formatted to four decimal places, followed by a newline character to the console.
Step 22: Start a third `for` loop, initializing integer `i` to 0, continuing as long as `i` is less than or equal to 10, and incrementing `i` by 1 in each iteration.
Step 23: Inside the third loop, calculate `pow(-1, i) * pow(x, 2 * i) / factorial(2 * i)` and add it to `f3`.
Step 24: The third `for` loop continues until `i` is greater than 10.
Step 25: Print the string "The value of f3 is ", followed by the value of `f3` formatted to four decimal places, followed by a newline character to the console.
Step 26: Return 0 from the `main` function, indicating successful program execution.

Write a program to solve approximately for the given equation :
x3 tan(x) - ex = 0.


#include <stdio.h>
#include <math.h>
#define EPSILON 0.001
#define MAX_ITER 100

double func(double x) {
    return pow(x, 3) * tan(x) - exp(x);
}

double derivFunc(double x) {
    return (3 * pow(x, 2) * tan(x)) + (pow(x, 3) * pow(1 / cos(x), 2)) - exp(x);
}

double chooseInitialGuess(double x1, double x2) {
    return fabs(func(x1)) > fabs(func(x2)) ? fabs(x2) : fabs(x1);
}

void newtonRaphson(double x0) {
    double x = x0;
    int iter = 0;
    
    while (fabs(func(x)) > EPSILON && iter < MAX_ITER) {
        if (derivFunc(x) == 0) {
            printf("Derivative is zero! Choose a different starting point.\n");
            return;
        }
        x = x - func(x) / derivFunc(x);
        iter++;
        printf("Iteration %d: x = %.6f\n", iter, x);
    }
    
    if (iter == MAX_ITER) {
        printf("\nDid not converge after %d iterations.\n", MAX_ITER);
    } else {
        printf("\nRoot found: x is nearly %.6f\n", x);
    }
}

int main() {
    double x1, x2, x0;
    printf("Enter two initial values (x1 and x2): ");
    scanf("%lf %lf", &x1, &x2);

    x0 = chooseInitialGuess(x1, x2);
    printf("Chosen initial guess: x0 = %.6f\n", x0);

    newtonRaphson(x0);
    
    return 0;
}


        
    
Step 1: This program implements the Newton-Raphson algorithm for root finding.
Step 2: Include the standard input/output library `stdio.h`.
Step 3: Include the math library `math.h`.
Step 4: Define a constant `EPSILON` with a value of 0.001.
Step 5: Define a constant `MAX_ITER` with a value of 100.
Step 6: Define a function named `func` that takes a double `x` as input and returns a double, calculating `pow(x, 3) * tan(x) - exp(x)`.
Step 7: Define a function named `derivFunc` that takes a double `x` as input and returns a double, calculating `(3 * pow(x, 2) * tan(x)) + (pow(x, 3) * pow(1 / cos(x), 2)) - exp(x)`.
Step 8: Define a function named `chooseInitialGuess` that takes two doubles `x1` and `x2` as input and returns a double, returning the absolute value of `x2` if the absolute value of `func(x1)` is greater than the absolute value of `func(x2)`, otherwise returning the absolute value of `x1`.
Step 9: Define a function named `newtonRaphson` that takes a double `x0` as input and returns nothing (void).
Step 10: Inside the `newtonRaphson` function, declare a double variable `x` and assign it the value of `x0`.
Step 11: Declare an integer variable `iter` and initialize it to 0.
Step 12: Start a `while` loop, continuing as long as the absolute value of `func(x)` is greater than `EPSILON` and `iter` is less than `MAX_ITER`.
Step 13: Inside the loop, check if `derivFunc(x)` is equal to 0.
Step 14: If `derivFunc(x)` is equal to 0, print "Derivative is zero! Choose a different starting point.\n" to the console and return from the function.
Step 15: Calculate `x = x - func(x) / derivFunc(x)`.
Step 16: Increment `iter` by 1.
Step 17: Print "Iteration ", followed by the value of `iter`, followed by ": x = ", followed by the value of `x` formatted to six decimal places, followed by a newline character to the console.
Step 18: The `while` loop continues until the condition is false.
Step 19: Check if `iter` is equal to `MAX_ITER`.
Step 20: If `iter` is equal to `MAX_ITER`, print "\nDid not converge after ", followed by the value of `MAX_ITER`, followed by " iterations.\n" to the console.
Step 21: Otherwise, print "\nRoot found: x is nearly ", followed by the value of `x` formatted to six decimal places, followed by a newline character to the console.
Step 22: Define the `main` function, which returns an integer.
Step 23: Declare double variables `x1`, `x2`, and `x0`.
Step 24: Print "Enter two initial values (x1 and x2): " to the console.
Step 25: Read two double values from the input and store them in `x1` and `x2` using `scanf`.
Step 26: Assign the result of `chooseInitialGuess(x1, x2)` to `x0`.
Step 27: Print "Chosen initial guess: x0 = ", followed by the value of `x0` formatted to six decimal places, followed by a newline character to the console.
Step 28: Call the `newtonRaphson` function with `x0` as an argument.
Step 29: Return 0 from the `main` function, indicating successful program execution.

Comments

Popular Posts