0% found this document useful (0 votes)
62 views91 pages

Unit II Programs

The documents provide examples of using conditional operators in C programming. Programs use the ternary operator ?: to return one value if a condition is true and another value if false. Examples check leap years, even/odd numbers, and return maximum values to demonstrate conditional operator usage. [SUMMARY

Uploaded by

THIRUNEELAKANDAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
62 views91 pages

Unit II Programs

The documents provide examples of using conditional operators in C programming. Programs use the ternary operator ?: to return one value if a condition is true and another value if false. Examples check leap years, even/odd numbers, and return maximum values to demonstrate conditional operator usage. [SUMMARY

Uploaded by

THIRUNEELAKANDAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 91

Relational Operators

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

If this year is leap year, enter 1. If not enter any integer: 2


Number of days in February = 28
Program 3
#include<stdio.h>
int main()
{
int a,b,result,choice;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
printf("Enter 1 for addition or 2 for multiplication:");
scanf("%d",&choice);
result=(choice==1)?a+b:(choice==2)?a*b:printf("Invalid Input");
if(choice==1||choice==2)
printf("The result is %d\n\n",result);
return 0;
}
Output
Enter first number:2
Enter second number:3
Enter 1 for addition or 2 for multiplication:1
The result is 5

Enter first number:2


Enter second number:3
Enter 1 for addition or 2 for multiplication:2
The result is 6
Program 4: Write a C program to find maximum between two numbers using conditional
operator.

#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

Enter three numbers:5


7
4
The biggest number is : 7
Program 6:Write a C program to check whether a number is even or odd using conditional
operator.

#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

Enter any year: 2007


COMMON YEAR

Enter any year: 2000


LEAP YEAR
Program 8: Write a C program to check whether character is an alphabet or not using
conditional operator.

#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

Enter any character:a


It is ALPHABET

Enter any character:3


It is NOT ALPHABET
1. Fibonacci Series up to n number of terms

#include <stdio.h>

int main()

int i, n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

for (i = 1; i <= n; ++i)

printf("%d, ", t1);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

return 0;

Output:

Enter the number of terms: 8


Fibonacci Series: 0 1 1 2 3 5 8 13
2.Print all numbers between 1 to 100 which divided by a specified number and
the remainder will be 3

#include <stdio.h>

int main() {

int x, i;

printf("Input an integer: ");

scanf("%d", &x);

for(i = 1; i <= 500; i++)

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;

printf("Enter the number of rows in pyramid of stars you wish to see\n");

scanf("%d", &n);

s = n;

for (row = 1; row <= n; row++)

for (c = 1; c < s; c++)

printf(" ");

s--;

for (c = 1; c <= 2*row - 1; c++)

printf("*");

printf("\n");

return 0;

Output:

Enter the number of rows in pyramid of stars you wish to see 5


*

***

*****

*******
4.Detect a number entered is palindrome or not.

#include <stdio.h>

int main()

int n, reversedInteger = 0, remainder, originalInteger;

printf("Enter an integer: ");

scanf("%d", &n); originalInteger = n;

while( n!=0 )

remainder = n%10;

reversedInteger = reversedInteger*10 + remainder;

n /= 10;

if (originalInteger == reversedInteger)

printf("%d is a palindrome.", originalInteger);

else

printf("%d is not a palindrome.", originalInteger);

return 0;

Output:

Enter an integer: 1022


1022 is not a palindrome.
5.Detect Armstrong numbers up to 1000.

#include <stdio.h>

#include <stdio.h>

Int main()

int number, temp, digit1, digit2, digit3;

printf("Print all Armstrong numbers between 1 and 1000:\n");

number = 001;

while (number <= 900)

digit1 = number - ((number / 10) * 10);

digit2 = (number / 10) - ((number / 100) * 10);

digit3 = (number / 100) - ((number / 1000) * 10);

temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 *


digit3);

if (temp == number)

printf("\n Armstrong no is:%d", temp);

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()

int n, reversedNumber = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &n);

while(n != 0)

remainder = n%10;

reversedNumber = reversedNumber*10 + remainder;

n /= 10;

printf("Reversed Number = %d", reversedNumber);

return 0;

Output:

Enter an integer: 2589


Reversed Number = 9852
6.Multiplication Table Up to 10

#include <stdio.h>

int main()

int n, i;

printf("Enter an integer: ");

scanf("%d",&n);

for(i=1; i<=10; ++i)

printf("%d * %d = %d \n", n, i, n*i);

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()

int i, Number, count;

printf(" Prime Number from 1 to 100 are: \n");

for(Number = 1; Number <= 100; Number++)

count = 0;

for (i = 2; i <= Number/2; i++)

if(Number%i == 0)

count++;

break;

if(count == 0 && Number != 1 )

printf(" %d ", Number);

return 0;
}

