C Practice Programs
[Link] a program to display the names of the days of a week.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int day,i;
pf("Enter a number between 1 to 7");
sf("%d",&day);
for(i=1;i<=day;i++)
switch(i)
{
case 1:
pf("\n 1st day of the week is Sunday");
break;
case 2:
pf("\n 2nd day of the week is Monday");
break;
case 3:
pf("\n 3rd day of the week is Tuesday");
break;
case 4:
pf("\n 4th day of the week is Wednesday");
break;
case 5:
pf("\n 5th day of the week is Thursday");
break;
case 6:
pf("\n 6th day of the week is Friday");
break;
case 7:
pf("\n 7th day of the week is Saturday");
break;
default:
pf("\n Invalid day");
exit(0);
}
}
1|P ag e
C Practice Programs
[Link] a program to detect whether the entered number is even or odd. use nested switch () case
statements.
#include<stdio.h>
void main() {
int x,y:
pf("\n Enter a Number");
sf("%d",&x);
switch(x)
{
case 0:
pf("\n Number is Even");
break;
case 1:
pf("\n Number is Odd");
break;
default:
y=x%2;
switch(y)
{
case 0:
pf("\n Number is Even");
break;
default:
pf("\n Numberis Odd");
break;
} }
getch();
}
*********************************************************************************
3. Display numbers from 1 to 15 using for loop. Use i++.
#include<stdio.h>
void main()
{
int i;
for(i=1;i<=15;i++)
printf("%5d",i);
}
*********************************************************************************
[Link] numbers from 1 to 15 using for loop. Use i=i+1.
#include<stdio.h>
void main()
{
int i;
{
for(i=1;i<=15;i=i+1)
printf("%5d",i);
}
*********************************************************************************
2|P ag e
C Practice Programs
[Link] a program to display even numbers from 0 to 14. Declare the initial counter value before
the for-loop statement.
#include<stdio.h>
void main() {
int i=0;
for(;i<=15;)
{
printf("%5d",i);
i+=2;
}}
*********************************************************************************
[Link] a program to display numbers in ascending and descending orders.
#include<stdio.h>
void main() {
int i=0;
printf("Numbers in Ascending Order:");
for(;++i<=10;)
printf("%3d",i);
printf(\n\n);
printf("Numbers in Decending Order:");
for(;i-->1;)
printf("%3d",i);
}
*********************************************************************************
[Link] a program to display Odd numbers from 0 to 15. Declare the initial counter value before
the for loop statement.
#include<stdio.h>
void main() {
int i=1;
for(;i<=15;)
{
printf("%5d",i);
i+=2;
} }
*********************************************************************************
While Loop
[Link] a program to print the string "C Language" 9 times using While Loop.
#include<stdio.h>
void main()
{
int x=1;
while(x<10)
{
printf("\n C Language");
x++;
} }
3|P ag e
C Practice Programs
Do-While Loop
[Link] a program print C Language for 5 times using do-while loop.
#include<stdio.h>
void main() {
int i=1;
do
{
printf("\n C Language");
i++;
}
while(i<=5);
}
*********************************************************************************
[Link] to join two static strings together, the following code is required.
#include<stdio.h>
#include<conio.h>
char*s1="string one";
char*s2="string two";
int main() {
char buffer[50];
strcat(buffer,s1);
strcat(buffer,s2);
printf("%s",buffer);
return 0;
}
*********************************************************************************
[Link] to demonstrate some Math functions.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float result,value;
clrscr();
printf("Input a float value:");
scanf("%f",&value);
result=sqrt(value);
printf("the square root of %.2f is %.2f\n", value,result);
result=pow(value,3);
printf("%.2f to the 3rd power is %.2f\n",value,result);
result=floor(value);
printf("The floor of %.2f is %.2f\n",value,result);
result=ceil(value);
printf("And the ceiling of %.2f is %.2f\n",value,result);
return 0;
}
**********************************************************************************
4|P ag e
C Practice Programs
[Link] to find the length of the string using strlen() function.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char name[100];
int length;
clrscr();
printf("Enter the string");
gets(name);
length=strlen(name);
printf("Number of characters in the string=%d",length);
getch();
return 0;
}
*********************************************************************************
[Link] a Program to check that the entered year is a Leap year or not. Use goto statement.
#include <stdio.h>
int main()
{
int leap;
printf("enter year");
scanf("%d",&leap);
if(leap%4==0)
goto leap;
else goto noleap;
leap:
printf("%d is a leap year");
return;
noleap:
printf("%d is not a leap year");
return 0;
}
Output:
enter year2000
2000 is a leap year
*********************************************************************************
[Link] a Program to detect the entered number as to whether it is even or odd. use goto
statement.
#include <stdio.h>
int main()
{
int x;
printf("Enter a number");
scanf("%d",&x);
if(x%2==0)
5|P ag e
C Practice Programs
goto even;
else
goto odd;
even:
printf("\n %d is Even Number");
return;
odd:
printf("\n %d is Odd Number");
return 0;
}
Output:
Enter a number1
1 is Odd Number
[Link] a program to calculate telephone bill. Transfer controls at different places according to
number of calls and calculate the total charges. follow rates int the table.
Telephone call Rate in Rs.
<100 No Charges
>99&<200 1
>199&<300 2
>299 3
#include <stdio.h>
int main() {
int nc;
printf("\n Enter Number of Calls:");
scanf("%d",&nc);
if(nc<100)
goto free;
else if(nc>99&&nc<200)
goto charge1;
else if(nc>199&&nc<300)
goto charge2;
else
goto charge3;
free:
printf("\n No charges");
return;
charge1:
printf("\n Total Charges:%d Rs.",nc*1);
return;
charge2:
printf("\n Total Charges:%d Rs.",nc*2);
return;
charge3:
printf("\n Total Charges:%d Rs.",nc*3);
return 0; }
********************************************************************************
6|P ag e
C Practice Programs
16. Write a program to print Arithmetic operators using Switch case.
#include<stdio.h>
#include<stdlib.h>
int main() {
int a,b,c,ch;
printf("\t ==========");
printf("\n\t MENU");
printf("\n\t\[1] ADDITION");
printf("\n\t\[2] SUBSTRACTION");
printf("\n\t\[3] MULTIPLICATION");
printf("\n\t\[4] DIVISION");
printf("\n\t\[5] REMAINDER");
printf("\n\t\[6] LARGER OUT OF TWO");
printf("\n\t\[0] EXIT");
printf("\n\t\==================");
printf("\n\n\t ENTER YOUR CHOICE");
scanf("%d",&ch);
if(ch<=6 & ch>0)
{
printf("Enter Two Numbers");
scanf("%d %d",&a,&b);
}
switch(ch)
{
case 1:
c=a+b;
printf("\n Addition:%d",c);
break;
case 2:
c=a-b;
printf("\n Substraction:%d",c);
break;
case 3:
c=a*b;
printf("\n Multiplication:%d",c);
break;
case 4:
c=a/b;
printf("\n Division:%d",c);
break;
case 5:
c=a%b;
printf("\n Remainder:%d",c);
break;
case 6:
if(a>b)
printf("\n\t %d (a)is larger than %d(b)",a,b);
else
7|P ag e
C Practice Programs
if(b>a)
printf("\n\t %d (b)is larger than %d(a)",b,a);
else
printf("\n\t %d(a)&%d(b) are same",a,b);
break;
case 0:
printf("\n\t Terminated by choice");
exit(0);
break;
default:
printf("\n Invalid Choice");
}
return 0;
}
Output:
ENTER YOUR CHOICE 1
Enter Two Numbers 2 6 Addition:8
ENTER YOUR CHOICE 2
Enter Two Numbers 3 6 Substraction:3
ENTER YOUR CHOICE 3
Enter Two Numbers 5 4 Multiplication:20
ENTER YOUR CHOICE 4
Enter Two Numbers 6 3 Divison:2
ENTER YOUR CHOICE 5
Enter Two Numbers 5 10 Remainder:0
ENTER YOUR CHOICE 6
Enter Two Numbers 6 5 Larger out of two: 6 (a)is larger than 5(b)
ENTER YOUR CHOICE 0
Enter Two Numbers 0: Exit Terminated by choice
[Link] to print different data types of memory storage using Size of() operator
#include<stdio.h>
#include<conio.h>
int main() {
int x;
float y;
char z;
char str[5]="world";
printf("enter x,y,z str values");
scanf("%d%f%c",&x,&y,&z);
printf("size of int=%d bytes\n",sizeof(x));
printf("size of float=%d bytes\n",sizeof(y));
printf("size of char=%d bytes\n",sizeof(z));
printf("size of string=%d bytes\n",sizeofstr(str));
printf("address of x=%u\n address of y=%u\n address of z=%u\n address of
str=%u\n",&x,&y,&z,&str);
return 0; }
*********************************************************************************
8|P ag e
C Practice Programs
[Link] to print using char data type
#include<stdio.h>
#include<conio.h>
void main()
{
char grade;
char name[10];
char place[10];
clrscr();
printf("enter your grade\n");
scanf("%c",&grade);
printf("enter your name\n");
scanf("%s",&name);
printf("enter your place\n");
scanf("%s",place);
printf("your name is %s,grade is %c & place is %s",name,grade,place);
getch();
}
*********************************************************************************
19. Write a program to Add two integers
#include<stdio.h>
int main()
{
int a,b,sum;
printf("\n Enter any two integers:");
scanf("%d %d",&a,&b);
sum=a+b;
printf("\n Sum of two integers a=%d and b=%d Sum=%d",a,b,sum);
return 0;
}
*********************************************************************************
20. Write a program to print integer (entered by the User)
#include<stdio.h>
int main()
{
int a;
printf("\n Enter any integer:");
scanf("%d",&a);
printf("\n Integer entered a=%d",a);
return 0;
}
9|P ag e
C Practice Programs
21. Write a program to multiply two floating numbers.
#include<stdio.h>
int main()
{
float a,b, product;
printf("\n Enter any two floating numbers:");
scanf("%f %f",&a,&b);
product=a*b;
printf("\n The product of a=%f and b=%f Product=%f",a,b,product);
return 0;
}
*********************************************************************************
22. Write a C program to find ASCII value of a character.
#include <stdio.h>
int main()
{
char c;
printf("Enter any Character");
scanf("%c",&c);
printf("\n ASCII code of %c character is: %d",c,c);
return 0;
}
*********************************************************************************
10 | P a g e