Unit II Programs
Unit II Programs
Program 1
#include<stdio.h>
int main()
{
int num1=30;
int num2=40;
printf("Value of %d > %d is %d\n",num1,num2,num1>num2);
printf("Value of %d >= %d is %d\n",num1,num2,num1>=num2);
printf("Value of %d <= %d is %d\n",num1,num2,num1<=num2);
printf("Value of %d < %d is %d\n",num1,num2,num1<num2);
printf("Value of %d == %d is %d\n",num1,num2,num1==num2);
printf("Value of %d != %d is %d",num1,num2,num1!=num2);
return 0;
}
Output
Value of 30 > 40 is 0
Value of 30 >= 40 is 0
Value of 30 <= 40 is 1
Value of 30 < 40 is 1
Value of 30 == 40 is 0
Value of 30 != 40 is 1
Program 2
#include<stdio.h>
int main()
{
int a=10,b=4;
if (a>b)
printf("a is greater than b\n");
else
printf("a is less than or equal to b\n");
if(a>=b)
printf("a is greater than or equal to b\n");
else
printf("a is lesser than b\n");
if(a<b)
printf("a is less than b\n");
else
printf("a is greater than or equal to b\n");
if(a<=b)
printf("a is lesser than or equal to b\n");
else
printf("a is greater than b\n");
if(a==b)
printf("a is equal to b\n");
else
printf("a and b are not equal\n");
if (a != b)
printf("a is not equal to b\n");
else
printf("a is equal b\n");
return 0;
}
Output
a is greater than b
a is greater than or equal to b
a is greater than or equal to b
a is greater than b
a and b are not equal
a is not equal to b
Logical Operators
program 1
#include <stdio.h>
int main()
{
int a=5,b=5,c=10,result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) equals to %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result);
result = !(a != b);
printf("!(a == b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0;
}
Output
(a == b) && (c > b) equals to 1
(a == b) && (c < b) equals to 0
(a == b) || (c < b) equals to 1
(a != b) || (c < b) equals to 0
!(a == b) equals to 1
!(a == b) equals to 0
program 2
#include <stdio.h>
int main()
{
int a=10,b=4,c=10,d=20;
if(a>b && c==d)
printf("a is greater than b AND c is equal to d\n");
else
printf("AND condition not satisfied\n");
if(a>b||c==d)
printf("a is greater than b OR c is equal to d\n");
else
printf("Neither a is greater than b nor c is equal to d\n");
if(!a)
printf("a is zero\n");
else
printf("a is not zero");
return 0;
}
Output
AND condition not satisfied
a is greater than b OR c is equal to d
a is not zero
Condition Operators
Program 1
#include <stdio.h>
int main()
{
int x=1,y;
y=(x==1?2:0);
printf("x value is %d\n",x);
printf("y value is %d",y);
}
Output
x value is 1
y value is 2
Program 2
#include <stdio.h>
int main(){
char February;
int days;
printf("If this year is leap year, enter 1. If not enter any integer: ");
scanf("%c",&February);
// If test condition (February == 'l') is true, days equal to 29.
// If test condition (February =='l') is false, days equal to 28.
days = (February == '1') ? 29 : 28;
printf("Number of days in February = %d",days);
return 0;
}
Output
If this year is leap year, enter 1. If not enter any integer: 1
Number of days in February = 29
#include<stdio.h>
int main()
{
int n1,n2,big;
printf("Enter Two Numbers:");
scanf("%d%d",&n1,&n2);
big=((n1>n2)?n1:n2);
printf("The Greater Number is %d",big);
return 0;
}
Output
Enter Two Numbers:3
6
The Greater Number is 6
Program 5: Write a C program to find maximum between three numbers using conditional
operator.
#include<stdio.h>
void main()
{
int a,b,c,big ;
printf("Enter three numbers:");
scanf("%d%d%d",&a,&b,&c) ;
big=a>b?(a>c?a:c):(b>c?b:c);
printf("The biggest number is : %d",big) ;
}
Output
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
(number % 2 == 0) ? printf("%d is even.", number) : printf("%d is odd.", number);
return 0;
}
Output:
Enter an integer: 3
3 is odd.
Enter an integer: 4
4 is even.
Program 7: Write a C program to check whether year is leap year or not using conditional
operator.
#include <stdio.h>
int main()
{
int year;
printf("Enter any year: ");
scanf("%d", &year);
(year%4==0 && year%100!=0)?printf("LEAP YEAR") :(year%400 ==0 )?printf("LEAP
YEAR") : printf("COMMON YEAR");
return 0;
}
Output
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character:");
scanf("%c", &ch);
(ch>='a' && ch<='z') || (ch>='A' && ch<='Z') ? printf("It is ALPHABET") : printf("It is NOT
ALPHABET");
return 0;
}
Output
#include <stdio.h>
int main()
int i, n, t1 = 0, t2 = 1, nextTerm;
scanf("%d", &n);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
return 0;
Output:
#include <stdio.h>
int main() {
int x, i;
scanf("%d", &x);
if((i%x) == 3) {
printf("%d\n", i);
return 0;
Output:
Input an integer: 48
3
51
99
147
195
243
291
339
387
435
483
3.Display stars in specific order.
#include <stdio.h>
int main()
int row, c, n, s;
scanf("%d", &n);
s = n;
printf(" ");
s--;
printf("*");
printf("\n");
return 0;
Output:
***
*****
*******
4.Detect a number entered is palindrome or not.
#include <stdio.h>
int main()
while( n!=0 )
remainder = n%10;
n /= 10;
if (originalInteger == reversedInteger)
else
return 0;
Output:
#include <stdio.h>
#include <stdio.h>
Int main()
number = 001;
if (temp == number)
number++;
return 0;
}
Output:
Armstrong no is:1
Armstrong no is:153
Armstrong no is:370
Armstrong no is:371
Armstrong no is:407
6.Reverse the entered number.
#include <stdio.h>
int main()
scanf("%d", &n);
while(n != 0)
remainder = n%10;
n /= 10;
return 0;
Output:
#include <stdio.h>
int main()
int n, i;
scanf("%d",&n);
return 0;
Output:
Enter an integer: 5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
7.Prime numbers between 1 to 100 in C Programming Language
#include <stdio.h>
int main()
count = 0;
if(Number%i == 0)
count++;
break;
return 0;
}
Output:
#include <stdio.h>
int main() {
int counter;
if(counter%2 == 1) {
return 0;
Output:
#include <stdio.h>
int main() {
int n,last;
scanf("%d",&last);
n=2;
while(n<=last)
printf(" %d",n);
n=n+2;
n=1;
while(n<=last)
printf(" %d",n);
n=n+2;
return 0;
Output:
Enter Last Number : 25
Even Number List : 2 4 6 8 10 12 14 16 18 20 22 24
#include <stdio.h>
int main() {
int i;
scanf("%lf",&number);
goto jump;
jump:
average=sum/(i-1);
return 0;
Output:
1. Enter a number: 25
2. Enter a number: 28
3. Enter a number: 46
4. Enter a number: 89
5. Enter a number: 50
Sum = 238.0
Average = 47.60
11.Program to Count Number of Digits in an Integer
#include <stdio.h>
int main() {
long long n;
int count = 0;
scanf("%lld", &n);
while(n != 0)
n /= 10;
++count;
Output:
Enter an integer: 89
Number of digits: 2
12.Program to print half pyramid using *
#include <stdio.h>
int main()
int i, j, rows;
scanf("%d",&rows);
printf("* ");
printf("\n");
return 0;
Output:
**
***
****
*****
13.Program to print half pyramid a using numbers
#include <stdio.h>
int main()
int i, j, rows;
scanf("%d",&rows);
printf("%d ",j);
printf("\n");
return 0;
Output:
12
123
1234
12345
Switch Case:
#include <stdio.h>
int main()
int color = 1;
scanf("%d", &color);
switch (color) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
return 0;
}
Output:
#include <stdio.h>
int main()
int day;
scanf("%d",&day);
switch(day)
case 1:
printf("1 - Sunday");
break;
case 2:
printf("2 - Monday");
break;
case 3:
printf("3 - Tuesday");
break;
case 4:
printf("4 - Wednesday");
break;
case 5:
printf("5 - Thursday");
break;
case 6:
printf("6 - Friday");
break;
case 7:
printf("7 - Saturday");
break;
default:
return 0;
Output:
2 - Monday
16.Write a c program continue statement inside for loop
#include <stdio.h>
int main()
if (j==4)
continue;
return 0;
Output:
01235678
17.write a c program continue in do-While loop
#include <stdio.h>
int main()
int j=0;
do
if (j==7)
j++;
continue;
j++;
}while(j<10);
return 0;
Output:
012345689
CONTROL STATEMENTS
EXAMPLE PROGRAM FOR IF STATEMENT IN C:
Program 1:
int main()
{
int m=40,n=40;
if (m == n)
{
printf("m and n are equal");
}
}
Output
m and n are equal
Program 2:
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
Output:
Variable x is less than y
Program 3:
#include <stdio.h>
int main()
{
int x, y;
printf("enter the value of x:");
scanf("%d", &x);
printf("enter the value of y:");
scanf("%d", &y);
if (x>y)
{
printf("x is greater than y\n");
}
if (x<y)
{
printf("x is less than y\n");
}
if (x==y)
{
printf("x is equal to y\n");
}
printf("End of Program");
return 0;
}
Output:
enter the value of x:5
enter the value of y:5
x is equal to y
End of Program
Output:
Enter two numbers:6
5
6 is maximum
Output:
Enter a number:6
The number 6 is positive.
EXAMPLE PROGRAM FOR IF ELSE STATEMENT IN C
Program 1:
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
return 0;
}
Output:
m and n are not equal
program 2:
Program to check whether an integer entered by the user is odd or even
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
Output:
Enter an integer:8
8 is an even integer
Enter an integer:7
7 is an odd integer
program 3:
#include <stdio.h>
int main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
{
printf("You are eligible for voting");
}
else
{
printf("You are not eligible for voting");
}
return 0;
}
Output:
Enter your age:18
You are eligible for voting
Output:
Enter a number:6
The number 6 is positive.
Enter a number:-7
The number -7 is negative.
program 5:
#include<stdio.h>
#include<string.h>
int main()
{
char a[20],b[20];
printf("Enter the first string:");
scanf("%s",a);
printf("Enter the second string:");
scanf("%s",b);
if((strcmp(a,b)==0))
{
printf("Strings are the same");
}
else
{
printf("Strings are different");
}
return 0;
}
Output:
Enter the first string:hai
Enter the second string:hai
Strings are the same
program 1:
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m>n) {
printf("m is greater than n");
}
else if(m<n) {
printf("m is less than n");
}
else {
printf("m is equal to n");
}
}
Output
m is greater than n
program 2:
#include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2\n");
//Nested if else
if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else
{
printf("var2 is greater than var1\n");
}
}
else
{
printf("var1 is equal to var2\n");
}
return 0;
}
Output:
Input the value of var1:12
Input the value of var2:21
var1 is not equal to var2
var2 is greater than var1
EXAMPLE PROGRAM FOR SWITCH-CASE STATEMENT IN C
Program 1:
Output:
Enter the number of the day:6
Friday
Output:
Enter an operator (+, -, *, /):+
Enter two operands:3
4
3.0 + 4.0 = 7.0
Program 3:
#include <stdio.h>
int main()
{
int x = 2;
switch (x)
{
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
case 3: printf("Choice is 3");
break;
default: printf("Choice other than 1, 2 and 3");
break;
}
return 0;
}
Output:
Choice is 2
ARRAYS
#include <stdio.h>
int main()
int i,n;
int marks[n];
int sum=0;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&marks[i]);
sum+=marks[i];
return 0;
Output:
#include <stdio.h>
void display(int a)
printf("%d",a);
int main()
int c[]={2,3,4};
display(c[2]);
return 0;
Output:
4
3.Write Program to count total number of array elements in C
#include <stdio.h>
int main()
int arr[]={10,20,30,40,50};
int n;
n=sizeof(arr)/sizeof(int);
return 0;
Output:
#include <stdio.h>
int i=0,temp=0;
temp = array[i];
array[i] = array[n-i-1];
array[n-i-1] = temp;
int main()
scanf("%d",&n);
{
printf("array_1[%d] : ",i);
scanf("%d",&array_1[i]);
Array_Swap(array_1 , n);
printf("\narray_1[%d] : %d",i,array_1[i]);
return 0;
Output:
array_1[0] : 2
array_1[1] : 3
array_1[2] : 5
array_1[3] : 8
array_1[4] : 6
array_1[0] : 6
array_1[1] : 8
array_1[2] : 5
array_1[3] : 3
array_1[4] : 2
5.Write a program in C to read n number of values in an array and display it
in reverse order.
#include <stdio.h>
int main()
int i,n,a[100];
scanf("%d",&n);
for(i=0;i<n;i++)
printf("element - %d : ",i);
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("% 5d",a[i]);
}
printf("\n\nThe values store into the array in reverse are :\n");
for(i=n-1;i>=0;i--)
printf("% 5d",a[i]);
printf("\n\n");
return 0;
Output:
element -0 : 1
element 1 : 2
element - 2 : 3
element -3 : 4
element -4 : 5
element -5 : 6
element 6 : 7
element 7 : 8
element 8 : 9
element 9 : 10
The values store into the array are : 1 2 3 4 5 6 7 8 9 10
#include <stdio.h>
int main()
int a[100];
int i, n, sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("element - %d : ",i);
scanf("%d",&a[i]);
sum += a[i];
return 0;
}
Output:
element - 1 : 9
element -2 : 4
element -3 : 5
element -4 : 6
#include <stdio.h>
int main()
int arr[10];
int i;
printf("element - %d : ",i);
scanf("%d", &arr[i]);
printf("\n");
return 0;
OutPut:
Read and Print elements of an array:
element -0 : 4
element -1 : 2
element 2 : 6
element - 3 : 8
element -4 : 7
element 5 : 1
element -6 : 3
element -7 : 2
element -8 : 4
element - 9 : 5
#include <stdio.h>
int main()
int a[100];
int i, n, sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("element - %d : ",i);
scanf("%d",&a[i]);
sum += a[i];
return 0;
}
Output:
element 1 : 4
element - 2 : 7
element -3 : 8
element -4 : 6
#include <stdio.h>
int main()
printf("Enter n: ");
scanf("%d", &n);
scanf("%d", &marks[i]);
sum += marks[i];
average = sum/n;
return 0;
Output:
Enter n: 5
Enter number1: 6
Enter number2: 3
Enter number3: 5
Enter number4: 4
Enter number5: 3
Average = 4
10. C program to store temperature of two cities for a week and display it.
#include <stdio.h>
int main()
int temperature[CITY][WEEK];
scanf("%d", &temperature[i][j]);
return 0;
Output:
City 1, Day 1: 2
City 1, Day 2: 5
City 1, Day 3: 3
City 1, Day 4: 3
City 1, Day 5: 2
City 1, Day 6: 8
City 1, Day 7: 9
City 2, Day 1: 10
City 2, Day 2: 7
City 2, Day 3: 7
City 2, Day 4: 12
City 2, Day 5: 98
City 2, Day 6: 23
City 2, Day 7: 47
City 1, Day 1 = 2
City 1, Day 2 = 5
City 1, Day 3 = 3
City 1, Day 4 = 3
City 1, Day 5 = 2
Displaying values:
City 1, Day 1 = 2
City 1, Day 2 = 5
City 1, Day 3 = 3
City 1, Day 4 = 3
City 1, Day 5 = 2
City 1, Day 6 = 8
City 1, Day 7 = 9
City 2, Day 1 = 10
City 2, Day 2 = 7
City 2, Day 3 = 7
City 2, Day 4 = 12
City 2, Day 5 = 98
City 2, Day 6 = 23
City 2, Day 7 = 47
11. C program to find the sum of two matrices of order 2*2 using
multidimensional arrays.
#include <stdio.h>
int main()
int i, j;
scanf("%f", &a[i][j]);
scanf("%f", &b[i][j]);
printf("\nSum Of Matrix:");
printf("%.1f\t", c[i][j]);
if(j==1)
printf("\n");
return 0;
Output:
Enter a11: 2
Enter a22: 2
Enter b12:0
1.3 25.0
12. C Program to store values entered by the user in a three-dimensional
array and display it
#include <stdio.h>
int main()
int i, j, k, test[2][3][2];
scanf("%d", &test[i][j][k]);
printf("\nDisplaying values:\n");
}
}
return 0;
Output:
Enter 12 values:
5 6
45
67
Displaying values:
test[0][0][0] = 7
test[0][0][1] = 8
test[0][1][0] = 9
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 1
test[1][0][1] = 2
test[1][1][0] = 3
test[1][1][1] = 45
test[1][2][0] = 67
test[1][2][1] = 7
13.Write a c program to find maximum in arr[] of size n
#include <stdio.h>
int i;
max = arr[i];
return max;
int main()
int n = sizeof(arr)/sizeof(arr[0]);
return 0;
Output:
#include <stdio.h>
#define N 4
int i, j, k;
res[i][j] = 0;
res[i][j] += mat1[i][k]*mat2[k][j];
int main()
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
int mat2[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
int res[N][N];
int i, j;
printf("\n");
return 0;
Output:
Result matrix is
10 10 10 10
20 20 20 20
30 30 30 30
40 40 40 40
15.Write a c program getMissingNo takes array and size of array as
arguments
#include <stdio.h>
int i, total;
total = (n+1)*(n+2)/2;
total -= a[i];
return total;
int main()
printf("%d", miss);
return 0;
Output: