diff --git a/README.md b/README.md index 2b5538a..d3af9be 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/LBh0h0yx) # Assignment 1: C Programming Basics ## Instructions diff --git a/src/q1.c b/src/q1.c index e9adc59..04a3465 100644 --- a/src/q1.c +++ b/src/q1.c @@ -1,6 +1,15 @@ -/* Write a C program that takes an integer input from the user and determines if it is positive, negative, or zero. -Expected Output: -If n > 0, print Positive (case-insensitive, extra messages allowed). -If n < 0, print Negative (case-insensitive, extra messages allowed). -If n == 0, print Zero (case-insensitive, extra messages allowed). -*/ \ No newline at end of file +#include +int main() { + int n; + printf("Enter an integer: "); + scanf("%d", &n); + + if (n > 0) + printf("The number is Positive.\n"); + else if (n < 0) + printf("The number is Negative.\n"); + else + printf("The number is Zero.\n"); + + return 0; +} diff --git a/src/q10.c b/src/q10.c index 05a3fd8..2d12955 100644 --- a/src/q10.c +++ b/src/q10.c @@ -1,5 +1,18 @@ -/* Write a C program that prints a pattern of asterisks. - * - *** - ***** -*/ \ No newline at end of file +#include + +int main() { + int i, j, k; + int n = 3; + + for(i = 1; i <= n; i++) { + for(j = 1; j <= n - i; j++) { + printf(" "); + } + for(k = 1; k <= 2*i - 1; k++) { + printf("*"); + } + printf("\n"); + } + + return 0; +} diff --git a/src/q11.c b/src/q11.c index 30824f6..ca9816e 100644 --- a/src/q11.c +++ b/src/q11.c @@ -1,5 +1,18 @@ -/* Write a C program that prints a pattern of asterisks. - ***** - *** - * -*/ \ No newline at end of file +#include + +int main() { + int i, j, k; + int n = 3; + + for(i = n; i >= 1; i--) { + for(j = 1; j <= n - i; j++) { + printf(" "); + } + for(k = 1; k <= 2*i - 1; k++) { + printf("*"); + } + printf("\n"); + } + + return 0; +} diff --git a/src/q12.c b/src/q12.c index ac47268..0047a8e 100644 --- a/src/q12.c +++ b/src/q12.c @@ -1 +1,11 @@ -// Write a C program that prints all numbers from 1 to 100 using a for loop. \ No newline at end of file +#include + +int main() { + int i; + + for(i = 1; i <= 100; i++) { + printf("%d\n", i); + } + + return 0; +} diff --git a/src/q13.c b/src/q13.c index 8009d54..a23ab4f 100644 --- a/src/q13.c +++ b/src/q13.c @@ -1 +1,10 @@ -// Write a C program that calculates the sum of all even numbers from 1 to 50 using a while loop. \ No newline at end of file +#include +int main(){ + int i=2,sum=0; + while(i<=50){ + sum+=i; + i+=2; + } + printf("%d\n",sum); + return 0; +} diff --git a/src/q14.c b/src/q14.c index 8f1a9d5..76834ae 100644 --- a/src/q14.c +++ b/src/q14.c @@ -1 +1,16 @@ -// Write a C program that prompts the user for a positive integer and prints all the factors of that number using a for loop. \ No newline at end of file +#include +int main() { +int num, i; + + printf("Enter a positive integer: "); + scanf("%d", &num); + + printf("Factors of %d are:\n", num); + for(i = 1; i <= num; i++) { + if(num % i == 0) { + printf("%d\n", i); + } + } + + return 0; +} diff --git a/src/q15.c b/src/q15.c index f582057..8316680 100644 --- a/src/q15.c +++ b/src/q15.c @@ -1 +1,27 @@ -// Write a C program that prompts the user for a positive integer and checks if it is a prime number using a while loop. \ No newline at end of file +#include + +int main() { + int num, i = 2, isPrime = 1; + + printf("Enter a positive integer: "); + scanf("%d", &num); + + if(num <= 1) { + isPrime = 0; + } + + while(i < num) { + if(num % i == 0) { + isPrime = 0; + break; + } + i++; + } + + if(isPrime) + printf("%d is a prime number.\n", num); + else + printf("%d is not a prime number.\n", num); + + return 0; +} diff --git a/src/q16.c b/src/q16.c index af07832..9e51329 100644 --- a/src/q16.c +++ b/src/q16.c @@ -1 +1,18 @@ -// Write a C program that calculates the factorial of a given number using a do-while loop. \ No newline at end of file +#include +int main(){ + int num,i; + unsigned long long factorial=1; + printf("Enter a positive integer: "); + scanf("%d",&num); + i=1; + if(num<0){ + printf("Factorial is not defined for negative numbers.\n"); + }else{ + do{ + factorial*=i; + i++; + }while(i<=num); + printf("Factorial of %d is %llu\n",num,factorial); + } + return 0; +} diff --git a/src/q17.c b/src/q17.c index 6858adf..4fe43bf 100644 --- a/src/q17.c +++ b/src/q17.c @@ -1 +1,10 @@ -// Write a C program that prompts the user for a positive integer and prints a countdown from that number to 1 using a for loop. \ No newline at end of file +#include +int main(){ + int num,i; + printf("Enter a positive integer: "); + scanf("%d",&num); + for(i=num;i>=1;i--){ + printf("%d\n",i); + } + return 0; +} diff --git a/src/q18.c b/src/q18.c index 4229091..1201b38 100644 --- a/src/q18.c +++ b/src/q18.c @@ -1,2 +1,11 @@ -// Write a C program that prompts the user for a positive integer and prints the -// multiplication table for that number up to 10 using a while loop. \ No newline at end of file +#include +int main(){ + int num,i=1; + printf("Enter a positive integer: "); + scanf("%d",&num); + while(i<=10){ + printf("%d x %d = %d\n",num,i,num*i); + i++; + } + return 0; +} diff --git a/src/q19.c b/src/q19.c index a2b2ae3..0b0b721 100644 --- a/src/q19.c +++ b/src/q19.c @@ -1,5 +1,13 @@ -/* Write a C program that prompts the user for a positive integer and prints a pattern of asterisks (*) in a square shape using nested loops. -Example: User’s Input = 2, then pattern to print will be: - * * - * * -*/ \ No newline at end of file +#include +int main(){ + int n,i,j; + printf("Enter a positive integer: "); + scanf("%d",&n); + for(i=1;i<=n;i++){ + for(j=1;j<=n;j++){ + printf("* "); + } + printf("\n"); + } + return 0; +} diff --git a/src/q2.c b/src/q2.c index 30e6fa0..9b192e0 100644 --- a/src/q2.c +++ b/src/q2.c @@ -1,5 +1,13 @@ -/* Write a C program that prompts the user for their age and determines if they are eligible to vote (consider the legal voting age in your country). -Expected Output: -If age >= 18, print Eligible to vote (case-insensitive, extra messages allowed). -If age < 18, print Not eligible to vote (case-insensitive, extra messages allowed). -*/ \ No newline at end of file +#include +int main() { + int age; + printf("Enter your age: "); + scanf("%d", &age); + + if (age >= 18) + printf("Eligible to vote.\n"); + else + printf("Not eligible to vote.\n"); + + return 0; +} diff --git a/src/q20.c b/src/q20.c index 8d1027a..c909a52 100644 --- a/src/q20.c +++ b/src/q20.c @@ -1 +1,17 @@ -// Write a C program that prompts the user for a number between 1 and 7 and prints the corresponding day of the week using a switch-case statement. \ No newline at end of file +#include +int main(){ + int day; + printf("Enter a number between 1 and 7: "); + scanf("%d",&day); + switch(day){ + case 1: printf("Sunday\n"); break; + case 2: printf("Monday\n"); break; + case 3: printf("Tuesday\n"); break; + case 4: printf("Wednesday\n"); break; + case 5: printf("Thursday\n"); break; + case 6: printf("Friday\n"); break; + case 7: printf("Saturday\n"); break; + default: printf("Invalid input\n"); + } + return 0; +} diff --git a/src/q3.c b/src/q3.c index f74a4ac..39bbca8 100644 --- a/src/q3.c +++ b/src/q3.c @@ -1 +1,12 @@ -// Write a C program that calculates the absolute value of a given number without using the built-in absolute value function. \ No newline at end of file +#include +int main() { + int n; + printf("Enter a number: "); + scanf("%d", &n); + + if (n < 0) + n = -n; + + printf("Absolute value: %d\n", n); + return 0; +} diff --git a/src/q4.c b/src/q4.c index 35772e9..47d06ee 100644 --- a/src/q4.c +++ b/src/q4.c @@ -1 +1,8 @@ -// Write a C program that prints all even numbers between 1 and 100 using a for loop. \ No newline at end of file +#include +int main(){ + int i; + for(i=2;i<=100;i+=2){ + printf("%d ",i); + } + return 0; +} diff --git a/src/q5.c b/src/q5.c index f89ed1d..286532b 100644 --- a/src/q5.c +++ b/src/q5.c @@ -1 +1,12 @@ -// Write a C program that prompts the user for a positive integer and calculates the factorial of that number using a while loop. \ No newline at end of file +#include +int main(){ + int n,i=1; + unsigned long long f=1; + scanf("%d",&n); + while(i<=n){ + f*=i; + i++; + } + printf("%llu\n",f); + return 0; +} diff --git a/src/q6.c b/src/q6.c index 6b94dca..83ddbed 100644 --- a/src/q6.c +++ b/src/q6.c @@ -1 +1,10 @@ -// Write a C program that prompts the user for a number and prints its multiplication table up to 10 using a do-while loop. \ No newline at end of file +#include +int main(){ + int num,i=1; + scanf("%d",&num); + do{ + printf("%d x %d = %d\n",num,i,num*i); + i++; + }while(i<=10); + return 0; +} diff --git a/src/q7.c b/src/q7.c index 3678e6f..d5afe42 100644 --- a/src/q7.c +++ b/src/q7.c @@ -1,11 +1,19 @@ -/* Write a C program that prints a pattern of asterisks. - * - ** - *** - **** - ***** - **** - *** - ** - * -*/ \ No newline at end of file +#include + +int main() { + int i, j; + int n = 5; +for(i = 1; i <= n; i++) { + for(j = 1; j <= i; j++) { + printf("*"); + } + printf("\n"); + } +for(i = n-1; i >= 1; i--) { + for(j = 1; j <= i; j++) { + printf("*"); + } + printf("\n"); + } +return 0; +} diff --git a/src/q8.c b/src/q8.c index d9575f4..27a8031 100644 --- a/src/q8.c +++ b/src/q8.c @@ -1,7 +1,15 @@ -/* Write a C program that prints a pattern of asterisks. - * - ** - *** - **** - ***** -*/ \ No newline at end of file +#include + +int main() { + int i, j; + int n = 5; + + for(i = 1; i <= n; i++) { + for(j = 1; j <= i; j++) { + printf("*"); + } + printf("\n"); + } + + return 0; +} diff --git a/src/q9.c b/src/q9.c index f4f89d0..e9cca37 100644 --- a/src/q9.c +++ b/src/q9.c @@ -1,7 +1,15 @@ -/* Write a C program that prints a pattern of asterisks. - ***** - **** - *** - ** - * -*/ \ No newline at end of file +#include + +int main() { + int i, j; + int n = 5; + + for(i = n; i >= 1; i--) { + for(j = 1; j <= i; j++) { + printf("*"); + } + printf("\n"); + } + + return 0; +}