Skip to content

Commit c2bcb67

Browse files
authored
Merge pull request #15 from Unishkhadka/master
Added miscellaneous folder and electricity bill generator
2 parents 3c860d4 + f98b1ab commit c2bcb67

File tree

8 files changed

+226
-1
lines changed

8 files changed

+226
-1
lines changed

Basic Questions/10_prime_numbers.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
5+
int n, i, flag = 0;
6+
printf("Enter a positive integer: ");
7+
scanf("%d", &n);
8+
9+
// 0 and 1 are not prime numbers
10+
// change flag to 1 for non-prime number
11+
if (n == 0 || n == 1)
12+
flag = 1;
13+
14+
for (i = 2; i <= n / 2; ++i) {
15+
16+
// if n is divisible by i, then n is not prime
17+
// change flag to 1 for non-prime number
18+
if (n % i == 0) {
19+
flag = 1;
20+
break;
21+
}
22+
}
23+
24+
// flag is 0 for prime numbers
25+
if (flag == 0)
26+
printf("%d is a prime number.", n);
27+
else
28+
printf("%d is not a prime number.", n);
29+
30+
return 0;
31+
}

Basic Questions/readme.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,65 @@ Finally, the program prints the output, which is the value of p.
412412

413413
![output_of_basic_product_of_n_numbers](../outputs/basic_product_of_n_numbers.png)
414414

415+
## 10. WAP to check whether the given number is prime or composite.
416+
417+
### Program
418+
419+
```c
420+
//WAP to check whether the given number is prime or composite.
421+
422+
#include <stdio.h>
423+
424+
int main() {
425+
426+
int n, i, flag = 0;
427+
printf("Enter a positive integer: ");
428+
scanf("%d", &n);
429+
430+
// 0 and 1 are not prime numbers
431+
// change flag to 1 for non-prime number
432+
if (n == 0 || n == 1)
433+
flag = 1;
434+
435+
for (i = 2; i <= n / 2; ++i) {
436+
437+
// if n is divisible by i, then n is not prime
438+
// change flag to 1 for non-prime number
439+
if (n % i == 0) {
440+
flag = 1;
441+
break;
442+
}
443+
}
444+
445+
// flag is 0 for prime numbers
446+
if (flag == 0)
447+
printf("%d is a prime number.", n);
448+
else
449+
printf("%d is not a prime number.", n);
450+
451+
return 0;
452+
}
453+
```
454+
This C program checks whether the given number is prime or composite.
455+
456+
This program takes an integer as input from the user.
457+
If the number is 0 or 1 then the flag is assigned 1.
458+
Then i is initialized as 2 and then i assigned as the numbers in range (2,n/2).
459+
If the input goes through a loop where if the integer is divisible by 'i' then
460+
the flag is assigned 1 else the flag is assigned to 0. Finally the program checks
461+
the value of flag. I the flag is 1 the it prints the integer is prime while it
462+
prints the integer is not prime if the flag is 0.
463+
464+
For example, the input is seven then this program prints "7 is a prime number".
465+
466+
_output_
467+
468+
![output of basic questions q1](../outputs/basic_prime.png)
469+
415470
<!-- Add new question above this comment -->
416471

472+
473+
417474
## License
418475

