0% found this document useful (0 votes)
109 views6 pages

Prog Lab Day 2

The document contains multiple C programs demonstrating various programming concepts, including swapping numbers with and without a third variable, calculating the area of a triangle using Heron's formula, checking if a number is odd or even, determining leap years, and finding the largest of two or three numbers using different methods. Each program includes user input, processing logic, and output statements. The examples illustrate fundamental programming techniques in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views6 pages

Prog Lab Day 2

The document contains multiple C programs demonstrating various programming concepts, including swapping numbers with and without a third variable, calculating the area of a triangle using Heron's formula, checking if a number is odd or even, determining leap years, and finding the largest of two or three numbers using different methods. Each program includes user input, processing logic, and output statements. The examples illustrate fundamental programming techniques in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

C Program to swap two numbers using a third variable


//C Program to swap two numbers using a third variable
#include<stdio.h>
int main()
{
int a,b,t;
printf("Enter two integers: ");
scanf("%d %d",&a,&b);
printf("\n Before Swapping: a=%d,b=%d",a,b);
t=a;a=b;b=t;
printf("\n After Swapping: a=%d,b=%d",a,b);
return 0;
}

2. C Program to swap two numbers without using a third variable


//C Program to swap two numbers without using a third variable
#include<stdio.h>
int main()
{
int a,b;
printf("Enter two integers: ");
scanf("%d %d",&a,&b);
printf("\n Before Swapping: a=%d,b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n After Swapping: a=%d,b=%d",a,b);
return 0;
}

3. C Program to print the area of a triangle using Heron's formula


//C Program to print the area of a triangle using Heron's formula
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,s,ar;
printf("Enter the three sides of the triangle: ");
scanf("%f %f %f",&a,&b,&c);
s=(a+b+c)/2.0;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\n The area of the triangle is %.2f [Link]", ar);
return 0;
}

4. C Program to test a given number is odd or even


#include<stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d",&num);
if(num%2==0)
printf("%d is even",num);
else
printf("%d is odd",num);
return 0;
}

5. C Program to test a given year is a leap year or not


#include<stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%400 == 0)
printf("%d is a leap year",year);
else if ((year%100 != 0) && (year%4 == 0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
return 0;
}

6. C Program to find the larger of two numbers


#include<stdio.h>
int main()
{
int a,b;
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
if(a > b)
printf("%d is larger",a);
else if (b > a)
printf("%d is larger",b);
else
printf("Both numbers are equal");
return 0;
}
7. C Program to find the larger of two numbers using conditional operator
(?:)
#include<stdio.h>
int main()
{
int a,b,max;
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
max=(a>b)?a:b;
printf("%d is the largest", max);
return 0;
}

6. C Program to find the largest of three numbers using nested-if structure


#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c;
printf("Enter three numbers: ");
scanf("%d %d %d",&a,&b, &c);
if(a==b && b==c)
{
printf("\n All three numbers are equal");
exit(1);
}
if(a > b) {
if(a > c)
printf("%d is largest",a);
else
printf("%d is largest",c); }
if(b > a) {
if(b > c)
printf("%d is largest",b);
else
printf("%d is largest",c); }
return 0;
}

7. C Program to find the largest of three numbers using logical AND


#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers: ");
scanf("%d %d %d",&a,&b, &c);
if((a > b) && (a > c))
printf("%d is largest",a);
else if ((b > a) && (b > c))
printf("%d is largest",b);
else
printf("%d is largest",c);
return 0;
}

8. C Program to find the largest of three numbers using conditional


operator (?:)
#include<stdio.h>
int main()
{
int a,b,c,max;
printf("Enter three numbers: ");
scanf("%d %d %d",&a,&b,&c);
max=((a>b && a>c)?a:(b>a && b>c)?b:c);
printf("%d is the largest", max);
return 0;
}

You might also like