0% found this document useful (0 votes)
3 views6 pages

Programs

The document describes various simple AI programs demonstrating fundamental concepts such as rule-based reasoning, pattern matching, logical inference, expert systems, and machine learning. Each program includes an explanation of its function, the AI concept used, and a brief overview of its working mechanism. Examples include a maximum finder, chatbot, even/odd checker, medical diagnosis system, recommendation system, linear regression, BFS algorithm, perceptron, and A* algorithm.

Uploaded by

itsmisthyrastogi
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)
3 views6 pages

Programs

The document describes various simple AI programs demonstrating fundamental concepts such as rule-based reasoning, pattern matching, logical inference, expert systems, and machine learning. Each program includes an explanation of its function, the AI concept used, and a brief overview of its working mechanism. Examples include a maximum finder, chatbot, even/odd checker, medical diagnosis system, recommendation system, linear regression, BFS algorithm, perceptron, and A* algorithm.

Uploaded by

itsmisthyrastogi
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

Find Maximum of Two Numbers (Simple Rule-Based AI)

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))

if a > b:
print("Maximum is:", a)
else:
print("Maximum is:", b)

Explanation:
This program compares two numbers using if–else rules. The system follows predefined rules to
decide which number is larger.

AI Concept Used:
Rule-based reasoning (basic decision-making)

Working:

● Read two numbers


● Apply comparison rule
● Display the maximum value

Simple Chatbot Using If–Else


user = input("You: ").lower()

if "hello" in user:
print("Bot: Hello! How can I help you?")
elif "name" in user:
print("Bot: I am a simple AI chatbot.")
elif "bye" in user:
print("Bot: Goodbye!")
else:
print("Bot: Sorry, I didn't understand.")

Explanation:
This program acts as a basic chatbot. It checks the user’s input and responds using predefined
conditions. Though simple, it represents the foundation of Natural Language Processing
(NLP).
AI Concept Used:
Pattern matching and basic NLP
Working:
● Take user input as text
● Match keywords using conditions
● Return a suitable response

Even or Odd (AI Logic Program)


n = int(input("Enter a number: "))

if n % 2 == 0:
print("Even number")
else:
print("Odd number")
Explanation:
This program determines whether a number is even or odd using logical reasoning. It uses
mathematical rules to make a decision.
AI Concept Used:
Logical inference
Working:
● Input a number
● Check divisibility by 2
● Display result

Simple Expert System (Medical Diagnosis)


fever = input("Do you have fever? (yes/no): ")
cough = input("Do you have cough? (yes/no): ")

if fever == "yes" and cough == "yes":


print("Diagnosis: You may have flu.")
else:
print("Diagnosis: Symptoms are unclear.")
Explanation:
This program mimics a human expert by asking questions and giving a diagnosis based on user
responses. It uses IF–THEN rules, which is the core idea behind expert systems.
AI Concept Used:
Expert systems and knowledge-based reasoning
Working:
● Ask symptoms
● Apply predefined rules
● Generate diagnosis

Guess the Number (Learning Through Feedback)


secret = 7
guess = int(input("Guess the number: "))

if guess == secret:
print("Correct guess!")
else:
print("Wrong guess!")
Explanation:
This program allows interaction between the user and system. The system checks user feedback
to determine whether the guess is correct.
AI Concept Used:
Learning through feedback
Working:
● Store a secret number
● Accept user guess
● Compare and respond

Simple Recommendation System


choice = input("Do you like movies or books? ")

if choice == "movies":
print("Recommendation: Watch an AI sci-fi movie.")
else:
print("Recommendation: Read an AI book.")

Explanation:
This program suggests content based on user preference. Though basic, it demonstrates the idea
behind modern AI recommendation engines.
AI Concept Used:
Decision-based recommendation logic
Working:
● Ask user preference
● Match preference with recommendation
● Display result

Linear Regression (Very Basic ML Example)


from sklearn.linear_model import LinearRegression

X = [[1], [2], [3], [4]]


y = [2, 4, 6, 8]

model = LinearRegression()
[Link](X, y)

print([Link]([[5]]))

Explanation:
This program uses machine learning to find the relationship between input and output data. It
predicts future values based on learned patterns.
AI Concept Used:
Supervised Machine Learning
Working:
● Provide training data
● Train regression model
● Predict output for new input

Simple BFS Algorithm (AI Search)


graph = {
'A': ['B', 'C'],
'B': ['D'],
'C': ['E'],
'D': [],
'E': []
}

visited = []

def bfs(visited, graph, node):


queue = [node]
[Link](node)

while queue:
m = [Link](0)
print(m, end=" ")
for n in graph[m]:
if n not in visited:
[Link](n)
[Link](n)

bfs(visited, graph, 'A')


Explanation:
BFS is a graph search algorithm that explores nodes level by level. It is commonly used in AI for
path finding and state-space search.
AI Concept Used:
Search algorithms
Working:
● Start from initial node
● Visit all neighbors
● Continue level-wise traversal

Simple Perceptron (Basic Neural Network)


import numpy as np

inputs = [Link]([[0,0],[0,1],[1,0],[1,1]])
outputs = [Link]([0,0,0,1]) # AND gate

weights = [Link](2)
bias = 0.5

for _ in range(1000):
for i in range(len(inputs)):
summation = [Link](inputs[i], weights) + bias
prediction = 1 if summation > 1 else 0
error = outputs[i] - prediction
weights += error * inputs[i]

print(weights)

Explanation:
The perceptron is the simplest neural network model. It learns weights based on error correction
and is used for binary classification.
AI Concept Used:
Artificial Neural Networks
Working:
● Initialize weights
● Calculate output
● Update weights using error
● Repeat until learning stabilizes

Simple A* Algorithm (Path Finding)


def heuristic(a, b):
return abs(a - b)

print("Basic heuristic function example")

Explanation:
A heuristic function estimates the cost to reach a goal state. It helps AI systems choose the most
efficient path.
AI Concept Used:
Heuristic search
Working:
● Calculate estimated distance
● Guide the search process

You might also like