www.Vidyarthiplus.
com REGISTER NUMBER
/*program using i/o statements and expressions*/
#include<stdio.h>
#include<conio.h>
Void main()
{
float totamt,amount,subtot,distant,taxamt,quantity,value,discount,tax;
printf("\nenter the quantity of item sold:");
scanf("%f",&quantity);
printf("\n enter the value of item:");
scanf("%f",&value);
printf("\n enter the discount percentage:");
scanf("%f",&discount);
printf("\n enter the tax:");
scanf("%f",&tax);
amount=quantity*value;
distant=(amount*discount)/100.0;
subtot=amount-distant;
taxamt=(subtot*tax)/100.0;
totamt=subtot+taxamt;
printf("\n\n\n******bill******");
printf("\n quantity sold:%f",quantity);
printf("\n priceperitem:%f",value);
printf("\n amount %f",amount);
printf("\n discount:%f",distant);
printf("\n discounted total: %f",subtot);
printf("\n tax:+ %f",taxamt);
printf("\n total amount %f",totamt);
getch();
}
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/*program to find Area & Perimeter of Circle*/
#include<stdio.h>
#include<conio.h>
#define pi 3.14f
void main()
float rad,area,perm;
clrscr();
printf("enter the radius of circle:");
scanf("%f",&rad);
area=pi*rad*rad;
perm=2*pi*rad;
printf("area of circle:%f\n perimeter of circle:%f\n",area,perm);
getch();
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/*Program to check whether a person is eligible to Vote or Not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[50];//or*name
printf("\n Type the name of the candidate:");
gets(name);
printf("\n Enter the age:");
scanf("%d",&age);
if(age>=18)
printf("\n %s is Eligibile for vote",name);
else
printf("\n %s is not Eligibile for vote",name);
getch();
}
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/*Program to find largest of three given numbers*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
int big;
printf("Enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
big=a;
else if(b>a&&b>c)
big=b;
else
big=c;
printf("largest number is=%d",big);
return 0;
getch();
}
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/* Program to find whether the given year is leap year or Not. */
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
printf("enter a year:");
scanf("%d",&year);
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
}
else
printf("%d is a leap year",year);
}
else
printf("%d is not a leap year",year);
getch();
}
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/*Program to do Arithmetic operations.*/
#include<stdio.h>
void main()
{
int a,b,result,sq1,sq2,ch;
float divide;
clrscr();
printf("enter two integers:");
scanf("%d%d",&a,&b);
printf("1.add,2.subtrat,3.multiply,4.divide,5.sqare");
printf("\n enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
result=(a+b);
printf("sum=%d\n",result);
break;
}
case 2:
{
result=(a-b);
printf("difference=%d\n",result);
break;
}
case 3:
{
result=(a*b);
printf("mutiplication=%d\n",result);
break;
}
case 4:
{
result=a/(float)b;
printf("diviosin=%2f\n",result);
break;
}
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
case 5:
{
sq1=(a*a);
printf("square=%d\n",sq1);
sq2=b*b;
printf("second square number=%\n",sq2);
break;
}
}
getch();
}
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/*Program to find the given number is Armstrong or not . */
#include<stdio.h>
#include<cono.h>
void main()
{
int n,orgnum,r,result=0;
printf("enter a three digit number:");
scanf("%d",&n);
orgnum=n;
while(orgnum!=0)
{
r=orgnum%10;
result+=r*r*r;
orgnum/=10;
}
if(result==n)
printf("%d is an armstrong number",n);
else
printf("%d is not an armstrong number",n);
getch();
}
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/*Program to Sort the numbers based on the weight. */
#include <stdio.h>
#include <math.h>
void main()
int nArray[50],wArray[50],nelem,i,j,t;
clrscr();
printf("\nEnter the number of elements in an array : ");
scanf("%d",&nelem);
printf("\nEnter %d elements\n",nelem);
for(i=0;i<nelem;i++)
scanf("%d",&nArray[i]);
//Calculate the weight
for(i=0; i<nelem; i++)
wArray[i] = 0;
if(percube(nArray[i]))
wArray[i] = wArray[i] + 5;
if(nArray[i]%4==0 && nArray[i]%6==0)
wArray[i] = wArray[i] + 4;
if(prime(nArray[i]))
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
wArray[i] = wArray[i] + 3;
// Sorting an array
for(i=0;i<nelem;i++)
for(j=i+1;j<nelem;j++)
if(wArray[i] > wArray[j])
t = wArray[i];
wArray[i] = wArray[j];
wArray[j] = t;
for(i=0; i<nelem; i++)
printf("<%d,%d>\n", nArray[i],wArray[i]);
getch();
int prime(int num)
int flag=1,i;
for(i=2;i<=num/2;i++)
if(num%i==0)
flag=0;
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
break;
return flag;
int percube(int num)
int i,flag=0;
19
for(i=2;i<=num/2;i++)
if((i*i*i)==num)
flag=1;
break;
return flag;
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/* Get a Height of Different Persons and find how many of them are are above
average */
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n,sum=0,count=0,height[100];
float avg;
clrscr();
//Read Number of persons
printf("Enter the Number of Persons : ");
scanf("%d",&n);
//Read the height of n persons
printf("\nEnter the Height of each person in centimeter\n");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
sum = sum + height[i];
}
avg = (float)sum/n;
//Counting
for(i=0;i<n;i++)
if(height[i]>avg)
count++;
//display
printf("\nAverage Height of %d persons is : %.2f\n",n,avg);
printf("\nThe number of persons above average : %d ",count);
getch();
}
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/* Program to find Body Mass Index of the individuals */
#include<stdio.h>
#include<math.h>
void main(void){
int n,i,j;
float massheight[10][10];
float bmi[10];
printf("How many people's BMI do you want to calculate?\n");
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<2;j++){
switch(j){
case 0:
printf("\nPlease enter the mass of the person %d in kg: ",i+1);
scanf("%f",&massheight[i][0]);
break;
case 1:
printf("\nPlease enter the height of the person %d in meter: ",i+1);
scanf("%f",&massheight[i][1]);
break;}
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
for(i=0;i<n;i++){
bmi[i]=massheight[i][0]/pow(massheight[i][1],2.0);
printf("Person %d's BMI is %f\n",i+1,bmi[i]);
getch();
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/*Program to reverse the string without changing the position of special
characters for the given string.*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
void swap(char *a, char *b)
{
char t;
t = *a;
*a = *b;
*b = t;
}
// Main program
void main()
{
char str[100];
// Function Prototype
void reverse(char *);
int isAlpha(char);
void swap(char *a ,char *b);
clrscr();
printf("Enter the Given String : ");
// scanf("%[^\n]s",str);
gets(str);
reverse(str);
printf("\nReverse String : %s",str);
getch();
}
void reverse(char str[100])
{
// Initialize left and right pointers
int r = strlen(str) - 1, l = 0;
// Traverse string from both ends until
// 'l' and 'r'
while (l < r)
{
// Ignore special characters
if (!isAlpha(str[l]))
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
l++;
else if(!isAlpha(str[r]))
r--;
else
{
swap(&str[l], &str[r]);
l++;
r--;
}
}
}
// To check x is alphabet or not if it an alphabet then return 0 else 1
int isAlpha(char x)
{
return ( (x >= 'A' && x <= 'Z') ||
(x >= 'a' && x <= 'z') );
}
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
/*C Program to Convert the given decimal number into binary, octal and
hexadecimal numbers using user defined functions.*/
#include <stdio.h>
#include <conio.h>
void swap(char *s1, char *s2)
char temp;
temp = *s1;
*s1 = *s2;
*s2 = temp;
void reverse(char *str, int length)
int start = 0;
int end = length -1;
while (start < end)
swap(&str[start], &str[end]);
start++;
end--;
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
char* convert(int num, char str[100], int base)
int i = 0;
if (num == 0)
str[i++] = '0';
str[i] = '\0';
return str;
while (num != 0)
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
str[i] = '\0'; // Append string terminator
// Reverse the string
reverse(str, i);
return str;
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
void main()
char str[100];
int n;
clrscr();
printf("Enter the given decimal number : ");
scanf("%d",&n);
printf("\nThe Binary value : %s\n",convert(n,str,2));
printf("\nThe Octal value : %s\n",convert(n,str,8));
printf("\nThe Hexa value : %s\n",convert(n,str,16));
getch();
CS8261 - C PROGRAMMING LABORATORY
www.Vidyarthiplus.com REGISTER NUMBER
OUTPUT:
CS8261 - C PROGRAMMING LABORATORY