0% found this document useful (0 votes)
22 views9 pages

CP Final Lab

The document contains code for a C program that calculates the difference between two dates provided by the user in days, years, months, and days format. It also determines the day of the week, month, year characteristics (leap year or not), and writes out the year in English for the later date. The program takes in the two dates, start and end, as well as the day of week for the start date. It then calculates the date difference in multiple formats and outputs the results.

Uploaded by

priyanshu rane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views9 pages

CP Final Lab

The document contains code for a C program that calculates the difference between two dates provided by the user in days, years, months, and days format. It also determines the day of the week, month, year characteristics (leap year or not), and writes out the year in English for the later date. The program takes in the two dates, start and end, as well as the day of week for the start date. It then calculates the date difference in multiple formats and outputs the results.

Uploaded by

priyanshu rane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Name Priyanshu Rane

Branch – ECE

MIS- 112116032

Question-1
#include <stdio.h>
int main()
{
    printf("++ Enter the Order of Fibonacci Sequence: ");
    int a, i;
    scanf("%d", &a);
    printf("++ Enter Initial/Base Terms (space-seperated): ");

    // Defining array for taking inputs


    int b[a];

    // Now making use of a for loop to take input from user


    for (i = 0; i < a; i++)
    {
        scanf("%d", &b[i]);
    }

    // Take input of length


    int length;
    printf("++ Enter number of terms in sequence to view: ");
    scanf("%d", &length);

    // Now making use of while loop to print each number of the Fibonacci
series
    printf("\n** The Fibonacci sequence upto %d-th term is:\n", length);
    int final[length];
    for (i = 0; i < a; i++)
    {
        final[i] = b[i];
        printf("%d ", final[i]);
    }

    int curr_len = a;
    while (curr_len < length)
    {
        // Using for loop inside the while to calculate each term of series
        int sum = 0;
        for (i = curr_len - a; i < curr_len; i++)
        {
            sum += final[i];
        }
        final[curr_len] = sum;
        printf("%d ", final[curr_len]);
        curr_len++;
    }

    return 0;
}

Output

:++ Enter Order of Fibonacci Sequence: 2


++ Enter Initial/Base Terms (space-seperated): 1 2 3
++ Enter number of terms in sequence to view:3
** The Fibonacci sequence upto 3-th term is:
1 2 3
--------------------------------
Process exited after 7.357 seconds with return value 0
Press any key to continue . .

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

/* run this program using the console pauser or add your own getch,
system("pause") or input loop */

