Write the following program using \"C\" language 1. Write a program using functi
ID: 3675656 • Letter: W
Question
Write the following program using "C" language
1. Write a program using function(use return in each function and the variable be used as parameters):--To round the floating point number to 3 digits.eg if input is 3456.7856 output should be 3456.786
2. Write a program to show whether the year is a leap year or not .If it is then add the digits of the year and write the sum
Format of output should be:
Enter the Year 2016
2016 is a leap year and sum of digits is 9
3. Write a program
If a>b then swap two numbers
If a<b then subtract two numbers
If two numbers are equal then add the numbers
The output should be displayed this way
5>3 a=3 b=5
3<5 c=b-a c=2
5==5 c=b+a c=10
Explanation / Answer
1.
#include<stdio.h>
#include<conio.h>
float round(float n)
{
char charn[10],ch[10],c;
int n1,j,i;
float nn;
clrscr();
sprintf(charn,"%f",n);
printf("%s",charn);
for(i=0;i<10;i++)
{
if(charn[i]=='.')
{
c=charn[i+3];
n1=atoi(c);
printf(" %d",n1);
n1++;
charn[i+3]=itoa(n1);
j=i+3;
break;
}
}
for(i=0;i<=j;i++)
{
ch[i]=charn[i];
}
printf(" %s",ch);
nn=(float)atof(ch);
return nn;
}
void main()
{
float aa=3456.7856,bb;
bb=round(aa);
printf("%f",bb);
getch();
}
2.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,year;
clrscr();
printf(" Enter year");
scanf("%d",&year);
a=year%4;
y=year;
if(a==0)
{
for(i=0;i<4;i++)
{
sum=sum+(year%10);
year=year/10;
}
printf("%d Year is Leap and sum of digits is %d",y,sum);
}
else
{
printf(" Year is not Leap");
}
getch();
}
3.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter no");
scanf("%d",&a);
printf("Enter no");
scanf("%d",&b);
if(a==b)
c=a+b;
if(a>b)
{
c=a;
a=b;
b=c;
}
else if(b>c)
{
c=b-a;
}
printf("%d is a, %d is b, %d is c",a,b,c);
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.