0% found this document useful (0 votes)
7 views5 pages

Class Notes On Programming

The document provides a series of C programming ideas, including a text-based game, a simple text adventure, and an age calculator. It explains how to implement these programs using basic input/output, conditional statements, and loop statements. Additionally, it discusses how to work with real-time data using the time.h library in C.

Uploaded by

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

Class Notes On Programming

The document provides a series of C programming ideas, including a text-based game, a simple text adventure, and an age calculator. It explains how to implement these programs using basic input/output, conditional statements, and loop statements. Additionally, it discusses how to work with real-time data using the time.h library in C.

Uploaded by

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

Skip to content

Chat history

You said:
give me an idea of a program to make in c language that includes basic input output
and conditional statement and loop statement, a program that seems impossible but
its actually possible
ChatGPT said:
How about creating a program in C that simulates a simple text-based game of "Rock,
Paper, Scissors, Lizard, Spock"? This game adds a few extra options to the
traditional Rock, Paper, Scissors game, making it a bit more interesting. It
involves basic input/output, conditional statements, and loop statements. Here's an
outline of how you can approach this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
int playerChoice, computerChoice;
int playAgain = 1;

while (playAgain) {
printf("Welcome to Rock, Paper, Scissors, Lizard, Spock!\n");
printf("Choose your move:\n");
printf("1. Rock\n2. Paper\n3. Scissors\n4. Lizard\n5. Spock\n");

// Get the player's choice


scanf("%d", &playerChoice);

// Generate a random choice for the computer


srand(time(NULL));
computerChoice = rand() % 5 + 1;

// Check who wins


if (playerChoice < 1 || playerChoice > 5) {
printf("Invalid choice. Please choose a number between 1 and 5.\n");
} else {
printf("Computer chose: %d\n", computerChoice);

if (playerChoice == computerChoice) {
printf("It's a tie!\n");
} else if ((playerChoice == 1 && (computerChoice == 3 || computerChoice
== 4)) ||
(playerChoice == 2 && (computerChoice == 1 || computerChoice
== 5)) ||
(playerChoice == 3 && (computerChoice == 2 || computerChoice
== 4)) ||
(playerChoice == 4 && (computerChoice == 2 || computerChoice
== 5)) ||
(playerChoice == 5 && (computerChoice == 1 || computerChoice
== 3))) {
printf("You win!\n");
} else {
printf("Computer wins!\n");
}
}

printf("Do you want to play again? (1 for yes, 0 for no): ");
scanf("%d", &playAgain);
}

printf("Thanks for playing!\n");


return 0;
}
This program allows the user to play multiple rounds of the game and uses
conditional statements to determine the winner of each round. It also includes a
loop to keep the game going until the user decides to quit. While it may seem
complex at first, it's entirely possible to implement in C.

You said:
give me an idea of a program to make in c language that only includes basic input
output and conditional statement and loop statement, a program that seems
impossible but its actually possible and it doesnt include time
ChatGPT said:
Certainly! Here's a C program that uses basic input/output, conditional statements,
and loop statements to implement a "Simple Text Adventure Game" without using the
time function. In this game, the player makes choices to navigate through a simple
story:

#include <stdio.h>

int main() {
int choice;

printf("Welcome to the Simple Text Adventure Game!\n");


printf("You find yourself in a dark room with two doors.\n");
printf("Choose a door to enter (1 or 2): ");
scanf("%d", &choice);

if (choice == 1) {
printf("You enter a room filled with treasure. You win!\n");
} else if (choice == 2) {
printf("You enter a room with a hungry lion. You lose!\n");
} else {
printf("Invalid choice. You are lost!\n");
}

return 0;
}
In this program, the user makes a choice (1 or 2) to select a door. Based on the
choice, the program uses conditional statements to determine the outcome. It
doesn't use the time function, making it a simple yet interactive text-based
adventure game.

You said:
give me another idea, an idea like age calculator, at first i thought it was
impossible but i eventually made it,
ChatGPT said:
How about creating a C program that calculates and displays the day of the week for
a given date? Calculating the day of the week for a given date might seem
challenging at first, but it's possible to implement this without using external
libraries. Here's a basic outline for such a program:

