0% found this document useful (0 votes)
48 views28 pages

Barkha

Uploaded by

techcuber17
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)
48 views28 pages

Barkha

Uploaded by

techcuber17
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/ 28

Name: Barkha Khatri

REG ID:67855

P.F LAB TASK

#include <stdio.h>

#include <string.h>

void create_account() {

char username[50], password[50];

int age;

printf("Enter your username: ");

scanf("%s", username);

printf("Enter your password: ");

scanf("%s", password);

printf("Enter your age: ");

scanf("%d", &age);

// Check if the user is above 18

if (age > 18) {

printf("Account created successfully for %s!\n", username);

} else {

// User is under 18

printf("Account creation failed. You must be above 18 years old to create an account.\n");

}
}

int main() {

create_account();

return 0;

Problem02(login.sb)
Takeausernameandpasswordas inputandvalidateifpasswordisequalsto12345thenlogintothesystem.
#include <stdio.h>

#include <string.h>

void login() {

char username[50], password[50];

printf("Enter your username: ");

scanf("%s", username);

printf("Enter your password: ");


scanf("%s", password);

// Validate if the password is correct

if (strcmp(password, "12345") == 0) {

printf("Login successful! Welcome %s.\n", username);

} else {

printf("Invalid password. Login failed.\n");

int main() {

login();

return 0;

Problem03(change.sb)
Take a number as input if the number is less than 0 then change the color of sprite else change
the positionof thespriteby2 pointsbyy.
Assignment 02
Create script to generate three numbers randomly and set to the variables (guess1, guess2
guess3) respectively after that play a game if the all three generated number are equal then you
win
the game otherwise lose.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main() {

// Seed the random number generator with the current time

srand(time(0));

// Generate three random numbers between 1 and 10 (inclusive)

int guess1 = rand() % 10 + 1;

int guess2 = rand() % 10 + 1;

int guess3 = rand() % 10 + 1;

// Display the generated numbers


printf("Generated numbers: %d, %d, %d\n", guess1, guess2, guess3);

// Check if all three numbers are equal

if (guess1 == guess2 && guess2 == guess3) {

printf("You win the game! All numbers are equal.\n");

} else {

printf("You lose the game. The numbers are not equal.\n");

return 0;

Problem02 (circle.sb)

Create a script to circle shape with pen size 5 points


ANS= when green flag clicked
set pen size to 5
pen down
repeat 36
move 10 steps
turn clockwise 10 degrees

Problem03 (voteCasting.sb)
Create script to take age of the person as input and determine whether it is eligible
for casting his/her own vote.

Note: (the person must be 18 years or above to be eligible for)


when green flag clicked

ask [What is your age?] and wait

if <(answer) >= 18> then

say [You are eligible to vote!] for 2 seconds

else

say [You are not eligible to vote.] for 2 seconds

Assignment 03
1) Write a C program to convert temperature from Celsius to Fahrenheit. Use the formula:
Fahrenheit = (Celsius * 9/5) + 32

#include <stdio.h>

int main() {

float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9 / 5) + 32;

printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);

return 0;
}

2) Write a C program to calculate the volume of a cube. The volume of a cube is given by:
Volume = side * side * side
#include <stdio.h>

int main() {
float side, volume;

printf("Enter the side length of the cube: ");


scanf("%f", &side);

volume = side * side * side;

printf("The volume of the cube is: %.2f\n", volume);

return 0;
}
3) Write a C program to take three numbers as input and calculate their sum and average.

#include <stdio.h>

int main() {

float num1, num2, num3, sum, average;

printf("Enter three numbers: ");

scanf("%f %f %f", &num1, &num2, &num3);

sum = num1 + num2 + num3;

average = sum / 3;

printf("Sum of the numbers: %.2f\n", sum);

printf("Average of the numbers: %.2f\n", average);

return 0;

4) Write a C program to convert a given number of days into years, weeks, and days.
#include <stdio.h>

int main() {
inttotal_days, years, weeks, days;

printf("Enter the number of days: ");


scanf("%d", &total_days);
years = total_days / 365; // Assuming a year has 365 days
total_days = total_days % 365; // Remaining days after extracting years

weeks = total_days / 7; // Calculate weeks


days = total_days % 7; // Remaining days after extracting weeks

printf("%d days is equivalent to:\n", total_days + (years * 365) + (weeks * 7));


printf("%d years, %d weeks, and %d days.\n", years, weeks, days);

return 0;
}

Assignment 04
1. Write a C program that asks the user for their age and uses an if-else statement to check if they are
eligible to vote (18 years or older). Print a message indicating eligibility.

#include <stdio.h>

int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote!\n");
} else {
printf("You are not eligible to vote.\n");
}

