0% found this document useful (0 votes)
11 views9 pages

Beginner C Programming Practical Tasks

some test solutions

Uploaded by

aayandele465
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)
11 views9 pages

Beginner C Programming Practical Tasks

some test solutions

Uploaded by

aayandele465
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

Great!

Since this is a practical test in beginner C programming, you're most likely


going to be tested on foundational skills. Below are essential C programming topics
along with sample problems and code snippets that commonly appear in beginner-
level practical tests:

🔹 1. Printing Output

Task: Print your name, department, and registration number using printf().

#include <stdio.h>

int main() {
printf("Name: Adedoyin Ayandele\n");
printf("Department: Computer Science\n");
printf("Reg. Number: CSC2025XYZ\n");
return 0;
}

🔹 2. Basic Arithmetic Calculator

Task: Take two numbers and perform addition, subtraction, multiplication, and
division.

#include <stdio.h>

int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);

printf("Sum = %d\n", a + b);


printf("Difference = %d\n", a - b);
printf("Product = %d\n", a * b);

if (b != 0)
printf("Quotient = %.2f\n", (float)a / b);
else
printf("Cannot divide by zero\n");

return 0;
}

🔹 3. Even or Odd

Task: Take a number as input and check if it is even or odd.

#include <stdio.h>

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

if (num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);

return 0;
}

🔹 4. Simple Loop (1 to n)

Task: Print numbers from 1 to n using a loop.

#include <stdio.h>

int main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);

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


printf("%d\n", i);
}

return 0;
}

🔹 5. Sum of Digits

Task: Input a number and find the sum of its digits.

#include <stdio.h>

int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);

while (num != 0) {
sum += num % 10;
num /= 10;
}

printf("Sum of digits = %d\n", sum);


return 0;
}

🔹 6. Swap Two Numbers

Task: Swap the values of two variables using a temporary variable.

#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
printf("Before swapping: a = %d, b = %d\n", a, b);

temp = a;
a = b;
b = temp;

printf("After swapping: a = %d, b = %d\n", a, b);


return 0;
}

🔹 7. Simple write() System Call (bonus if they ask for


low-level)
#include <unistd.h>

int main() {
const char *msg = "Hello from write()!\n";
write(1, msg, 20); // 1 is STDOUT
return 0;
}

🔹 8. Simple if-else Statement

Task: Input a number and check if it's positive, negative or zero.

#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num > 0)
printf("Positive\n");
else if (num < 0)
printf("Negative\n");
else
printf("Zero\n");

return 0;
}

🔹 9. Find the Largest of Three Numbers


#include <stdio.h>

int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

if (a >= b && a >= c)


printf("%d is the largest.\n", a);
else if (b >= a && b >= c)
printf("%d is the largest.\n", b);
else
printf("%d is the largest.\n", c);

return 0;
}

🔹 10. Factorial of a Number


#include <stdio.h>

int main() {
int n, i;
unsigned long long factorial = 1;

printf("Enter an integer: ");


scanf("%d", &n);

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


factorial *= i;
}

printf("Factorial = %llu\n", factorial);


return 0;
}

🔸 Tips for the Practical:


Always start with #include <stdio.h> (and unistd.h if using write()).


Use gcc yourfile.c -o yourfile to compile.


Run with ./yourfile.


Read instructions carefully — especially for scanf() vs hardcoding.



If you’re asked to submit a screenshot, make sure the terminal is clear and
visible.

Would you like me to create a cheat sheet PDF or one combined code file for you to
test and study with?

Absolutely — I'm glad to help! Here are more beginner-level C programming


practical questions along with sample solutions. These are often asked in early
programming tests or lab sessions.

🔹 11. Check for Prime Number

Task: Determine if a number is prime.

#include <stdio.h>

int main() {
int num, i, isPrime = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);

if (num <= 1) isPrime = 0;


for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}

if (isPrime)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

return 0;
}

🔹 12. Reverse a Number


#include <stdio.h>

int main() {
int num, reversed = 0;
printf("Enter an integer: ");
scanf("%d", &num);

while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
printf("Reversed number = %d\n", reversed);
return 0;
}

🔹 13. Simple Menu-Driven Calculator


#include <stdio.h>

int main() {
int a, b, choice;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

printf("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n");


printf("Choose an operation: ");
scanf("%d", &choice);

switch (choice) {
case 1: printf("Result = %d\n", a + b); break;
case 2: printf("Result = %d\n", a - b); break;
case 3: printf("Result = %d\n", a * b); break;
case 4:
if (b != 0)
printf("Result = %.2f\n", (float)a / b);
else
printf("Cannot divide by zero\n");
break;
default: printf("Invalid choice\n");
}

return 0;
}

🔹 14. Check for Palindrome Number


#include <stdio.h>

int main() {
int num, original, reversed = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
original = num;

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

if (original == reversed)
printf("Palindrome number.\n");
else
printf("Not a palindrome.\n");

return 0;
}
🔹 15. Fibonacci Series (First n Terms)
#include <stdio.h>

int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);

printf("Fibonacci Series: ");


for (int i = 1; i <= n; i++) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}

return 0;
}

🔹 16. Count Vowels in a String


#include <stdio.h>
#include <string.h>

int main() {
char str[100];
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

for (int i = 0; str[i]; i++) {


char c = str[i];
if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||
c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
count++;
}

printf("Number of vowels: %d\n", count);


return 0;
}

🔹 17. Multiplication Table of a Number


#include <stdio.h>

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

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


printf("%d x %d = %d\n", n, i, n * i);
}

return 0;
}

🔹 18. Check Leap Year


#include <stdio.h>

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

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)


printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);

return 0;
}

🔹 19. Grade Calculator


#include <stdio.h>

int main() {
int mark;
printf("Enter your score (0–100): ");
scanf("%d", &mark);

if (mark >= 70)


printf("Grade: A\n");
else if (mark >= 60)
printf("Grade: B\n");
else if (mark >= 50)
printf("Grade: C\n");
else if (mark >= 45)
printf("Grade: D\n");
else
printf("Grade: F\n");

return 0;
}

Would you like me to:

1.

Add all these new codes to the PDF cheat sheet and .c practice file?

2.
3.

Create a quiz or challenge to help you prepare faster?


4.

Let me know how you'd like to proceed!

You might also like