#include <stdio.h>

int main() {
int day, month, year;
// Input date from the user
printf("Enter a date (dd mm yyyy): ");
scanf("%d %d %d", &day, &month, &year);

// Validate the input (basic validation)


if (day < 1 || day > 31 || month < 1 || month > 12) {
printf("Invalid date!\n");
return 1; // Exit with an error code
}

// Zeller's Congruence algorithm for calculating the day of the week


if (month < 3) {
month += 12;
year--;
}

int k = year % 100;


int j = year / 100;

int dayOfWeek = (day + 13 * (month + 1) / 5 + k + k / 4 + j / 4 - 2 * j) % 7;

// Map the result to the corresponding day of the week


switch (dayOfWeek) {
case 0:
printf("Saturday\n");
break;
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
default:
printf("Invalid result!\n");
break;
}

return 0;
}
This program takes a date as input and uses Zeller's Congruence algorithm to
calculate the day of the week for that date. While it may seem complicated, it's
indeed possible to implement with basic input/output, conditional statements, and
loops.

You said:
write an age calculator in c
ChatGPT said:
Sure, here's a simple age calculator program in C that calculates a person's age
based on their birthdate and the current date:

#include <stdio.h>

int main() {
int birthDay, birthMonth, birthYear;
int currentDay, currentMonth, currentYear;
int age;

// Input birthdate from the user


printf("Enter your birthdate (dd mm yyyy): ");
scanf("%d %d %d", &birthDay, &birthMonth, &birthYear);

// Input current date


printf("Enter the current date (dd mm yyyy): ");
scanf("%d %d %d", &currentDay, &currentMonth, &currentYear);

// Calculate age
age = currentYear - birthYear;

// Adjust age if the birthday hasn't occurred yet this year


if (currentMonth < birthMonth || (currentMonth == birthMonth && currentDay <
birthDay)) {
age--;
}

// Display the calculated age


printf("Your age is: %d years\n", age);

return 0;
}
This program takes the birthdate and the current date as input and calculates the
age based on the birthdate. It accounts for whether the birthday has occurred this
year or not.

You said:
how do i add real time in c
ChatGPT said:
To work with real-time data and time-related operations in C, you can use the
time.h library. The time.h library provides functions for accessing and
manipulating time and date information. Here's how you can work with real-time data
in C:

Include the time.h library in your C program:

#include <stdio.h>
#include <time.h>
You can use the time() function to get the current time in seconds since the Unix
epoch (January 1, 1970). Here's an example:

time_t current_time;
current_time = time(NULL);
You can use the ctime() function to convert the time in seconds to a human-readable
string:

char* c_time_string;
c_time_string = ctime(&current_time);
printf("Current time is: %s", c_time_string);
To perform more advanced time-related operations, you can use structures like
struct tm. For example, you can extract the current year, month, day, and time:

struct tm* time_info;


time_info = localtime(&current_time);

int year = time_info->tm_year + 1900; // Year is since 1900


int month = time_info->tm_mon + 1; // Month is 0-based
int day = time_info->tm_mday;
int hour = time_info->tm_hour;
int minute = time_info->tm_min;
int second = time_info->tm_sec;

printf("Current date and time: %d-%02d-%02d %02d:%02d:%02d\n", year, month, day,


hour, minute, second);
You can also perform arithmetic and comparisons with time_t values to calculate
time intervals or compare dates.

Here's a simple example that prints the current time and date:

#include <stdio.h>
#include <time.h>

int main() {
time_t current_time;
current_time = time(NULL);

char* c_time_string;
c_time_string = ctime(&current_time);

struct tm* time_info;


time_info = localtime(&current_time);

int year = time_info->tm_year + 1900;


int month = time_info->tm_mon + 1;
int day = time_info->tm_mday;
int hour = time_info->tm_hour;
int minute = time_info->tm_min;
int second = time_info->tm_sec;

printf("Current date and time: %d-%02d-%02d %02d:%02d:%02d\n", year, month,


day, hour, minute, second);

return 0;
}
This program will display the current date and time when you run it.

No file chosenNo file chosen


ChatGPT can make mistakes. Check important info.

You might also like