return 0;
}
2. Write a program to check whether a year entered by the user is a leap year or not using nested if-else.

#include <stdio.h>

int main() {
int year;

// Ask the user to enter a year


printf("Enter a year: ");
scanf("%d", &year);

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
} else {
// If divisible by 4 but not by 100
printf("%d is a leap year.\n", year);
}
} else {
// If not divisible by 4
printf("%d is not a leap year.\n", year);
}

return 0;
}

3. Write a simple C program using cascaded if-else statements to perform addition, subtraction,
multiplication, or division based on user input.
#include <stdio.h>

int main() {
int num1, num2;
char operator;
printf("Enter first number: ");
scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

printf("Enter operator (+, -, *, /): ");


scanf(" %c", &operator); // Space before %c to consume any leftover newline

if (operator == '+') {
printf("%d + %d = %d\n", num1, num2, num1 + num2);
} else if (operator == '-') {
printf("%d - %d = %d\n", num1, num2, num1 - num2);
} else if (operator == '*') {
printf("%d * %d = %d\n", num1, num2, num1 * num2);
} else if (operator == '/') {
if (num2 != 0) {
printf("%d / %d = %.2f\n", num1, num2, (float)num1 / num2);
} else {
printf("Error! Division by zero is not allowed.\n");
}
} else {
printf("Invalid operator! Please use +, -, *, or /.\n");
}

return 0;
}

4. Write a C program to input the coefficients a, b and c of a quadratic equation in the form
ax2 + bx+ c=0.Using a cascaded if-else statement, calculate the discriminant and check the nature of
the roots. The program should indicate whether the roots are real and distinct, real and equal, or
imaginary.
#include <stdio.h>
#include <math.h>

intmain() {

double a, b, c, discriminant, root1, root2;

printf("Enter the coefficients a, b and c of the quadratic equation ax^2 + bx + c = 0\n");


printf("a = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
printf("c = ");
scanf("%lf", &c);

if (a == 0) {
printf("Invalid input! 'a' cannot be 0 for a quadratic equation.\n");
} else {

discriminant = b * b - 4 * a * c;

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);


root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The roots are real and distinct.\n");
printf("Root 1 = %.2lf\n", root1);
printf("Root 2 = %.2lf\n", root2);
} else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("The roots are real and equal.\n");
printf("Root 1 = Root 2 = %.2lf\n", root1);
} else {

printf("The roots are imaginary.\n");


}
}

return 0;
}

Assignment 05
● Question 1: Write a program to prompt the user to enter a number (1–4), where each number
represents a season (1 for Spring, 2 for Summer, etc.). Use a switch-case to display the season
name based on the user's input.

#include <stdio.h>