Output:

Prime Number from 1 to 100 are:


2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 8
9 97
8.Write a C program to print odd numbers between 1 to 100 using for loop.

#include <stdio.h>

int main() {

int counter;

printf("Odd numbers between 1 to 100\n");

for(counter = 1; counter <= 100; counter++) {

if(counter%2 == 1) {

printf("%d ", counter);

return 0;

Output:

Odd numbers between 1 to 100


1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 5
7 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
9.Program to print even and odd numbers from 1 to 10.

#include <stdio.h>

int main() {

int n,last;

printf("\n Enter Last Number : ");

scanf("%d",&last);

printf("\n Even Number List :\n ");

n=2;

while(n<=last)

printf(" %d",n);

n=n+2;

printf("\n\n Odd Number List :\n ");

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

Odd Number List : 1 3 5 7 9 11 13 15 17 19 21 23 25


10.Write a program to demonstrate the loop using GOTO statement.

#include <stdio.h>

int main() {

const int maxInput = 5;

int i;

double number, average, sum=0.0;

for(i=1; i<=maxInput; ++i)

printf("%d. Enter a number: ", i);

scanf("%lf",&number);

if(number < 0.0)

goto jump;

sum += number; // sum = sum+number;

jump:

average=sum/(i-1);

printf("Sum = %.2f\n", sum);

printf("Average = %.2f", average);

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;

printf("Enter an integer: ");

scanf("%lld", &n);

while(n != 0)

n /= 10;

++count;

printf("Number of digits: %d", 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;

printf("Enter number of rows: ");

scanf("%d",&rows);

for(i=1; i<=rows; ++i)

for(j=1; j<=i; ++j)

printf("* ");

printf("\n");

return 0;

Output:

Enter number of rows: 5

**

***

****
*****
13.Program to print half pyramid a using numbers

#include <stdio.h>

int main()

int i, j, rows;

printf("Enter number of rows: ");

scanf("%d",&rows);

for(i=1; i<=rows; ++i)

for(j=1; j<=i; ++j)

printf("%d ",j);

printf("\n");

return 0;

Output:

Enter number of rows: 5


1

12

123

1234
12345
Switch Case:

14.write a c program for choose a color.

#include <stdio.h>

int main()

int color = 1;

printf("Please choose a color(1: red,2: green,3: blue):\n");

scanf("%d", &color);

switch (color) {

case 1:

printf("you chose red color\n");

break;

case 2:

printf("you chose green color\n");

break;

case 3:

printf("you chose blue color\n");

break;

default:

printf("you did not choose any color\n");

return 0;
}

Output:

Please choose a color(1: red,2: green,3: blue): 2


you chose green color
15.Write a C Program for Switch case to Find weekdays name with weekday
number.

#include <stdio.h>

int main()

int day;

printf("Enter weekday number (1-7): ");

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:

printf("%d : Invalid Day Option",day);

return 0;

Output:

Enter weekday number (1-7): 2

2 - Monday
16.Write a c program continue statement inside for loop

#include <stdio.h>

int main()

for (int j=0; j<=8; j++)

if (j==4)

continue;

printf("%d ", j);

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;

printf("%d ", j);

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

enter the value of x:7


enter the value of y:5
x is greater than y
End of Program
Program 4:
#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
if(num1 > num2)
{
printf("%d is maximum", num1);
}
if(num2 > num1)
{
printf("%d is maximum", num2);
}
if(num1 == num2)
{
printf("Both are equal");
}
return 0;
}

Output:
Enter two numbers:6
5
6 is maximum

Enter two numbers:6


6
Both are equal
Program 5:
#include<stdio.h>
int main()
{
int a;
printf("Enter a number:");
scanf("%d",&a);
if(a>0)
{
printf( "The number %d is positive.",a);
}
Return 0;
}

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

Enter your age:17


You are not eligible for voting
program 4:
#include<stdio.h>
int main()
{
int a;
printf("Enter a number:");
scanf("%d",&a);
if(a>0)
{
printf( "The number %d is positive.",a);
}
else
{
printf("The number %d is negative.",a);
}
return 0;
}

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

Enter the first string:hai


Enter the second string:hello
Strings are different
EXAMPLE PROGRAM FOR NESTED IF STATEMENT IN C

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:

program to print the day of the week.


#include<stdio.h>
int main()
{
int day;
printf("Enter the number of the day:");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Invalid choice");
}
return 0;
}

Output:
Enter the number of the day:6
Friday

Enter the number of the day:7


Invalid choice
Program 2:
#include<stdio.h>
int main() {
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber+secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}

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

1.C program to find the sum marks of n students using arrays

#include <stdio.h>

int main()

int i,n;

int marks[n];

int sum=0;

printf("Enter number of students: ");

scanf("%d",&n);

for(i=0;i<n;i++){

printf("Enter marks of student%d: ",i+1);

scanf("%d",&marks[i]);

sum+=marks[i];

printf("Sum of marks = %d",sum);

return 0;

Output:

Enter number of students: 3


Enter marks of student1: 80
Enter marks of student2: 85
Enter marks of student3: 90
Sum of marks = 255
2.C program to pass a single element of an array to function

#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);

printf("Number of elemenets are: %d\n",n);

return 0;

Output:

Number of elemenets are: 5


4.Program to reverse array elements (by swapping first element to last, second
to second last and so on) in C

#include <stdio.h>

void Array_Swap(int *array , int n)

int i=0,temp=0;

for(i=0 ; i<n/2 ; i++)

temp = array[i];

array[i] = array[n-i-1];

array[n-i-1] = temp;

int main()

int array_1[30] = {0};

int i=0 ,n=0;

printf("\nEnter the number of elements for the array : ");

scanf("%d",&n);

printf("\nEnter the elements for array_1..\n");

for(i=0 ; i<n ; i++)

{
printf("array_1[%d] : ",i);

scanf("%d",&array_1[i]);

Array_Swap(array_1 , n);

printf("\nThe array after swap is..\n");

for(i=0 ; i<n ; i++)

printf("\narray_1[%d] : %d",i,array_1[i]);

return 0;

Output:

Enter the number of elements for the array : 5

Enter the elements for array_1..

array_1[0] : 2

array_1[1] : 3

array_1[2] : 5

array_1[3] : 8

array_1[4] : 6

The array after swap is..

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];

printf("\n\nRead n number of values in an array and display it in


reverse order:\n");

printf("Input the number of elements to store in the array :");

scanf("%d",&n);

printf("Input %d number of elements in the array :\n",n);

for(i=0;i<n;i++)

printf("element - %d : ",i);

scanf("%d",&a[i]);

printf("\nThe values store into the array are : \n");

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:

Read n number of values in an array and display it in reverse order:

Input the number of elements to store in the array :10

Input 10 number of elements in the array :

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

The values store into the array in reverse are : 10 9 8 7 6 5 4 3 2 1


6.Write a program in C to find the sum of all elements of an array.

#include <stdio.h>

int main()

int a[100];

int i, n, sum=0;

printf("\n\nFind sum of all elements of array:\n");

printf("Input the number of elements to be stored in the array :");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

for(i=0;i<n;i++)

printf("element - %d : ",i);

scanf("%d",&a[i]);

for(i=0; i<n; i++)

sum += a[i];

printf("Sum of all elements stored in the array is : %d\n\n", sum);

return 0;

}
Output:

Find sum of all elements of array: 5

Input the number of elements to be stored in the array :5

Input 5 elements in the array :


element -0 : 8

element - 1 : 9

element -2 : 4

element -3 : 5

element -4 : 6

Sum of all elements stored in the array is : 32


7.Write a program in C to store elements in an array and print it.

#include <stdio.h>

int main()

int arr[10];

int i;

printf("\n\nRead and Print elements of an array:\n");

printf("Input 10 elements in the array :\n");

for(i=0; i<10; i++)

printf("element - %d : ",i);

scanf("%d", &arr[i]);

printf("\nElements in array are: ");

for(i=0; i<10; i++)

printf("%d ", arr[i]);

printf("\n");

return 0;

OutPut:
Read and Print elements of an array:

Input 10 elements in the 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

Elements in array are: 4 2 6 8 7 1 3 2 4 5


8.Write a program in C to find the sum of all elements of the array

#include <stdio.h>

int main()

int a[100];

int i, n, sum=0;

printf("\n\nFind sum of all elements of array:\n");

printf("Input the number of elements to be stored in the array :");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

for(i=0;i<n;i++)

printf("element - %d : ",i);

scanf("%d",&a[i]);

for(i=0; i<n; i++)

sum += a[i];

printf("Sum of all elements stored in the array is : %d\n\n", sum);

return 0;
}

Output:

Find sum of all elements of array:

Input the number of elements to be stored in the array :5


Input 5 elements in the array :
element -0 : 5

element 1 : 4

element - 2 : 7

element -3 : 8

element -4 : 6

Sum of all elements stored in the array is : 30


9. Program to find the average of n (n < 10) numbers using arrays

#include <stdio.h>

int main()

int marks[10], i, n, sum = 0, average;

printf("Enter n: ");

scanf("%d", &n);

for(i=0; i<n; ++i)

printf("Enter number%d: ",i+1);

scanf("%d", &marks[i]);

sum += marks[i];

average = sum/n;

printf("Average = %d", average);

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>

const int CITY = 2;

const int WEEK = 7;

int main()

int temperature[CITY][WEEK];

for (int i = 0; i < CITY; ++i) {

for(int j = 0; j < WEEK; ++j) {

printf("City %d, Day %d: ", i+1, j+1);

scanf("%d", &temperature[i][j]);

printf("\nDisplaying values: \n\n");

for (int i = 0; i < CITY; ++i) {

for(int j = 0; j < WEEK; ++j)

printf("City %d, Day %d = %d\n", i+1, j+1, 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()

float a[2][2], b[2][2], c[2][2];

int i, j;

printf("Enter elements of 1st matrix\n");

for(i=0; i<2; ++i)

for(j=0; j<2; ++j)

printf("Enter a%d%d: ", i+1, j+1);

scanf("%f", &a[i][j]);

printf("Enter elements of 2nd matrix\n");

for(i=0; i<2; ++i)

for(j=0; j<2; ++j)

printf("Enter b%d%d: ", i+1, j+1);

scanf("%f", &b[i][j]);

for(i=0; i<2; ++i)

for(j=0; j<2; ++j)


{

c[i][j] = a[i][j] + b[i][j];

printf("\nSum Of Matrix:");

for(i=0; i<2; ++i)

for(j=0; j<2; ++j)

printf("%.1f\t", c[i][j]);

if(j==1)

printf("\n");

return 0;

Output:

Enter elements of 1st matrix

Enter a11: 2

Enter a12: 0.5

Enter a21: 1.1

Enter a22: 2

Enter elements of 2nd matrix

Enter b11: 0.2

Enter b12:0

Enter b21: 0.23


Enter b22: 23

Sum Of Matrix:2.2 0.5

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];

printf("Enter 12 values: \n");

for(i = 0; i < 2; ++i) {

for (j = 0; j < 3; ++j) {

for(k = 0; k < 2; ++k ) {

scanf("%d", &test[i][j][k]);

printf("\nDisplaying values:\n");

for(i = 0; i < 2; ++i) {

for (j = 0; j < 3; ++j) {

for(k = 0; k < 2; ++k ) {

printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);

}
}

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 largest(int arr[], int n)

int i;

int max = arr[0];

for (i = 1; i < n; i++)

if (arr[i] > max)

max = arr[i];

return max;

int main()

int arr[] = {10, 324, 45, 90, 9808};

int n = sizeof(arr)/sizeof(arr[0]);

printf("Largest in given array is %d", largest(arr, n));

return 0;

Output:

Largest in given array is 9808


14.Write a C program to multiply two square matrices

#include <stdio.h>

#define N 4

void multiply(int mat1[][N], int mat2[][N], int res[][N])

int i, j, k;

for (i = 0; i < N; i++)

for (j = 0; j < N; j++)

res[i][j] = 0;

for (k = 0; k < N; k++)

res[i][j] += mat1[i][k]*mat2[k][j];

int main()

int mat1[N][N] = { {1, 1, 1, 1},

{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;

multiply(mat1, mat2, res);

printf("Result matrix is \n");

for (i = 0; i < N; i++)

for (j = 0; j < N; j++)

printf("%d ", res[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 getMissingNo (int a[], int n)

int i, total;

total = (n+1)*(n+2)/2;

for ( i = 0; i< n; i++)

total -= a[i];

return total;

int main()

int a[] = {1,2,4,5,6};

int miss = getMissingNo(a,5);

printf("%d", miss);

return 0;

Output:

You might also like