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

Sample C Programs

The document provides several C programming examples demonstrating the use of conditional statements (if, else, nested if, else if ladder) and looping constructs (while, do while, for, nested loops). It includes programs to determine if a number is odd or even, find the greatest of three numbers, and identify the largest of four numbers. Additionally, it shows how to print weekdays based on a number and includes examples of different looping techniques.

Uploaded by

indreshreddycr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Sample C Programs

The document provides several C programming examples demonstrating the use of conditional statements (if, else, nested if, else if ladder) and looping constructs (while, do while, for, nested loops). It includes programs to determine if a number is odd or even, find the greatest of three numbers, and identify the largest of four numbers. Additionally, it shows how to print weekdays based on a number and includes examples of different looping techniques.

Uploaded by

indreshreddycr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

IF ELSE STATEMENT

To find odd or even using if else statement

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

// true if num is perfectly divisible by 2


if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}

NESTED IF
Program for to find greatest digit from three numbers using nested if
#include <stdio.h>
int main()
{
int dig1, dig2, dig3;
printf("Enter three numbers: ");
scanf("%d%d%d", &dig1, &dig2, &dig3);
if(dig1 > dig2)
{
if(dig1 > dig3)
{
printf("dig1 is the maximum");
}
else
{
printf("dig3 is the maximum");
}
}
else
{
if(dig2 > dig3)
{
printf("dig2 is the maximum");
}
else
{
printf("dig3 is the maximum");
}
}
return 0;
}

Program to find the largest number from given input and then giving the result whether
or not it is greater or equal after manipulation with nested if statement.

Code:

#include <stdio.h>
int main()
{
int g1, g2;
printf("Get value for g1:");
scanf("%d", &g1);
printf("Get value for g2:");
scanf("%d",&g2);
if (g1 != g2)
{
printf("g1 is not equal to g2\n");
if (g1 > g2)
{
printf("g1 is larger than g2\n");
}
else
{
printf("g2 is larger than g1\n");
}
}
else
{
printf("g1 is equal to g2\n");
}
return 0;
}

ELSE IF LADDER

[Link] to find largest number from four given numbers using if else ladder

#include<stdio.h>
int main()
{
float a,b,c,d, lg;
printf("Enter four numbers:\n");
scanf("%f%f%f%f", &a, &b, &c, &d);

if(a>b && a>c && a>d)


{
lg = a;
}
else if(b>a && b>c && b>d)
{
lg = b;
}
else if(c>a && c>b && c>d)
{
lg = c;
}
else
{
lg = d;
}
printf("Largest = %f", lg);
return(0);
}

2.C program to print weekday based on given number.

#include<stdio.h>

int main()
{
int day;
printf("Enter day number: ");
scanf("%d", &day);
if(day==1)
{
printf("SUNDAY.");
}
else if(day==2)
{
printf("MONDAY.");
}
else if(day==3)
{
printf("TUESDAY.");
}
else if(day==4)
{
printf("WEDNESDAY.");
}
else if(day==5)
{
printf("THURSDAY.");
}
else if(day==6)
{
printf("FRIDAY.");
}
else if(day==7)
{
printf("SATURDAY.");
}
else
{
printf("INVALID DAY.");
}
return(0);
}

Looping statements

While loop

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
while(num<=10)
{
printf("%d\n",num);
num++;
}

return 0;
}

Do while loop

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
do
{
printf("%d\n",2*num);
num++;
}
while(num<=10);
return 0;
}
For loop

#include<stdio.h>
int main()
{
int number;
for(number=1;number<=10;number++)
{
printf("%d\n",number);
}
return 0;
}

Nested loop statement

#include <stdio.h>
int main()
{
int i, j;
int table = 2;
int max = 5;
for (i = 1; i <= table; i++)
{
for (j = 0; j <= max; j++)
{
printf("%d x %d = %d\n", i, j, i*j);
}
printf("\n"); /* blank line between tables */
}
}

You might also like