C programming practice
C programming practice
#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;
}
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;
}
#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;
}
#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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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.");
}
}
}
printf("The biggest number is %d",big);
}
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;
}
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;
}
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;
}
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
#include <stdio.h>
int main() {
int n;
scanf("%d",&n);
printf(" * ");
printf("\n");
return 0;
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;
}
#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");
}
1
121
12321
1234321
123454321
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;
}
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;
}