Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views2 pages

C Beginner Exercises

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

C Beginner Exercises

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C Language Beginner Practice Exercises with Model

Answers

Exercise 1 – Function with String Parameter


Task: Write a function greetUser that takes a user’s name as input and prints:
'Hello, ! Welcome to C programming.'. Call this function in main() with your name.
#include void greetUser(char name[]){ printf("Hello, %s! Welcome to C
programming.\n", name); } int main(){ greetUser("Nani"); return 0; }

Exercise 2 – Function Returning a Value


Task: Write a function multiply that takes two integers as input and returns their
product. In main(), call this function with numbers 7 and 8, then print the result.
#include int multiply(int a, int b){ return a * b; } int main(){ int result =
multiply(7, 8); printf("Product: %d\n", result); return 0; }

Exercise 3 – Days to Years, Months, Weeks, and Days


Task: Ask the user for the number of days. Convert it into years, months, weeks,
and remaining days (assume 1 year = 365 days, 1 month = 30 days, 1 week = 7
days).
#include int main(){ int totalDays, years, months, weeks, days; printf("Enter the
number of days: "); scanf("%d", &totalDays;); years = totalDays / 365; totalDays
%= 365; months = totalDays / 30; totalDays %= 30; weeks = totalDays / 7; days =
totalDays % 7; printf("Years: %d\n", years); printf("Months: %d\n", months);
printf("Weeks: %d\n", weeks); printf("Days: %d\n", days); return 0; }

Exercise 4 – Even or Odd Number


Task: Ask the user to enter an integer. Write a function isEven that returns 1 if the
number is even, otherwise returns 0. Print the result in main().
#include int isEven(int n){ if(n % 2 == 0) return 1; else return 0; } int main(){
int num; printf("Enter a number: "); scanf("%d", #); if(isEven(num)) printf("%d
is even\n", num); else printf("%d is odd\n", num); return 0; }

Exercise 5 – Grade Calculator


Task: Write a program that asks the user for their score (0–100) and prints the
grade using the given grading scale.
#include int main(){ float score; printf("Enter your score: "); scanf("%f",
&score;); if(score >= 90 && score <= 100) printf("Grade: A\n"); else if(score >=
80) printf("Grade: B\n"); else if(score >= 70) printf("Grade: C\n"); else
if(score >= 60) printf("Grade: D\n"); else if(score >= 0) printf("Grade: F\n");
else printf("Invalid score\n"); return 0; }

Exercise 6 – Function Returning the Largest Number


Task: Write a function findMax that takes three integers and returns the largest. In
main(), call it and display the result.
#include int findMax(int a, int b, int c){ int max = a; if(b > max) max = b; if(c
> max) max = c; return max; } int main(){ int num1, num2, num3; printf("Enter
three numbers: "); scanf("%d %d %d", &num1;, &num2;, &num3;); printf("Largest
number is: %d\n", findMax(num1, num2, num3)); return 0; }

Exercise 7 – Hours to Days and Hours


Task: Ask the user to enter total hours worked. Convert this into days and
remaining hours (1 day = 24 hours).
#include int main(){ int totalHours, days, hours; printf("Enter total hours: ");
scanf("%d", &totalHours;); days = totalHours / 24; hours = totalHours % 24;
printf("Days: %d, Hours: %d\n", days, hours); return 0; }

Exercise 8 – Positive, Negative, or Zero


Task: Write a function checkNumber that prints whether an integer is positive,
negative, or zero.
#include void checkNumber(int n){ if(n > 0) printf("%d is positive\n", n); else
if(n < 0) printf("%d is negative\n", n); else printf("The number is zero\n"); }
int main(){ int num; printf("Enter a number: "); scanf("%d", #);
checkNumber(num); return 0; }

Exercise 9 – Temperature Category


Task: Ask the user to enter a temperature in Celsius and print if it is Hot, Warm, or
Cold.
#include int main(){ float temp; printf("Enter temperature in Celsius: ");
scanf("%f", &temp;); if(temp >= 30) printf("Hot\n"); else if(temp >= 15)
printf("Warm\n"); else printf("Cold\n"); return 0; }

Exercise 10 – Sum of Numbers Using Loop and Return


Task: Write a function sumN that takes an integer n and returns the sum of
numbers from 1 to n.
#include int sumN(int n){ int sum = 0; for(int i = 1; i <= n; i++){ sum += i; }
return sum; } int main(){ int num; printf("Enter a positive number: ");
scanf("%d", #); printf("Sum from 1 to %d is %d\n", num, sumN(num)); return 0; }

You might also like