int main(int argc, char *argv[]) {


         
    int startDate, startMonth, startYear;
    int endDate, endMonth, endYear;
    unsigned long long int dateDiff;
    unsigned int startDay, dDiff, mDiff, yDiff;
   
    // taking as input two dates from user
    printf("** User Input of Date Details **\n");
    printf("\t++ Enter First (earlier) Date (DD/MM/YYYY format): ");
    scanf("%d/%d/%d", &startDate, &startMonth, &startYear);
    printf("\t++ Enter Second (later) Date (DD/MM/YYYY format): ");
    scanf("%d/%d/%d", &endDate, &endMonth, &endYear);
    printf("\t++ Enter the Day of the Week for First Date (0-6 for Sun-Sat):
");
    scanf("%u", &startDay);
   
    // calculating date difference
    if(endDate >= startDate) { // end date is beyond start date
        dateDiff = endDate - startDate;
    }
    else { // end date is less than start date -- borrow days of next month
        dateDiff = endDate;
        if(startMonth <= 6) { // adding 31 day months
            dateDiff = dateDiff + (31 - startDate);
        }
        else { // adding 30 day months
            dateDiff = dateDiff + (30 - startDate);
            if((startMonth == 11) && !((startYear%400 == 0) || ((startYear%100
!= 0) && (startYear%4 == 0)))) {
                // adjusting non-leap year month having 29 days
                dateDiff = dateDiff - 1;
            }
        }
       
        // start month incremented due to days of one month borrowed while
subtraction
        if(startMonth == 12) {
            startMonth = 1;
            startYear = startYear + 1;
        }
        else {
            startMonth = startMonth + 1;
        }
    }
    dDiff = dateDiff; // keeping the date difference
   
    // calculating month difference
    if(endMonth >= startMonth) { // end month is beyond start month
        mDiff = (endMonth - startMonth); // keeping month difference
        if(endMonth <= 7) { // adding 31 day months
            dateDiff = dateDiff + mDiff*31;
        }
        else { // adding 30 day months
            dateDiff = dateDiff + mDiff*30;
            if(startMonth <= 6) {
                dateDiff = dateDiff + (7 - startMonth);
            }
            if(!((startYear%400 == 0) || ((startYear%100 != 0) && (startYear%4
== 0)))
               && (endMonth == 12) && (startMonth <= 11)) {
                // adjusting non-leap year month having 29 days
                dateDiff = dateDiff - 1;
            }
        }
    }
    else { // end month is less than start month -- borrow days of next year
        mDiff = 12 + (endMonth - startMonth); // keeping month difference
borrowing 12 months from next year
        if(startMonth <= 6) { // start month is within 31 day months zone
            dateDiff = dateDiff + (7-startMonth)*31 + 6*30;
        }
        else { // start month is within 30 day months zone
            dateDiff = dateDiff + (13-startMonth)*30;
        }
        if((startMonth < 12) && !((startYear%400 == 0) || ((startYear%100 !=
0) && (startYear%4 == 0)))) {
            // adjusting non-leap year month having 29 days
            dateDiff = dateDiff - 1;
        }
       
        if(endMonth <= 6) { // end month is within 31 day months zone
            dateDiff = dateDiff + (endMonth-1)*31;
        }
        else { // end month is within 30 day months zone
            dateDiff = dateDiff + (endMonth-7)*30 + 6*31;
        }
        if((endMonth == 12) && !((startYear%400 == 0) || ((startYear%100 != 0)
&& (startYear%4 == 0)))) {
            // adjusting non-leap year month having 29 days
            dateDiff = dateDiff - 1;
        }
       
        // start year incremented due to days of one month borrowed while
subtraction
        startYear = startYear + 1;
    }
   
    // calculating year difference
    yDiff = (endYear - startYear);
    if(yDiff > 0){ // computing days w.r.t. years only if years are apart
(not-same)
      dateDiff = dateDiff + yDiff*365; // adding 365 days years
     
      // adding additional days for leap-years
      if(endMonth < 12){ // end year not to be counted for leap-year day
        dateDiff = dateDiff + ((endYear-1)/4 - (startYear-1)/4);
      }
      else{ // end year leap-year day to be counted
        dateDiff = dateDiff + (endYear/4 - startYear/4);
      }
   
      // subtracting extra-added days for non-leap years
      // (those are divisible by 100 but not divisible by 400)
      // FIRST: subtract -1 for all 100 divisible years
      if(endMonth < 12){ // end year not to be discounted for leap-year day
        dateDiff = dateDiff - ((endYear-1)/100 - (startYear-1)/100);
      }
      else{ // end year leap-year day to be discounted
        dateDiff = dateDiff - (endYear/100 - startYear/100);
      }
      // NEXT: add +1 for all 400 divisible years (due to previous additional
subtractions made)
      if(endMonth < 12){ // end year not to be counted for leap-year day
        dateDiff = dateDiff + ((endYear-1)/400 - (startYear-1)/400);
      }
      else{ // end year leap-year day to be counted
        dateDiff = dateDiff + (endYear/400 - startYear/400);
      }
    }
   
    printf("\n** The Date Difference **");
   
    // printing the date difference in terms of number of days
    printf("\n\t++ In DAYS (only): %llu days", dateDiff);
   
    // printing the date difference in terms of number of years, months and
days
    printf("\n\t++ With BREAK-UP: %u years, %u months, %u days", yDiff, mDiff,
dDiff);
   
    // printing the date difference in terms of number of weeks and days
    printf("\n\t++ In WEEK-DAYS: %llu weeks and %llu days", dateDiff/7,
dateDiff%7);
   
    printf("\n\n** The Second Date Status **");
    printf("\n\t++ The Day of the Week: ");
    switch((dateDiff%7 + startDay)%7) { // calculating the week-day
        case 0: printf("Sunday");    break;
        case 1: printf("Monday");    break;
        case 2: printf("Tuesday");   break;
        case 3: printf("Wednesday"); break;
        case 4: printf("Thursday");  break;
        case 5: printf("Friday");    break;
        case 6: printf("Saturday");  break;
    }
    printf("\n\t++ The Name of the Month: ");
    switch(endMonth) { // printing the month name
        case  1: printf("january");   break;
        case  2: printf("february");   break;
        case  3: printf("march");    break;
        case  4: printf("april");  break;
        case  5: printf("may");     break;
        case  6: printf("june");    break;
        case  7: printf("july");    break;
        case  8: printf("august"); break;
        case  9: printf("september");     break;
        case 10: printf("octover");      break;
        case 11: printf("november");   break;
        case 12: printf("december");    break;
    }
    printf("\n\t++ The Year Characteristics: ");
    if(((endYear%400 == 0) || ((endYear%100 != 0) && (endYear%4 == 0)))) {
        // check for leap-year
        printf("LEAP-YEAR!");
    }
    else { // not a leap-year
        printf("NOT LEAP-YEAR!");
    }
    printf("\n\t++ The Year in English: ");
    if(endYear == 0) { // end year is zero
        printf("Zero");
    }
    else { // non-zero end year
        switch(endYear/1000) { // end year is beyond 1000
            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:                   break;  // do nothing
        }
        if(endYear/1000 > 0) {
            printf("Thousand ");
        }
        switch((endYear%1000)/100) { // the hundred-th part of end year
            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:                   break;  // do nothing
        }
        if((endYear%1000)/100 > 0) {
            printf("Hundred ");
        }
        if((endYear%100)/10 == 1){ /* YY within 10-19 */
            switch (endYear%10){ /* word generation for 10-19 */
                case 0: printf("Ten");       break;
                case 1: printf("Eleven");    break;
                case 2: printf("Twelve");    break;
                case 3: printf("Thirteen");  break;
                case 4: printf("Forteen");   break;
                case 5: printf("Fifteen");   break;
                case 6: printf("Sixteen");   break;
                case 7: printf("Seventeen"); break;
                case 8: printf("Eighteen");  break;
                case 9: printf("Nineteen");  break;
            }
        }
        else { /* YY other than 10-19 */
            switch ((endYear%100)/10){ /* word generation for decimal part */
                case 2: printf("Twenty ");  break;
                case 3: printf("Thirty ");  break;
                case 4: printf("Forty ");   break;
                case 5: printf("Fifty ");   break;
                case 6: printf("Sixty ");   break;
                case 7: printf("Seventy "); break;
                case 8: printf("Eighty ");  break;
                case 9: printf("Ninety ");  break;
                default: /* do nothing */   break;
            }
            switch (endYear%10) { /* word generation for unitary part */
                case 0: printf("\b");       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;
            }
        }
    }
    printf("\n");
   
    return 0;
}
OUTPUT:** User Input of Date Details **
        ++ Enter First (earlier) Date (DD/MM/YYYY format): 01/01/2003
        ++ Enter Second (later) Date (DD/MM/YYYY format): 02/04/2004
        ++ Enter the Day of the Week for First Date (0-6 for Sun-Sat): 0