intmain() {
int season;

// Prompt the user to enter a number (1–4)

printf("Enter a number (1-4) to represent a season:\n");

printf("1 for Spring\n2 for Summer\n3 for Autumn\n4 for Winter\n");

scanf("%d", &season);

// Use a switch-case statement to display the season name

switch (season) {

case 1:

printf("Spring\n");

break;

case 2:

printf("Summer\n");

break;

case 3:

printf("Autumn\n");

break;

case 4:

printf("Winter\n");
break;

default:

printf("Invalid input! Please enter a number between 1 and 4.\n");

break;

return 0;

● Question 2: Write a program that takes a character input from the user and uses a switch-case to
check if the letter is a vowel (a, e, i, o, u). If it’s a vowel, print "Vowel"; otherwise, print
"Consonant."

#include <stdio.h>

#include <ctype.h>

intmain() {
char input;

printf("Enter a single character: ");

scanf(" %c", &input);

input = tolower(input);

if (!isalpha(input)) {

printf("Invalid input. Please enter an alphabet letter.\n");

} else {

switch (input) {

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':
printf("Vowel\n");

break;

default:

printf("Consonant\n");

return 0;

● Question 3: Write a program that asks the user to choose a conversion type:
o 1: Kilometers to Miles
o 2: Meters to Feet
o 3: Centimeters to Inches
Based on the user’s choice, use a switch-case to perform the selected conversion and
display the result.

#include <stdio.h>
intmain() {

int choice;

float value, result;

// Display the menu for conversion types

printf("Choose a conversion type:\n");

printf("1: Kilometers to Miles\n");

printf("2: Meters to Feet\n");

printf("3: Centimeters to Inches\n");

printf("Enter your choice (1-3): ");

scanf("%d", &choice);

// Perform conversion based on the user's choice

switch (choice) {

case 1:

printf("Enter the value in kilometers: ");

scanf("%f", &value);

result = value * 0.621371; // Conversion factor

printf("%.2f kilometers is equal to %.2f miles.\n", value, result);

break;

case 2:

printf("Enter the value in meters: ");

scanf("%f", &value);

result = value * 3.28084; // Conversion factor

printf("%.2f meters is equal to %.2f feet.\n", value, result);

break;

case 3:

printf("Enter the value in centimeters: ");

scanf("%f", &value);
result = value * 0.393701; // Conversion factor

printf("%.2f centimeters is equal to %.2f inches.\n", value, result);

break;

default:

printf("Invalid choice! Please select 1, 2, or 3.\n");

return 0;

Assignment 06
Write a program that takes a series of integer inputs from the user and performs the
following:
 Use a while loop to keep accepting input until the user enters a zero (0) to stop.

 Use a do while loop to display each entered number until zero is encountered.

 Use a for loop to display the total count of numbers entered, excluding zero.

LAB TASKS:
Task 01:
Sum of Integers Using While Loop

Write a program that prompts the user to enter a positive integer and calculates the sum
of all integers from 1 to that number using a while loop.
#include <stdio.h>

intmain() {

int number, sum = 0, i = 1;

printf("Enter a positive integer: ");

scanf("%d", &number);

if (number <= 0) {

printf("Invalid input. Please enter a positive integer.\n");

} else {

while (i<= number) {

sum += i;

i++;

printf("The sum of all integers from 1 to %d is: %d\n", number, sum);

return 0;

}
Task 02:
 User Input Validation Using Do While Loop

Create a program that continuously asks the user to enter a number until they enter a negative
number. Use a do while loop
#include <stdio.h>

intmain() {

int number;

do {

printf("Enter a number (enter a negative number to stop): ");

scanf("%d", &number);

if (number >= 0) {

printf("You entered: %d\n", number);

} while (number >= 0); // Continue looping until a negative number is entered

printf("You entered a negative number. Program terminated.\n");


return 0;

Task 03:
 Printing Even Numbers Using For Loop

Develop a program that prints the first 10 even numbers (0 to 18) using a for loop.
#include <stdio.h>

intmain() {
inti;

// Loop to print first 10 even numbers


printf("The first 10 even numbers are:\n");
for (i = 0; i<= 18; i += 2) {
printf("%d ", i); // Print the current even number
}

printf("\n"); // To move to the next line after printing all numbers


return 0;
}

Assignment 07
1. Find out the sum of series 12 + 22 + …. + n2
2. If a four-digit number is input through the keyboard, write a program to obtain the sum of
the first and last digit of this number.
3. Write a program to find GCD (greatest common divisor or HCF) and LCM (least common
multiple) of two numbers.

1. Write a program to display the following pattern.

#include <stdio.h>

intmain() {

int rows = 4; // Number of rows in the pattern

for (inti = 1; i<= rows; i++) {

for (int j = 1; j <= i; j++) {

printf("*");

printf("\n"); // Move to the next line after each row

return 0;

2. Write a program to check whether a number is Palindrome or not.


#include <stdio.h>
intmain() {

int number, original, reversed = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &number);

original = number;

while (number != 0) {

remainder = number % 10;

reversed = reversed * 10 + remainder;

number /= 10;

if (original == reversed) {

printf("%d is a palindrome.\n", original);

} else {

printf("%d is not a palindrome.\n", original);

return 0;

}
3. Write a program to generate Fibonacci series.
#include <stdio.h>

intmain() {

int n, first = 0, second = 1, next;

printf("Enter the number of terms for the Fibonacci series: ");

scanf("%d", &n);

if (n <= 0) {

printf("Please enter a positive integer.\n");

} else {

printf("Fibonacci series up to %d terms:\n", n);

for (inti = 1; i<= n; i++) {

if (i == 1) {

next = first;

} else if (i == 2) {

next = second;

} else {

next = first + second;


first = second;

second = next;

printf("%d ", next);

printf("\n");

return 0;

4. Write programs to display each of the following patterns


*********
*******
*****
***
*
#include <stdio.h>

intmain() {
int rows = 5; // Number of rows in the pattern

// Outer loop for each row


for (inti = rows; i>= 1; i--) {
// Inner loop to print stars in each row
for (int j = 1; j <= (2 * i - 1); j++) {
printf("* ");
}
printf("\n"); // Move to the next line after each row
}

return 0;
}

You might also like