Day5_Assignment
Day5_Assignment
1.
//WAP to print Fibonacci Series up to a Given Number
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next;
printf("Enter the number up to which the Fibonacci series must be
printed: ");
scanf("%d", &n);
printf("\n");
return 0;
}
2.
//WAP to print factorial of a number
#include <stdio.h>
int main()
{
int num, fact = 1;
printf("Enter the number: ");
scanf("%d", &num);
3.
//WAP to check whether the number is Prime or not
#include<stdio.h>
int main()
{
int num, i = 2, flag = 0;
printf("Enter the number: ");
scanf("%d", &num);
if(num == 1)
{
printf("%d is neither a prime nor a composite.\n", num);
return 0; // Exit the program as 1 is not a prime number
}
else if(flag == 0)
{
printf("%d is a prime number.\n", num);
}
else
{
printf("%d is not a prime number.\n", num);
}
return 0;
}
4.
//WAP to print lower case alphabets
#include <stdio.h>
int main() {
char c = 'a';
return 0;
}
Challenge Questions:
1.
/*WAP to calculate the electricity bill based on the formula mentioned
below
Calculations
To calculate your electricity bill, follow these steps:
Watts = (amps) x (volts)
Kilowatt-hours = (watts) x (usage) / 1000.
Cost = (kilowatt-hours) x (electricity rate)
1. Subtract the current meter reading from the previous month’s reading
to find the energy consumption.
2. Multiply the units consumed by the per-unit charges based on the
applicable slabs (e.g., Rs. 4.22 for 1-100 units,
Rs. 5.02 for 101-200 units).
3. Add the fixed charge and energy duty (e.g., Rs. 40 fixed charge and
Rs. 0.15 per unit) to the energy charges.
4. The sum of the energy charges, fixed charge, and energy duty gives you
the total bill amount.
Example: If you consumed 250 units with the applicable slabs mentioned
above, the energy charges would be Rs. 1218.
Adding the fixed charge and energy duty, the total bill amount would be
Rs. 1296.
Requirements:
inputs : amps, volts, prev_month, this_month
comparison : >=, <=
control statements : if...elseif...else
number of variables : 9
data type of variables : float
scopes of variables : local */
#include <stdio.h>
int main() {
float amps, volts, watts, kilowatt_hours, usage, rate = 0, cost,
prev_month, this_month;
return 0;
}
2.
/*Program to calculate weekly pay using if else statemments.
Requirements:
Ask user to enter the number of hours worked in a week.
Output the gross_pay, tax, and the net_pay.
Assumptions:
Basic pay_rate = $12.00/hr
Overtime (in excess of 40 hours) = time and a half
Tax rate:
15% of the first $300
20% of the next $150
25% of the rest
inputs : hours_worked
outputs : gross_pay, tax, net_pay
comparison : >, <=
control statements : if...elseif...else, if...else
number of variables : 4
data type of variables : float, int
scopes of variables : local
*/
#include <stdio.h>
int main()
{
int hours_worked;
float gross_pay, net_pay, tax;
printf("Enter the number of hours worked in a week: ");
scanf("%d", &hours_worked);
return 0;
}
In-class Assignments:
1.
//program to check for a valid triangle
#include<stdio.h>
int main()
{
int side1, side2, side3;
printf("Enter the lengths of three sides of a triangle: ");
scanf("%d %d %d", &side1, &side2, &side3);
if(side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 >
side1)
printf("The given sides form a valid triangle.\n");
PS C:\Users\betti\Desktop\Training\Day5> ./prgm1
2.
//program to check if a character is alphabet
#include<stdio.h>
#include<stdlib.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("%c is an alphabet.\n", ch);
PS C:\Users\betti\Desktop\Training\Day5> ./prgm2
Enter a character: 2
PS C:\Users\betti\Desktop\Training\Day5> ./prgm2
Enter a character: a
3.
//program to check leap year
#include<stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if((0 == year % 4 && 0 != year % 100) || (0 == year % 400))
printf("%d is a leap year.\n", year);
PS C:\Users\betti\Desktop\Training\Day5> ./prgm3
Program is complete.
PS C:\Users\betti\Desktop\Training\Day5> ./prgm3
Program is complete.
4.
// program to check if a number is divisible by 3
#include<stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(0 == number % 3)
printf("%d is divisible by 3.\n", number);
PS C:\Users\betti\Desktop\Training\Day5> ./prgm4
Enter a number: 54
54 is divisible by 3.
Program is complete.
5.
//program to check for uppercase characters in a string
#include<stdio.h>
int main() {
char str[100];
int i, count = 0;
return 0;
}
Output:
PS C:\Users\betti\Desktop\Training\Day5> ./prgm5_v2
6.
//program to check for special characters
#include<stdio.h>
#include<ctype.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int i;
int specialCharCount = 0;
return 0;
}
Output:
PS C:\Users\betti\Desktop\Training\Day5> ./prgm6
7.
/*program to perform calculation using switch case
+ = addition
- = subtraction
* = multiplication
/ = division
% = modulus*/
#include<stdio.h>
int main()
{
char op;
float num1, num2;
printf("Enter the numbers to perform the calculation: ");
scanf("%f %f", &num1, &num2);
printf("Enter the operator (+, -, *, /, %%): ");
scanf(" %c", &op);
switch(op) {
case '+':
printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2);
break;
case '-':
printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2);
break;
case '*':
printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("%f / %f = %.2f\n", num1, num2, num1 / num2);
} else {
printf("Error: Division by zero!\n");
}
break;
case '%':
printf("%d %% %d = %d\n", (int)num1, (int)num2, (int)num1 %
(int)num2);
break;
default:
printf("Invalid operator!\n");
break;
}
return 0;
}
Output:
PS C:\Users\betti\Desktop\Training\Day5> ./prgm7
8.
//program to reverse a numbere
#include<stdio.h>
int main()
{
int num, rev = 0, rem = 0;
printf("enter the number to be reversed: ");
scanf("%d", &num);
int temp = num;
while (temp != 0) {
rem = temp % 10;
rev = rev * 10 + rem;
temp = temp / 10;
}
PS C:\Users\betti\Desktop\Training\Day5> ./prgm8
9.
//program to determine largest of three numbers
// inputs: num1,num2,num3
// comparison: >=
// control statements: if.....elseif...else
// how many variables used: 3
// data type of variables: int
// preffered scope of variables: local
#include<stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
return 0;
}
Output:
PS C:\Users\betti\Desktop\Training\Day5> ./prgm9
Requirements:
inputs : mark
comparison : >=, <
control statements : if...else
number of variables : 2 (grade, mark)
data type of variables : int, char
scopes of variables : local */
#include<stdio.h>
int main()
{
int mark;
printf("Enter the mark: ");
scanf("%d", &mark);
char grade;
if(mark >= 90)
{
grade = 'A';
}
else if(mark >= 80 && mark < 90)
{
grade = 'B';
}
else if(mark >= 70 && mark < 80)
{
grade = 'C';
}
else if(mark >= 60 && mark < 70)
{
grade = 'D';
}
else if(mark >= 0 && mark < 60)
{
grade = 'F';
}
else
{
printf("Invalid mark. Please enter a valid mark between 0 and
100.\n");
return 1; // terminate the program with an error code
}
PS C:\Users\betti\Desktop\Training\Day5> ./prgm10
The grade is C
11.
// program to count number of digits in a number using while loop
#include<stdio.h>
int main(){
int num, count = 0;
printf("Enter a number: ");
scanf("%d", &num);
int temp = num;
while(temp != 0){
temp = temp / 10;
count++;
}
}
Output:
PS C:\Users\betti\Desktop\Training\Day5> ./prgm11