Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 15 additions & 6 deletions src/q1.c
Original file line number Diff line number Diff line change
@@ -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).
*/
#include <stdio.h>
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;
}
23 changes: 18 additions & 5 deletions src/q10.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
/* Write a C program that prints a pattern of asterisks.
*
***
*****
*/
#include <stdio.h>

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;
}
23 changes: 18 additions & 5 deletions src/q11.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
/* Write a C program that prints a pattern of asterisks.
*****
***
*
*/
#include <stdio.h>

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;
}
12 changes: 11 additions & 1 deletion src/q12.c
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
// Write a C program that prints all numbers from 1 to 100 using a for loop.
#include <stdio.h>

int main() {
int i;

for(i = 1; i <= 100; i++) {
printf("%d\n", i);
}

return 0;
}
11 changes: 10 additions & 1 deletion src/q13.c
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
// Write a C program that calculates the sum of all even numbers from 1 to 50 using a while loop.
#include <stdio.h>
int main(){
int i=2,sum=0;
while(i<=50){
sum+=i;
i+=2;
}
printf("%d\n",sum);
return 0;
}
17 changes: 16 additions & 1 deletion src/q14.c
Original file line number Diff line number Diff line change
@@ -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.
#include <stdio.h>
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;
}
28 changes: 27 additions & 1 deletion src/q15.c
Original file line number Diff line number Diff line change
@@ -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.
#include <stdio.h>

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;
}
19 changes: 18 additions & 1 deletion src/q16.c
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
// Write a C program that calculates the factorial of a given number using a do-while loop.
#include <stdio.h>
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;
}
11 changes: 10 additions & 1 deletion src/q17.c
Original file line number Diff line number Diff line change
@@ -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.
#include <stdio.h>
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;
}
13 changes: 11 additions & 2 deletions src/q18.c
Original file line number Diff line number Diff line change
@@ -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.
#include <stdio.h>
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;
}
18 changes: 13 additions & 5 deletions src/q19.c
Original file line number Diff line number Diff line change
@@ -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:
* *
* *
*/
#include <stdio.h>
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;
}
18 changes: 13 additions & 5 deletions src/q2.c
Original file line number Diff line number Diff line change
@@ -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).
*/
#include <stdio.h>
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;
}
18 changes: 17 additions & 1 deletion src/q20.c
Original file line number Diff line number Diff line change
@@ -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.
#include <stdio.h>
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;
}
13 changes: 12 additions & 1 deletion src/q3.c
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
// Write a C program that calculates the absolute value of a given number without using the built-in absolute value function.
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

if (n < 0)
n = -n;

printf("Absolute value: %d\n", n);
return 0;
}
9 changes: 8 additions & 1 deletion src/q4.c
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
// Write a C program that prints all even numbers between 1 and 100 using a for loop.
#include <stdio.h>
int main(){
int i;
for(i=2;i<=100;i+=2){
printf("%d ",i);
}
return 0;
}
13 changes: 12 additions & 1 deletion src/q5.c
Original file line number Diff line number Diff line change
@@ -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.
#include <stdio.h>
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;
}
11 changes: 10 additions & 1 deletion src/q6.c
Original file line number Diff line number Diff line change
@@ -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.
#include <stdio.h>
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;
}
30 changes: 19 additions & 11 deletions src/q7.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/* Write a C program that prints a pattern of asterisks.
*
**
***
****
*****
****
***
**
*
*/
#include <stdio.h>

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;
}
22 changes: 15 additions & 7 deletions src/q8.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/* Write a C program that prints a pattern of asterisks.
*
**
***
****
*****
*/
#include <stdio.h>

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;
}
22 changes: 15 additions & 7 deletions src/q9.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/* Write a C program that prints a pattern of asterisks.
*****
****
***
**
*
*/
#include <stdio.h>

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;
}
Loading