419476
Released under [MIT](/LICENSE) by [@ComputeNepal](https://github.com/ComputeNepal).

Miscellaneous/1_Electricity_bill.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//A simple program of electricity bill generator
2+
3+
#include<stdio.h>
4+
#include <stdlib.h>
5+
6+
int main(){
7+
int units;
8+
float price;
9+
10+
printf("Enter the units:\t");
11+
scanf("%d",&units);
12+
system("clear");
13+
14+
if(units<=20){
15+
price=units*4;
16+
}
17+
else if(units<=50){
18+
price=(20*4)+((units-20)*7.3);
19+
}
20+
21+
else if(units<=150){
22+
price=((20*4)+(50*7.3))+((units-50)*8.6);
23+
}
24+
else if(units<=250){
25+
price=(20*4+30*7.3+100*8.6)+((units-150)*9.5);
26+
}
27+
else if(units>250){
28+
price=(20*4+30*7.3+100*8.6+100*9.5)+((units-250)*12.5);
29+
}
30+
31+
//you can automate the bill no, date and sc no for advancement
32+
printf("~~~NEPAL ELECTRICITY AUTHORITY~~~\n");
33+
printf("-----------------------------------\n");
34+
printf("\tELECTRICITY BILL\n");
35+
printf("-----------------------------------\n\n");
36+
printf("BILL NO:\t843948394\n");
37+
printf("BILL DATE:\t2079/12/12\n");
38+
printf("BRANCH:\t\tDAMAK\n");
39+
printf("-----------------------------------\n");
40+
printf("SC NO:\t\t309.11.012A\n");
41+
printf("BILL NO:\t843948394\n");
42+
printf("TOTAL UNITS:\t%d\n",units);
43+
printf("TOTAL AMOUNT:\tRS.%.3f\n",price);
44+
printf("-----------------------------------\n");
45+
printf("-----------------------------------\n");
46+
return 0;
47+
}

Miscellaneous/readme.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Miscellaneous
2+
3+
[![ComputeNepal - learn-c-programming](https://img.shields.io/static/v1?label=ComputeNepal&message=learn-c-programming&color=blue&logo=github)](https://github.com/ComputeNepal/learn-c-programming "Go to GitHub repo")
4+
[![stars - learn-c-programming](https://img.shields.io/github/stars/ComputeNepal/learn-c-programming?style=social)](https://github.com/ComputeNepal/learn-c-programming)
5+
[![forks - learn-c-programming](https://img.shields.io/github/forks/ComputeNepal/learn-c-programming?style=social)](https://github.com/ComputeNepal/learn-c-programming)
6+
7+
[![License](https://img.shields.io/badge/License-MIT-blue)](#license)
8+
[![issues - learn-c-programming](https://img.shields.io/github/issues/ComputeNepal/learn-c-programming)](https://github.com/ComputeNepal/learn-c-programming/issues)
9+
10+
[![contributions - welcome](https://img.shields.io/badge/contributions-welcome-blue)](/CONTRIBUTING.md "Go to contributions doc")
11+
12+
![Learn C Programming](https://repository-images.githubusercontent.com/615587446/9a0d7982-bdb2-4918-8570-ebfff27778ad)
13+
14+
## 1. WAP to build a simple electricity bill generator.
15+
16+
### Program
17+
```c
18+
//A simple program of electricity bill generator
19+
20+
#include<stdio.h>
21+
#include <stdlib.h>
22+
23+
int main(){
24+
int units;
25+
float price;
26+
27+
printf("Enter the units:\t");
28+
scanf("%d",&units);
29+
system("clear");
30+
31+
if(units<=20){
32+
price=units*4;
33+
}
34+
else if(units<=50){
35+
price=(20*4)+((units-20)*7.3);
36+
}
37+
38+
else if(units<=150){
39+
price=((20*4)+(50*7.3))+((units-50)*8.6);
40+
}
41+
else if(units<=250){
42+
price=(20*4+30*7.3+100*8.6)+((units-150)*9.5);
43+
}
44+
else if(units>250){
45+
price=(20*4+30*7.3+100*8.6+100*9.5)+((units-250)*12.5);
46+
}
47+
48+
//you can automate the bill no, date and sc no for advancement
49+
printf("~~~NEPAL ELECTRICITY AUTHORITY~~~\n");
50+
printf("-----------------------------------\n");
51+
printf("\tELECTRICITY BILL\n");
52+
printf("-----------------------------------\n\n");
53+
printf("BILL NO:\t843948394\n");
54+
printf("BILL DATE:\t2079/12/12\n");
55+
printf("BRANCH:\t\tDAMAK\n");
56+
printf("-----------------------------------\n");
57+
printf("SC NO:\t\t309.11.012A\n");
58+
printf("BILL NO:\t843948394\n");
59+
printf("TOTAL UNITS:\t%d\n",units);
60+
printf("TOTAL AMOUNT:\tRS.%.3f\n",price);
61+
printf("-----------------------------------\n");
62+
printf("-----------------------------------\n");
63+
return 0;
64+
}
65+
```
66+
67+
This C program allows a user to input total electricity units and generate
68+
the bill in proper format.
69+
70+
The basic logic of this C program is to take an integer 'units' as input
71+
from the user and the if else ladder checks suitable case for the units.
72+
If the unit is less than or equals to 20 units, then the rate is Rs.4 per
73+
unit which is assigned to 'price'. But if the units exceeds 20 and less
74+
than or equals to 50 then the price of 20 units is added to the price of
75+
remaining units. Same case continues untill 250 units. If the unit is more
76+
than 250 then the total sum price of 250 units is added to price of remaining
77+
units. After 250 units the rate is Rs.12.5 per unit even if the unit is way
78+
heigher.
79+
80+
Finally the bill is generated through a self written format which includes
81+
Bill no, Bill date, Total units, Total amount,etc.
82+
83+
For example, if the user input is 40 then the bill is generated for 40 units.
84+
85+
_output_
86+
87+
![output of Miscellaneous-Electricity bill generator](../outputs/Miscellaneous_Electricity_bill_output1.png)
88+
89+
![output of Miscellaneous-Electricity bill generator](../outputs/Miscellaneous_Electricity_bill_output2.png)
90+
4.32 KB
Loading
50.7 KB
Loading

outputs/basic_prime.png

5.23 KB
Loading

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ repository are welcome.
3131
| Pointer Questions | [View Questions](./Pointer%20Questions) |
3232
| Structure Questions | [View Questions](./Structure%20Questions) |
3333
| File Handling Questions | [View Questions](./File%20Handling%20Questions) |
34-
34+
| Miscellaneous | [View Questions](./Miscellaneous/)
3535
<!-- Add new category to the table -->
3636

3737
## Contributors

0 commit comments

Comments
 (0)