WAP in C to Convert a Given Number of Days in terms of Years, Weeks & Days(ignore leap year)?

#include<stdio.h>
#include<conio.h>
#define DAYSINWEEK 7

int main()
{
int numdays, year, week, days;
clrscr();
printf(“Enter the number of days”);
scanf(“%d”, &numdays);
year = numdays / 365;
week =(numdays % 365) / DAYSINWEEK;
days =(numdays % 365) % DAYSINWEEK;
printf (“%d is equivalent to %d years, %d weeks and %d days”,numdays, year, week, days);
getch();
return(0);
}

Output :-

convertdaytoweek.png

 

WAP in C to Find GCD of two Numbers?

#include<stdio.h>
#include<conio.h>
int main()
{
int n1, n2, i, g;
clrscr();
printf(“Enter two numbers: “);
scanf(“%d %d”, &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
if(n1%i==0 && n2%i==0)
g = i;
}
printf(“G.C.D of %d and %d is %d”, n1, n2, g);
getch();
return(0);
}

Output :-

gcd

WAP in C to display Day of the month ?

#include<stdio.h>
#include<conio.h>
#include<math.h>

int fm(int date, int month, int year) {
int fmonth, leap;
clrscr();
//leap function 1 for leap & 0 for non-leap
if ((year % 100 == 0) && (year % 400 != 0))
leap = 0;
else if (year % 4 == 0)
leap = 1;
else
leap = 0;

fmonth = 3 + (2 – leap) * ((month + 2) / (2 * month))
+ (5 * month + month / 9) / 2;

//bring it in range of 0 to 6
fmonth = fmonth % 7;

return fmonth;
}

int day_of_week(int date, int month, int year) {

int dayOfWeek;
int YY = year % 100;
int century = year / 100;

printf(“\nDate: %d/%d/%d \n”, date, month, year);

dayOfWeek = 1.25 * YY + fm(date, month, year) + date – 2 * (century % 4);

//remainder on division by 7
dayOfWeek = dayOfWeek % 7;

switch (dayOfWeek)
{
case 0:
printf(“weekday = Saturday”);
break;
case 1:
printf(“weekday = Sunday”);
break;
case 2:
printf(“weekday = Monday”);
break;
case 3:
printf(“weekday = Tuesday”);
break;
case 4:
printf(“weekday = Wednesday”);
break;
case 5:
printf(“weekday = Thursday”);
break;
case 6:
printf(“weekday = Friday”);
break;
default:
printf(“Incorrect data”);
}
return (0);
}

int main() {
int date, month, year;

printf(“\nEnter the year “);
scanf(“%d”, &year);

printf(“\nEnter the month “);
scanf(“%d”, &month);

printf(“\nEnter the date “);
scanf(“%d”, &date);

day_of_week(date, month, year);
getch();
return(0);
}

Output :-

Untitled

WAP in C to Find LCM of two Numbers?

#include<stdio.h>
#include<conio.h>
int main()
{
int n1, n2, min;
printf(“Enter two positive numbers: “);
scanf(“%d %d”, &n1, &n2);
// maximum number between n1 and n2 is stored in min
min = (n1>n2) ? n1 : n2;
// This Condition Is Always true
while(1)
{
if( min%n1==0 && min%n2==0 )
{
printf(“The LCM of %d and %d is %d.”, n1, n2,min);
break;
}
min++;
}
getch();
return(0);
}

Output :-

lcm.png

WAP in C to Find all Roots of a Quadratic equation?

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
float a, b, c;
float desc, r1, r2;
clrscr();
printf(“\nEnter the Values of a : “);
scanf(“%f”, &a);
printf(“\nEnter the Values of b : “);
scanf(“%f”, &b);
printf(“\nEnter the Values of c : “);
scanf(“%f”, &c);
desc = sqrt(b * b – 4 * a * c);
r1 = (-b + desc) / (2.0 * a);
r2 = (-b – desc) / (2.0 * a);
printf(“\nFirst Root : %f”, r1);
printf(“\nSecond Root : %f”, r2);
getch();
return(0);
}

Output :-

Quadratic

WAP in C for function without argument and return type?

#include<stdio.h>
#include<conio.h>
int prime();

int main()
{
int n, i, flag=0;
clrscr();
// no argument is passed to the function
// the value returned from the function is assigned to n
n = prime();
for(i=2; i<=n/2; ++i)
{
if(n%i==0)
{
flag = 1;
break;
}
}
if (flag == 1)
printf(“%d is not a prime number.”, n);
else
printf(“%d is a prime number.”, n);
getch();
return (0);
}

// prime() function returns integer entered by the user
int prime()
{
int n;
printf(“Enter a positive integer: “);
scanf(“%d”,&n);
return(n);
}

Output :-

noaguwithret

WAP in C for function with argument and return type?

#include<stdio.h>
#include<conio.h>
float calculate_area(int);
int main()
{
int radius;
float area;
clrscr();
printf(“\nEnter the radius of the circle : “);
scanf(“%d”,&radius);
area = calculate_area(radius);
printf(“\nArea of Circle : %f “,area);
getch();
return(0);
}
float calculate_area(int radius)
{
float areaofcircle;
areaofcircle = 3.14 * radius * radius;
return(areaofcircle);
}

Output :-

arguret