Q. Write a recursive C function to calculate the sum of digits of a number. Use this function in main to accept a number and print sum of its digits
Program
#include <stdio.h>
int sum (int a);
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}
int sum (int num)
{
if (num != 0)
{
return (num % 10 + sum (num / 10));
}
else
{
return 0;
}
}
Output:
Enter the number: 354
Sum of digits in 354 is 12
Program
#include <stdio.h>
int sum (int a);
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}
int sum (int num)
{
if (num != 0)
{
return (num % 10 + sum (num / 10));
}
else
{
return 0;
}
}
Output:
Enter the number: 354
Sum of digits in 354 is 12


10 Comments
1 csv, excel, mysql
ReplyDeletedata.csv
ID,Name,Age,Salary
1,Raj,25,30000
2,Amit,30,40000
3,Ravi,28,35000
4,Priya,32,45000
5,Neha,26,32000
6,Kiran,29,38000
data.xlsx
| ID | Name | Age | Salary |
| -- | ----- | --- | ------ |
| 1 | Raj | 25 | 30000 |
| 2 | Amit | 30 | 40000 |
| 3 | Ravi | 28 | 35000 |
| 4 | Priya | 32 | 45000 |
| 5 | Neha | 26 | 32000 |
| 6 | Kiran | 29 | 38000 |
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE employees (
emp_id INT,
name VARCHAR(50),
age INT,
salary INT
);
INSERT INTO employees VALUES
(1,'Raj',25,30000),
(2,'Amit',30,40000),
(3,'Ravi',28,35000),
(4,'Priya',32,45000),
(5,'Neha',26,32000),
(6,'Kiran',29,38000);
SELECT * FROM employees;
getwd()
setwd("C:/Users/abc/Desktop/MCA/Shardul/DA")
if (!requireNamespace("readxl", quietly = TRUE)) install.packages("readxl")
if (!requireNamespace("DBI", quietly = TRUE)) install.packages("DBI")
if (!requireNamespace("RMySQL", quietly = TRUE)) install.packages("RMySQL")
library(readxl)
library(DBI)
library(RMySQL)
data_csv <- read.csv("data.csv")
head(data_csv)
data_excel <- read_excel("data.xlsx")
head(data_excel)
con <- dbConnect(RMySQL::MySQL(),
dbname = "testdb",
host = "localhost",
user = "root",
password = "your_password")
data_db <- dbGetQuery(con, "SELECT * FROM employees")
head(data_db)
dbDisconnect(con)
2 Data Cleaning and Preprocessing
ReplyDeletedata <- data.frame(
ID = c(1, 2, 3, 4, 4, 5),
Name = c("Raj", "Amit", "Ravi", "Priya", "Priya", NA),
Age = c(25, NA, 28, 32, 32, 29),
Salary = c(30000, 40000, NA, 45000, 45000, 38000)
)
data$Age[is.na(data$Age)] <- mean(data$Age, na.rm = TRUE)
data$Salary[is.na(data$Salary)] <- mean(data$Salary, na.rm = TRUE)
data$Name[is.na(data$Name)] <- "Unknown"
data <- unique(data)
data$Name <- toupper(data$Name)
data$Salary_norm <- (data$Salary - min(data$Salary)) / (max(data$Salary) - min(data$Salary))
print(data)
3 Sampling Techniques (Training & Testing Dataset)
ReplyDeletedata <- data.frame(
ID = 1:10,
Name = c("Raj","Amit","Ravi","Priya","Neha","Kiran","Vijay","Pooja","Ajay","Rina"),
Age = c(25,30,28,32,26,29,31,27,24,33),
Salary = c(30000,40000,35000,45000,32000,38000,42000,36000,31000,47000)
)
set.seed(123)
train_index <- sample(1:nrow(data), 0.7 * nrow(data))
train_data <- data[train_index, ]
test_data <- data[-train_index, ]
print(train_data)
print(test_data)
install.packages("caTools")
library(caTools)
set.seed(123)
split <- sample.split(data$Salary, SplitRatio = 0.7)
train_data2 <- subset(data, split == TRUE)
test_data2 <- subset(data, split == FALSE)
print(train_data2)
print(test_data2)
4 Linear Regression and Model Evaluation
ReplyDeletedata <- data.frame(
Experience = c(1,2,3,4,5,6,7,8,9,10),
Salary = c(30000,35000,40000,45000,50000,55000,60000,65000,70000,75000)
)
model <- lm(Salary ~ Experience, data = data)
summary(model)
new_data <- data.frame(Experience = c(3.5, 7.5))
predictions <- predict(model, new_data)
print(predictions)
pred <- predict(model, data)
actual <- data$Salary
mse <- mean((actual - pred)^2)
rmse <- sqrt(mse)
r2 <- summary(model)$r.squared
print(mse)
print(rmse)
print(r2)
plot(data$Experience,
data$Salary,
main = "Linear Regression",
xlab = "Experience",
ylab = "Salary",
pch = 16)
abline(model, col = "red", lwd = 2)
5 Naive Bayes Classification
ReplyDeleteinstall.packages("e1071")
library(e1071)
data(iris)
set.seed(123)
index <- sample(1:nrow(iris), 0.7 * nrow(iris))
train <- iris[index, ]
test <- iris[-index, ]
model <- naiveBayes(Species ~ ., data = train)
print(model)
pred <- predict(model, test)
print(head(pred))
cm <- table(Predicted = pred, Actual = test$Species)
print(cm)
accuracy <- mean(pred == test$Species)
print(accuracy)
6 K-Nearest Neighbors (KNN)
ReplyDeleteinstall.packages("class")
library(class)
data(iris)
set.seed(123)
normalize <- function(x) {
(x - min(x)) / (max(x) - min(x))
}
iris_norm <- as.data.frame(lapply(iris[1:4], normalize))
iris_norm$Species <- iris$Species
sample_index <- sample(1:nrow(iris_norm), 0.7 * nrow(iris_norm))
train_data <- iris_norm[sample_index, 1:4]
train_label <- iris_norm[sample_index, 5]
test_data <- iris_norm[-sample_index, 1:4]
test_label <- iris_norm[-sample_index, 5]
predicted <- knn(
train = train_data,
test = test_data,
cl = train_label,
k = 5
)
cm <- table(Predicted = predicted, Actual = test_label)
print(cm)
accuracy <- sum(diag(cm)) / sum(cm)
cat("Accuracy:", accuracy * 100, "%\n")
7 K-Means Clustering and Visualization
ReplyDeleteinstall.packages("ggplot2")
library(ggplot2)
data(iris)
iris_data <- iris[, 1:4]
set.seed(123)
kmeans_result <- kmeans(
iris_data,
centers = 3,
nstart = 25
)
print(kmeans_result)
iris$Cluster <- as.factor(kmeans_result$cluster)
ggplot(
iris,
aes(
x = Sepal.Length,
y = Sepal.Width,
color = Cluster
)
) +
geom_point(size = 3) +
labs(
title = "K-Means Clustering on Iris Dataset",
x = "Sepal Length",
y = "Sepal Width"
) +
theme_minimal()
8 Exploratory Data Analysis (EDA)
ReplyDeletedata(iris)
str(iris)
summary(iris)
head(iris)
hist(
iris$Sepal.Length,
col = "lightblue",
main = "Histogram of Sepal Length",
xlab = "Sepal Length"
)
boxplot(
iris[,1:4],
col = c("red","green","blue","yellow"),
main = "Boxplot of Iris Features"
)
plot(
iris$Sepal.Length,
iris$Sepal.Width,
col = iris$Species,
pch = 19,
main = "Sepal Length vs Sepal Width",
xlab = "Sepal Length",
ylab = "Sepal Width"
)
legend(
"topright",
legend = unique(iris$Species),
col = 1:3,
pch = 19
)
9 Principal Component Analysis (PCA)
ReplyDeletedata(iris)
iris_data <- iris[, 1:4]
pca_result <- prcomp(
iris_data,
scale. = TRUE
)
summary(pca_result)
print(pca_result$rotation)
head(pca_result$x)
plot(
pca_result,
type = "l",
main = "Scree Plot - PCA"
)
10 Classification Tree using rpart
ReplyDeleteinstall.packages("rpart")
install.packages("rpart.plot")
library(rpart)
library(rpart.plot)
data(iris)
set.seed(123)
sample_index <- sample(1:nrow(iris), 0.7 * nrow(iris))
train_data <- iris[sample_index, ]
test_data <- iris[-sample_index, ]
model <- rpart(
Species ~ .,
data = train_data,
method = "class"
)
print(model)
rpart.plot(
model,
main = "Decision Tree - Iris Dataset",
type = 2,
extra = 104,
fallen.leaves = TRUE
)
predicted <- predict(
model,
test_data,
type = "class"
)
cm <- table(
Predicted = predicted,
Actual = test_data$Species
)
print(cm)
accuracy <- sum(diag(cm)) / sum(cm)
cat("Accuracy:", accuracy * 100, "%\n")