** The Date Difference **


        ++ In DAYS (only): 459 days
        ++ With BREAK-UP: 1 years, 3 months, 1 days
        ++ In WEEK-DAYS: 65 weeks and 4 days

** The Second Date Status **


        ++ The Day of the Week: Thursday
        ++ The Name of the Month: april
        ++ The Year Characteristics: LEAP-YEAR!
        ++ The Year in English: Two Thousand Four

--------------------------------
Process exited after 20.25 seconds with return value 0
Press any key to continue . . .

Q_4

For q1

1. In this code 1st we take input of how many number of Fibonacci


series we know

2. Then input that and create an array to print the output

3. Then we use for loop for taking input from the user next we
define the input of length

4. Next used while loop to print each number of Fibonacci series


and inside it we run a for loop to print the terms in Fibonacci
series.

5. Next by applying the logic for fibbonacci series we we complete


the program.

For q2

1. First we declare the variables int startDate, startMonth,


startYear;

int endDate, endMonth, endYear;


unsigned long long int dateDiff;

unsigned int startDay, dDiff, mDiff, yDiff

2. Then we take the input from the used by using scanf for of all
these above variables

3. Then we use if else blocks to verify wethere the moth is of 30


days or 31 days.

4. Then again using if else block we check and count the days weeks
.

5. And switch cases for the printing the year.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy