0% found this document useful (0 votes)
2 views

C programming practice

The document contains a series of C programming tasks that include calculating areas, converting data types, checking balances, computing taxes, and validating user input based on various conditions. Each task is accompanied by a code snippet that demonstrates the implementation of the described functionality. The tasks cover a wide range of programming concepts, including loops, conditionals, and basic arithmetic operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C programming practice

The document contains a series of C programming tasks that include calculating areas, converting data types, checking balances, computing taxes, and validating user input based on various conditions. Each task is accompanied by a code snippet that demonstrates the implementation of the described functionality. The tasks cover a wide range of programming concepts, including loops, conditionals, and basic arithmetic operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

1. Write a program to calculate the area of a circle.

#include <stdio.h>
#define PI 22/7.0

int main() {
double r,a;
printf("Enter the radius: ");
scanf("%lf",&r);
a = PI * r * r;
printf("The area is:%.12lf ",a);
return 0;
}

2. Write a program to convert an integer into the corresponding


floating-point number.
#include <stdio.h>

int main() {
int a;
float b;
printf("Enter a number: ");
scanf("%d",&a);
b = (float)a;
printf("The floating number is: %lf",b);
return 0;
}

3. Write a Program to check if withdraw amount is more than balance


or not, if withdraw amount is more than balance, then notify user for
insufficient balance.

#include <stdio.h>

int main() {
int balance = 0,withdraw = 0;
printf("Enter the balance: ");
scanf("%d",&balance);
printf("Enter amount to be withdrawn: ");
scanf("%d",&withdraw);
if (withdraw>balance)
{
printf("Insufficient Balance");
}
else
{
balance=balance-withdraw ;
printf("The balance is %d",balance);
}

return 0;
}

4. Write a Program to compute income tax based on income and print


net income tax to be paid based on income.

#include <stdio.h>

int main() {
float income,tax;
printf("Enter your income: ");
scanf("%f",&income);

if ((income>300000)&&(income<=700000))
{
tax = 0.05*income ;
printf("The tax to be paid is %.2f ",tax);
}

else if ((income>700000)&&(income<=1000000))
{
tax = 20000 + (0.1*income);
printf("The tax to be paid is %.2f ",tax);
}

else if ((income>1000000)&&(income<=1200000))
{
tax = 50000 + (0.15*income);
printf("The tax to be paid is %.2f ",tax);
}

else if ((income>1200000)&&(income<=1500000))
{
tax = 80000 + (0.2*income);
printf("The tax to be paid is %.2f ",tax);
}
else if (income>1500000)
{
tax = 140000 + (0.3*income);
printf("The tax to be paid is %.2f ",tax);
}

else
{
printf("No Tax");
}

return 0;
}

5. Write a Program to check he/she is passed or failed based on input


marks.
#include <stdio.h>

int main() {
int marks;
printf ("Enter your marks (out of 50): ");
scanf ("%d",&marks);
if (marks<=50)
{
if (marks<18)
{
printf("You failed.");
}
else
{
printf("You passed.");
}
}
else
{
printf("Invalid input.");
}
return 0;
}
6. Write a Program to check whether he/she is eligible to retire or not.
#include <stdio.h>
int main() {
int age,balance;
printf("Enter your age: ");
scanf("%d",&age);
if (age<18)
{
printf("You are not eligible to work.");
}
if (age>=60)
{
printf("Congratulations! You can retire.");
}
if ((age>=18)&&(age<60))
{
balance = 60-age;
printf("Unfortunately you can't retire now. You have to work for
another %d years. My condolenses are with you.",balance);
}
return 0;
}

7. Write a Program to check the age of the user, if age is above 17


print “You can vote” other print “You can’t vote”.
#include <stdio.h>

int main() {
int age;
printf("Enter your age: ");
scanf("%d",&age);
if (age>=18)
{
printf("You can vote.");
}
if (age<18)
{
printf("You can't vote");
}

return 0;
}
8. Write a Program to check age of the user, if user age is above 17
and below 71, then print “You can drive”, otherwise print “You can’t
drive”.
#include <stdio.h>

int main() {
int age;
printf("Enter your age: ");
scanf("%d",&age);
if ((age>17) && (age<71))
{
printf("You can drive.");
}
else
{
printf("You can't drive.");
}

return 0;
}

9. Write a program to check input character is uppercase character or


lowercase character.
#include <stdio.h>

int main() {
char ch;
printf("Enter your character: ");
scanf("%c",&ch);
if ((ch>=65) && (ch<=90))
{
printf("%c is Uppercase.",ch);
}
if ((ch>=97) && (ch<=122))
{
printf("%c is Lowercase.",ch);
}

return 0;
}
10. Write a program to check percentage of marks scored, if
percentage marks are less than 40 or Physics, chemistry and math
marks are less than 33, then print failed, otherwise print passed.
#include <stdio.h>

int main() {
int total,phy,chem,math;
printf ("Enter Physics marks: ");
scanf ("%d",&phy);
if ((phy>=100) || (phy<=0))
{
printf("Invalid input.");
return 0;
}
printf ("Enter your Chemistry marks: ");
scanf ("%d",&chem);
if ((chem>=100) || (chem<=0))
{
printf("Invalid input.");
return 0;
}
printf ("Enter Maths marks: ");
scanf ("%d",&math);
if ((math>=100) || (math<=0))
{
printf("Invalid input.");
return 0;
}
total=phy+chem+math;
if ((total>=100) || (total<=0))
{
printf("Invalid input.");
return 0;
}

if ((total<=300)&&(phy<=100)&&(chem<=100)&(math<=100))
{
if (total<40)
{
printf("Failed.");
}
else if (phy<33)
{
printf("Failed.");
}
else if (chem<33)
{
printf("Failed.");
}
else if (math<33)
{
printf("Failed.\n");
}
else
{
printf("Passed.");
}
printf ("Your total marks are %d",total);
}
else
{
printf("Invalid input. ");
}

return 0;
}
11. Write a program to check if the input number is positive or
not, if positive then check input is odd or even number.
#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d",&num);
if (num>=0)
{
printf("The number is positive. ");
if (num%2==0)
{
printf("The number is Even. ");
}
else
{
printf("The number is Odd. ");
}
}
else
{
printf("The number is negetive. ");
}

return 0;
}

12. Write a program to find the biggest among three numbers.


#include <stdio.h>

int main() {
int a,b,c;
printf("Enter any 3 numbers: ");
scanf("%d%d%d",&a,&b,&c);
if (a>b)
{
if (a>c)
{
printf("%d is the greatest.",a);
}
else
{
printf("%d is the greatest.",c);
}
}
else
{
if (b>c)
{
printf("%d is the greatest.",b);
}
else
{
printf("%d is the greatest.",c);
}
}

return 0;
}

13. Write a program to check based on the age of user, whether


user is minor or senior citizen or eligible worker.
#include <stdio.h>

int main() {
int age;
printf("Enter your age: ");
scanf("%d",&age);
if (age > 0)
{
if (age<18)
{
printf("You are a Minor.");
}
if ((age>=18) && (age<60))
{
printf("You are an Eligible Worker.");
}
if (age>=60)
{
printf("You are a Senior citizen.");
}
}
else
{
printf("You dont exist.");
}
return 0;
}

14. Write a program to check given three numbers are equal or


not.
#include <stdio.h>

int main() {
int a,b,c;
printf("Enter 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
if (a==b)
{
if (a==c)
{
printf("Equal");
}
else
{
printf("Not equal");
}
}
else
{
printf("Not equal");
}

return 0;
}
15. Write a program to print the grade of the user based on the
input marks.
#include <stdio.h>

int main() {
int marks;
printf("Enter your marks: ");
scanf("%d",&marks);
if (marks<=100 && marks >=0)
{
if (marks>=95)
{
printf("You got O grade. ");
}
else if (marks>=85 && marks<=95)
{
printf("You got A grade.");
}
else if (marks>=65 && marks<=84)
{
printf("You got B grade.");
}
else if (marks>=55 && marks<=64)
{
printf("You got C grade.");
}
else if (marks>=50 && marks<=54)
{
printf("You got D grade.");
}
else (marks<50)
{
printf("You got F grade.");
}
}
else
{
printf("Invalid Input. ");
}

return 0;
}

16. Write a program to compute the Electricity bill based on total


units consumed.
#include <stdio.h>

int main() {
float unit,rs;
printf("Enter number of units: ");
scanf("%f",&unit);
if (unit>=0 && unit<=50)
{
rs=unit*4.1139;
printf("The Bill is Rs. %f",rs);
}
else if (unit>=51 && unit<=100)
{
rs=unit*5.5639;
printf("The Bill is Rs. %f",rs);
}
else if (unit>=101 && unit<=200)
{
rs=unit*7.1139;
printf("The Bill is Rs. %f",rs);
}
else if (unit>=201)
{
rs=unit*8.1639;
printf("The Bill is Rs. %f",rs);
}

return 0;
}
17. Write a program to compare two numbers n1 and n2 and find
n1 equal to n2 or n1 is less than n2 or n1 more than n2.
18. Write a program to perform arithmetic operation based on the
user choice.
19. Write a program to display marks range to be secured to
obtain input grade.
20. Write a program to display Rank range based on college
choice. [note that you must assume hypothetical college names and
rank range.]
#include<stdio.h>
int main()
{
int choice;
printf(" 1. College 1 \n 2. College 2 \n 3. College 3 \n 4. College 4 \
n 5. College 5 \n ");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
{
printf("Rank should be <1000.");
break;
}
case 2 :
{
printf("Rank should be between 1000-9999.");
break;
}
case 3 :
{
printf("Rank should be between 10000-19999.");
break;
}
case 4 :
{
printf("Rank should be between 20000-39999 .");
break;
}
case 5 :
{
printf("Rank should be between 30000-49999.");
break;
}
default :
{
printf("Take a drop year.");
}
}

21. Write a program to display Rank range program wise based on


college choice. [note that you must assume hypothetical college
names and rank range.]
22. Write a program to print sum and average of the numbers
entered by user.
#include<stdio.h>
int main()
{
int num,n,sum=0;
float avg;
printf("Total number of numbers: ");
scanf("%d",&num);
for (int i=1;i<=num;i=i+1)
{
printf("Enter your number: ");
scanf("%d",&n);
sum = sum+n;
}
printf("The sum is %d \n",sum);
avg = sum/num;
printf("The average is %lf",avg);
}

23. Write a program to display the biggest among the 5 numbers.


#include<stdio.h>
int main()
{
int num;
int big=0;
for (int i=1;i<=5;i=i+1)
{
printf("Enter a number: ");
scanf("%d",&num);
if (num>=big)
{
big=num;
}

}
printf("The biggest number is %d",big);
}

24. Write a program to calculate sum of numbers from m to n.


25. Write a program to read the numbers until -1 is encountered.
Also count the number of negatives, positives and zeros entered by
user.
#include<stdio.h>
int main()
{
int pos=0;
int neg=0;
int zero=0;
int num=0;
for (int i;;i=i+1)
{
printf("Enter a number: ");
scanf("%d",&num);
if (num>0)
{
pos=pos+1;
}
else if (num<0)
{
neg=neg+1;
}
else
{
zero=zero+1;
}
if (num==-1)
{
break;
}
}
printf ("There are %d positives, %d negetives, %d
zeros.",pos,neg,zero);
}

26. Write a program to list all leap years from 1900 to 2100.
#include <stdio.h>

int main()
{
for (int i=1900; i<=2100; i=i+1)
{
if (i%100 == 0 )
{
if (i%400 == 0)
{
printf("%d \n",i);
}
}
else if (i%4 == 0)
{
printf("%d \n",i);
}
}
return 0;
}

27. Write a program to display square and cube of first n natural


numbers.
28. Write a program to read a character until a * is encountered.
Also count number of uppercase, lowercase and numbers entered.
#include<stdio.h>
int main()
{
char str,uc,lc,nums;
for (int i;;i=i+1)
{
printf("Enter a character: ");
scanf("%c",&str);
if ((str>=65) && (str<=90))
{
uc=uc+1
}
else if ((str>=97) && (str<=122))
{
lc=lc+1
}
else if (str)
{
zero=zero+1;
}
else if (str==*)
{
break;
}
}
printf ("There are %d upper cases, %d lower cases, %d
numbers.",uc,lc,nums);
}

29. Write a program to read numbers until -1 is encountered. Also


sum and mean of all the positive numbers and negative numbers.
#include <stdio.h>

int main() {
int num, pos=0, neg=0, zero=0;
while (num != -1)
{
printf("Enter your number: ");
scanf("%d",&num);
if (num>0)
{
pos=pos+1;
}
else if (num<0)
{
neg=neg+1;
}
else
{
zero=zero+1;
}
}
printf("The number of positives are:%d \n",pos);
printf("The number of negetives are:%d \n",neg);
printf("The number of zeros are:%d \n",zero);

return 0;
}

30. Write a program to calculate the average of first n numbers.


#include <stdio.h>

int main() {
int n=0,i=0,sum=0,avg=0;
printf("Enter a number: ");
scanf("%d",&n);
while (i<=n)
{
sum=sum+i;
i=i+1;
}
avg=sum/n;
printf("The average is %d",avg);
return 0;
}

31. Write a program to print the following pattern.


Pass 1-12345
Pass 2-12345
Pass 3-12345
Pass 4-12345
Pass 5-12345
32. Write a program to print the following pattern.

Enter the size to print the pattern: 7

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

#include <stdio.h>

int main() {
int n;

printf("Enter the size to print the pattern: ");

scanf("%d",&n);

for (int i=0; i<n; i=i+1)

for (int j=0; j<n; j=j+1)

printf(" * ");

printf("\n");

return 0;

33. Write a program to print the following pattern.


1
12
123
1234
12345
#include <stdio.h>

int main() {
int n;
printf("Enter the size to print: ");
scanf("%d",&n);
for (int i=1; i<=n; i=i+1)
{
for (int j=1; j<=i; j=j+1)
{
printf(" %d ",j);
}
printf("\n");
}
return 0;
}
34. Write a program to print the following pattern.
5
54
543
5432
54321

#include <stdio.h>

int main() {
int n;
printf("Enter the size to print: ");
scanf("%d",&n);
for (int i=n; i>0; i=i-1)
{
for (int j=n; j>=i; j=j-1)
{
printf(" %d ",j);
}
printf("\n");
}
return 0;
}

35. Write a program to print the following pattern.

#include <stdio.h>

int main() {
int n;
printf("Enter the size of the pattern: ");
scanf("%d",&n);
for (int i=0; i<=n; i=i+1)
{
for (int j=0;j<=n; j=j+1)
{
if (i+j<=n)
{
printf(" ");
}
else
{
printf("*");
}
}
printf("\n");
}

}
36. Write a program to print an equilateral triangle.
#include <stdio.h>

int main() {
int n;
printf("Enter the size of the pattern: ");
scanf("%d",&n);
for (int i=0; i<=n; i=i+1)
{
for (int j=0; j<(i*2-1); j=j+1)
{
if ((i+j)<n)
{
printf("#");
}
else
{
printf("");
}
}
printf("\n");
}

37. Write a program to print the following Pascal Triangle.

1
121

12321

1234321

123454321

38. Program to read and display n numbers using an array


39. Program to print the position of the smallest number in an
array of n numbers.
#include <stdio.h>

int main() {
int n,small,pos;
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements of the array: ");
for (int i=0;i<n;i=i+1)
{
scanf("%d",&arr[i]);
}
printf("The elements of the array are: ");
for (int j=0; j<n; j=j+1)
{
printf(" %d \n",arr[j]);
}
small=arr[0];
for (int k=0; k<n; k=k+1)
{
if (arr[k]<small)
{
small=arr[k];
}
}
printf("The smallest number of the array is: %d \n",small);

return 0;
}

40. Program to find whether an array of integers contains a


duplicate number.
#include <stdio.h>
int main() {
int n,org,dup;
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements of the array: ");
for (int i=0; i<n; i=i+1)
{
scanf("%d",&arr[i]);
}
printf("The elements of the array are: ");
for (int j=0; j<n; j=j+1)
{
printf(" %d ",arr[j]);
}
printf("\n");
for (int a=0; a<n; a=a+1)
{
for (int b=a+1; b<n; b=b+1)
{
if (arr[a]==arr[b])
{
dup=arr[b];
printf("The duplicate elements are: %d \n",dup);
}
else
{
printf("There are no duplicate elements.");
}
}

return 0;
}
41. Write a program to display a string using printf().
42. Write a program to convert characters of a string into lower
case.
#include <stdio.h>
#include <conio.h>

int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d",&n);
char string[n];
printf("Enter the string: ");
gets(string);
for (int i=0; i<n; i=i+1)
{
if (string[i]>=65 && string[i]<=90)
{
string[i]=string[i]+32;
}
else
{
string[i]=string[i];
}
}
printf("The new string is :",string);
return 0;
}

43. Write a program to convert characters of a string to upper


case.
44. Write a program to append a string to another string.
45. Write a program to compare two strings.
46. Write a program to reverse the given string.
47. Write a program to extract the first N characters of a string.
48. Write a program to calculate the length of string without
builtin functions.
49. Write a program to print the following pattern.
50. Write a program to print the following pattern.

51. Write a program to add two integers using functions.


52. Write a program to find the biggest of three integers using
functions.
53. Write a program to calculate the area of a circle using
function.
54. Write a program, using functions, to find whether a number is
even or odd.
55. Write a program to convert time to minutes.
56. Write a program to calculate P(n/r).
57. Write a C program that implements complex Numbers
58. Write a C program that implements fractions
59. Modify the last program in section 7 to include the address of
a student as a separate structure. The address should include the
following:
o Address as an array of 30 characters
o City as an array of 20 characters
o Zip code as an integer.
60. Program to illustrate the concept of passing structure to
function.
61. Program to call a function that returns a variable of structure.
62. Program to display the given array of elements.
63. Program to accept string and display vowels
64. Program to call function inside a function.
65. Program to find sum of n below numbers using recursion.
66. Program to find factorial of given number using recursion.
67. Program to find the GCD of two given integers using Recursion
68. Program to generate the Fibonacci series using Recursion.
69. Finding Prime Numbers
70. Write a Program to allocate a memory dynamically using
malloc()
71. Write a Program to allocate a memory dynamically using
calloc()
72. Write a Program to allocate a memory dynamically using
realloc()

You might also like