Professional Documents
Culture Documents
INDEX
PROGRAM -1
AIM: WAP to print “hello world”
SOURCE CODE:
#include<stdio.h>
int main()
{
printf("Hello World!!");
return 0;
}
OUTPUT:
22001001112
PROGRAM-2
AIM: Write a program to perform simple arithmetic operations in
C(Addition, Subtraction, Multiplication, Division, Modulus).
SOURCE CODE:
#include<stdio.h>
int main(){
int a,b;
printf("a=");
scanf("%d",&a);
printf("b=");
scanf("%d",&b);
printf("sum=%d\n",a+b);
printf("difference=%d\n",a-b);
printf("mutliply=%d\n",a*b);
printf("division=%d\n",a/b);
printf("remainder=%d",a%b);
return 0;
}
OUTPUT:
22001001112
PROGRAM-3
AIM: WAP to find area and perimeter of rectangle.
SOURCE CODE:
#include<stdio.h>
int main(){
float a,b,perimeter,area;
printf("Enter length of rectangle: ");
scanf("%f",&a);
printf("Enter breadth of rectangle: ");
scanf("%f",&b);
perimeter=(a+b)+(a+b);
area=a*b;
printf("Perimeter of rectangle is %0.4f
m\n",perimeter);
printf("Area of rectangle is %0.4f sq. m\n",area);
return 0;
}
OUTPUT:
22001001112
PROGRAM-4
AIM: WAP to find area and perimeter of circle.
SOURCE CODE:
#include<stdio.h>
int main()
{
float radius,area,PI,perimeter;
PI= 3.141;
printf("Enter radius of circle:");
scanf("%f", & radius);
area = PI * radius * radius;
perimeter= 2*PI*radius;
printf("Perimeter of circle = %f m\n",perimeter);
printf("Area of circle : %0.4f sq. m\n", area);
return 0;
}
OUTPUT:
22001001112
PROGRAM-5
AIM: WAP to convert given temperature in Fahrenite into Celsius.
SOURCE CODE:
#include<stdio.h>
int main(){
float F,C;
printf("Enter temperature in fahrenheit:");
scanf("%f",&F);
C=(F-32)*5/9;
printf("Temperature in Celcius is %f",C);
return 0 ;
}
OUTPUT:
22001001112
PROGRAM-6
AIM: WAP to compute where x = a/b -c
SOURCE CODE:
#include<stdio.h>
int main(){
float a, b, c, x;
printf("Enter the value of a: ");
scanf("%f", &a);
printf("Enter the value of b: ");
scanf("%f", &b);
printf("Enter the value of c: ");
scanf("%f", &c);
x = a / b - c;
printf("The value of x is: %.2f\n", x);
return 0;
}
OUTPUT:
22001001112
PROGRAM-7
AIM: WAP to calculate area of triangle when two sides of a
triangle are given
SOURCE CODE:
#include<stdio.h>
int main(){
float a, b, area;
printf("First side of a triangle is: ");
scanf(" %f", &a);
printf("Second side of a triangle is: ");
scanf(" %f", &b);
printf("area of a triangle ");
area = (a * b) / 2;
printf("%f", area);
return 0;
}
OUTPUT:
22001001112
PROGRAM-8
AIM: WAP to calculate area of triangle when three sides of a
triangle are given
SOURCE CODE:
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c,d,s;
s=(a+b+c)/2;
d= sqrt((s*(s-a)*(s-b)*(s-c)));
printf("The area of triangle is %0.2f sq.cm",d);
return 0;
}
OUTPUT:
22001001112
PROGRAM-9
AIM: WAP to find whether a given number is positive or not.
SOURCE CODE:
#include<stdio.h>
int main(){
float a;
printf("Enter a number:");
scanf("%f",&a);
if(a>=0){
printf("%f is a positive number",a);
}
else{
printf("%f is not a positive number",a);
}
return 0;
}
OUTPUT:
22001001112
PROGRAM-10
AIM: WAP to find greatest of three numbers
SOURCE CODE:
#include<stdio.h>
int main(){
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
OUTPUT:
22001001112
PROGRAM-11
AIM: WAP to calculate average marks of a student in five different
subjects and assign them grades
SOURCE CODE:
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c,d,e,average;
printf("Marks in subject 1:");
scanf("%f",&a);
printf("Marks in subject 2:");
scanf("%f",&b);
printf("Marks in subject 3:");
scanf("%f",&c);
printf("Marks in subject 4:");
scanf("%f",&d);
printf("Marks in subject 5:");
scanf("%f",&e);
average=(a+b+c+d+e)/5;
if(average>=90){
printf("The grade is A");
}
else if(average>=75){
printf("The grade is B");
}
else if(average>=60){
printf("The grade is C");
}
else if(average>=50){
printf("The grade is D");
}
else if(average<=50){
22001001112
OUTPUT:
22001001112
PROGRAM-12
AIM: WAP to design a simple calculate using switch -case
statements
SOURCE CODE:
#include<stdio.h>
#include<math.h>
int main(){
int num1,num2 ,A=0;
char operator;
// printf("Enter operator:");
// scanf("%c",&operator);
switch(operator){
case'+':
A=num1+num2;
printf("The sum is %d",A);
break;
case '-':
A=num1-num2;
printf("The difference is %d",A);
break;
case'*':
A=num1*num2;
printf("The multiplication is %d",A);
22001001112
break;
case'/':
A=num1/num2;
printf("The division is %d",A);
break;
case '%':
A =(num1%num2);
printf("%d",A);
break;
}
return 0;
}
OUTPUT:
22001001112
PROGRAM-13
AIM: WAP to print sum of all numbers between 1 to 500 which
are divisible by 11
SOURCE CODE:
#include<stdio.h>
int main(){
int num, sum = 0;
for(num = 1; num <= 500; num++)
{ if (num % 11 == 0){
sum += num; }}
printf("Sum of numbers between 1 to 500 divisible by
11: %d\n", sum);
return 0;
}
OUTPUT:
22001001112
PROGRAM-14
AIM: WAP to print the factorial of given number
SOURCE CODE:
#include <stdio.h>
int main() {
int n, i;
int fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
return 0;
}
OUTPUT:
22001001112
PROGRAM-15
AIM: WAP to print the sum of digits of a given number
SOURCE CODE:
#include<stdio.h>
int main(){
int a,sum=0,r;
while(a!=0){
r=a%10;
a=a/10;
sum=sum+r;
}
printf("The sum of digits is %d",sum);
return 0;
}
OUTPUT:
22001001112
PROGRAM-16
AIM: WAP to find whether the given number is Armstrong or Not
SOURCE CODE:
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
OUTPUT:
22001001112
PROGRAM-17
AIM: WAP to find whether the given number is Palindrome or Not
SOURCE CODE:
#include<stdio.h>
int main(){
int n, reverse =0,remainder,original;
printf("Enter number:");
scanf("%d",&n);
original=n;
while(n >0){
remainder=n%10;
reverse=reverse*10+remainder;
n=n/10;
}
if(original==reverse){
printf("%d is palindrome",original);}
else {
printf("%d is not palindrome",original);}
return 0;
}
OUTPUT:
22001001112
PROGRAM-18
AIM: WAP to print a series of numbers using do while loop.
SOURCE CODE:
#include<stdio.h>
int main() {
int start, end;
printf("Enter the starting number: ");
scanf("%d", &start);
printf("Enter the ending number: ");
scanf("%d", &end);
printf("Series of numbers from %d to %d: ", start,
end);
do{
printf("%d ", start); start++;}
while (start <= end); printf("\n");
return 0;
}
OUTPUT:
22001001112
PROGRAM-19
AIM: WAP to print the Fibonacci series
SOURCE CODE:
#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
OUTPUT:
22001001112
PROGRAM-20
AIM: WAP to find the sum of all numbers in 1-D array (size of 10).
SOURCE CODE:
#include<stdio.h>
int main(){
int arr[100], size, i, sum = 0;
OUTPUT:
22001001112
PROGRAM-21
AIM: WAP to count the number of elements present in a given
array
SOURCE CODE:
#include<stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
int i;
printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++) {
printf("Element %d: ", i+1);
scanf("%d", &arr[i]);}
int count = 0;
for (i = 0; i < size; i++){ count++;}
printf("Number of elements in the array: %d\n",
count);
return 0;
}
OUTPUT:
22001001112
PROGRAM-22
AIM: WAP to print largest and second largest number from an
array of size 10
SOURCE CODE:
#include <stdio.h>
int main() {
int a[10], n;
int largest1, largest2, i;
OUTPUT:
22001001112
PROGRAM-23
AIM: Write a program to print all elements from an array which
are divisible by any number given by users.
SOURCE CODE:
#include<stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size] ,i, num;
printf("Enter the elements of the array:\t");
PROGRAM-24
AIM: WAP to add two matrices in 2-D array
SOURCE CODE:
#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows and columns of the
matrices: ");
scanf("%d%d", &m, &n);
int a[m][n], b[m][n], c[m][n];
printf("Enter the elements of matrix A: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of matrix B: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
printf("The sum of the two matrices is: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
22001001112
}
return 0;
}
OUTPUT:
22001001112
PROGRAM-25
AIM: WAP to find transpose of a Matrix
SOURCE CODE:
#include<stdio.h>
int main()
{
int m,n;
printf("Enter the number of rows and column: \n");
scanf("%d %d",&m,&n);
int arr[10][10];
printf("\nEnter the elements of the matrix: \n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nThe elements in the matrix are: \n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
int brr[10][10];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
brr[j][i]=arr[i][j];
22001001112
}
}
printf("\nAfter transpose the elements are...\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d ",brr[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
22001001112
PROGRAM-26
AIM: WAP to create a function to swap two numbers using call by value
SOURCE CODE:
#include<stdio.h>
void swap(int,int);
int main(){
int x,y;
printf("Enter x: ");
scanf("%d",&x);
printf("Enter y: ");
scanf("%d",&y);
swap(x,y);
return 0;
}
void swap(int a,int b){
int temp;
temp=b;
b=a;
a=temp;
printf("After swapping \nx= %d\n y=%d",a,b);
}
OUTPUT:
22001001112
PROGRAM-27
AIM: WAP to find power of a number without math.h
SOURCE CODE:
#include<stdio.h>
int main(){
int base, exponent;
long value = 1;
printf("Enter a base value: ");
scanf("%d", &base);
printf("Enter an exponent value: ");
scanf("%d", &exponent);
while (exponent != 0){
value *= base;
--exponent;
}
printf("result = %ld", value);
return 0;
}
OUTPUT:
22001001112
PROGRAM-28
AIM: WAP to calculate factorial of a number using recursion
SOURCE CODE:
#include<stdio.h>
int factorial(int x){
if(x==0){
return 1;
}
return(x*factorial(x-1));
}
int main(){
int n;
printf("Enter a number: ");
scanf("%d",&n);
printf("Factorial of %d is %d",n,factorial(n));
return 0;
}
OUTPUT:
22001001112
PROGRAM-29
AIM: Write a program to find length of string, combine two strings, copy one string to
another string, compare two strings, reverse a given string (using string.h).
SOURCE CODE:
#include<stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], str3[100];
printf("Enter a string: ");
scanf("%[^\n]", str1);
// Find the length of the string
int length = strlen(str1);
printf("Length of the string: %d\n", length);
// Combine two strings
printf("Enter another string to combine with the
first string: ");
scanf(" %[^\n]", str2);
strcpy(str3, str1); strcat(str3, str2);
printf("Combined string: %s\n", str3);
// Copy one string to another
char str4[100];
strcpy(str4, str1);
printf("Copied string: %s\n", str4);
// Compare two strings
printf("Enter a string for comparison: ");
scanf(" %[^\n]", str2);
int compareResult = strcmp(str1, str2);
if (compareResult == 0) {
printf("The strings are equal.\n"); }
else if (compareResult < 0) {
printf("String 1 is lexicographically smaller than
string 2.\n"); }
22001001112
else {
printf("String 1 is lexicographically larger than
string 2.\n"); }
// Reverse a string
printf("Original string: %s\n", str1);
strrev(str1);
printf("Reversed string: %s\n", str1);
return 0;
}
OUTPUT:
22001001112
PROGRAM-30
AIM: WAP for user defined data type namely Book and implement it using
Structure
SOURCE CODE:
#include <stdio.h>
#include <string.h>
struct Book {
char title[100];
char author[100];
int publicationYear;
double price;
};
int main() {
struct Book book1;
printf("\nBook Details:\n");
printf("Title: %s\n", book1.title);
22001001112
return 0;
}
OUTPUT:
22001001112
PROGRAM-31
AIM: WAP to implement binary search
SOURCE CODE:
#include<stdio.h>
int binarySearch(int arr[], int low, int high, int key) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1; }
return -1; }
int main() {
int arr[] = {2, 4, 6, 8, 10, 12, 14, 16};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 14;
int index = binarySearch(arr, 0, n - 1, key);
if (index != -1)
printf("Element %d found at index %d.\n", key, index);
else
printf("Element %d not found in the array.\n", key);
return 0; }
OUTPUT:
22001001112
PROGRAM-32
AIM: WAP to implement bubble sort
SOURCE CODE:
#include<stdio.h>
void bubbleSort(int arr[], int size) {
int i, j, temp;
for (i = 0; i < size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp; } } } }
int main() {
int arr[100], size, i;
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter the elements:\n");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]); }
bubbleSort(arr, size);
printf("Sorted array in ascending order: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]); }
printf("\n");
return 0; }
OUTPUT:
22001001112
PROGRAM-33
AIM: WAP to implement insertion sort
SOURCE CODE:
#include<stdio.h>
void insertionSort(int arr[], int size) {
int i, j, key;
for (i = 1; i < size; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1; }
arr[j + 1] = key; } }
int main() {
int arr[100], size, i;
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter the elements:\n");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]); }
insertionSort(arr, size);
printf("Sorted array in ascending order: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]); }
printf("\n");
return 0;
}
22001001112
OUTPUT:
22001001112
PROGRAM-34
AIM: WAP to implement call by reference for swapping of two
numbers.
SOURCE CODE:
#include<stdio.h>
void swap(int * num1, int * num2);
int main()
{int num1, num2;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
swap(&num1, &num2);
return 0;
}
void swap(int * num1, int * num2)
{
int temp;
temp = *num1;
*num1= *num2;
*num2= temp;
printf("After swapping\n");
printf("Value of num1 = %d \n", *num1);
printf("Value of num2 = %d \n", *num2);
}
OUTPUT:
22001001112