Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

ProgramminginC(1)

ProgramminginC(1)

Uploaded by

hivowox550
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)
12 views

ProgramminginC(1)

ProgramminginC(1)

Uploaded by

hivowox550
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
You are on page 1/ 9

Subject: Programming in C Language (LAB WORK)

1. Write a C program to print your 2. Write a C program to find the sum


address? and average of 3 numbers?

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
clrscr(); int a,b,c, sum;
printf(“ADDRESS\n”); float avg;
printf(“Anu\n”); clrscr();
printf(“Al-Azhar College of Arts and Science\n”); printf("Please enter 3 numbers");
printf(“Thodupuzha\n”); scanf("%d%d%d",&a,&b,&c);
getch(); sum=a+b+c;
} avg=(a+b+c)/3;
printf("\nSum is %d", sum);
printf("\nAverage is %f",avg);
getch();
}

Output Output
ADDRESS Please Enter 3 numbers
Anu 5 8 2
Al-Azhar College of Arts and Science Sum is 16
Thodupuzha Average is 5

3. Write a C program to calculate Simple 4. Write a C program to calculate the


Interest? square and square root of a number?

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<math.h>
void main() #include<conio.h>
{ void main()
float principle, rate, time, simple_interest; {
clrscr() int n,sqr=0,sqroot=0;
printf("Enter the principle :"); clrscr();
scanf("%f", &principle); printf("\nEnter The Number\n");
printf("Enter the rate :"); scanf("%d",&n);
scanf("%f", &rate); sqr=n*n;
printf("Enter the time :"); sqroot=sqrt(n);
scanf("%f", &time); printf("\n SQUARE OF %d is %d .",n,sqr); printf("\n SQU
simple_interest = principle * rate * time / 100; getch();
printf("Simple interest is %0.2f", simple_interest); }
getch()
}
Output Output
Enter the principle :5400 Enter The Number
Enter the rate :8 25
Enter the time :3 SQUARE OF 25 is 625.
Simple interest is 1296.00 SQUARE-ROOT OF 25 IS 5.

1 SB, Dept. of Computer Science


Subject: Programming in C Language (LAB WORK)

5. Write a C program to find the smallest 6. Write a C program to check whether


of 2 numbers? the given number is even or odd?

#include<stdio.h> #include <stdio.h>


#include<conio.h> #include <conio.h>
void main()
{ void main()
int a,b; {
clrscr(); int number;
clrscr();
printf("\n Enter First Number : "); printf("Enter an integer: ");
scanf("%d",&a); scanf("%d", &number);

printf("\n Enter Second Number : "); // True if the number is perfectly divisible by 2
scanf("%d",&b);
if(number % 2 == 0)
if(a<b) //logical test printf("%d is even.", number);
{ else
printf("\n %d is Smaller",a); printf("%d is odd.", number);
}
else getch();
{ }
printf("\n %d is Smaller",b);
}
getch();
}

Output Output
Enter First Number : 6 Enter an integer
Enter Second Number : 3 5
3 is Smaller 5 is odd.

7. Write a C program to find the largest among 3 numbers?

#include<stdio.h> else
#include<conio.h> printf(" %d is larger",c);
void main()
{ getch();
int a,b,c; }
clrscr();
printf("Enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b) Output
if(a>c) Enter three numbers
printf(" %d is larger",a); 10 3 18
else 18 is larger.
printf(" %d is larger",c);
else
if(b>c)
printf(" %d is larger",b);

2 SB, Dept. of Computer Science


Subject: Programming in C Language (LAB WORK)

8. Write a C program to check 9. Write a C program to check whether the


whether the given number is given number is palindrome or not?
positive or negative?

#include <stdio.h> #include <stdio.h>


#include <conio.h> #include <conio.h>
void main()
{ void main() {
int num; int n, n1, rev = 0, rem;
clrscr(); clrscr();

/* Input number from user */ printf("Enter any number:\n ");


scanf("%d", &n);
printf("Enter any number: \n"); n1 = n;
scanf("%d", &num);
while (n > 0)
if(num > 0) {
{ rem = n % 10;
printf("Number is POSITIVE"); rev = rev * 10 + rem;
} n = n / 10;
if(num < 0) }
{
printf("Number is NEGATIVE"); if (n1 == rev){
} printf("Given number is a palindrome number");
if(num == 0) }
{ else{
printf("Number is ZERO"); printf("Given number is not a palindrome number");
} }
getch(); getch();
} }

Output Output
Enter any number: Enter any number:
14 121
Number is POSITIVE Given number is a palindrome number

10. Write a C program to check whether the given number is Armstrong or not?

#include<stdio.h> if(sum==temp)
int main(){ printf("%d is an Armstrong number",temp);
int num,r,sum=0,temp; else
printf("Enter a number: "); printf("%d is not an Armstrong number",temp);
scanf("%d",&num); return 0;
temp=num; }
while(num!=0)
{
r=num%10; Output
num=num/10; Enter a number: 153
sum=sum+(r*r*r); 153 is an Armstrong number
}

3 SB, Dept. of Computer Science


Subject: Programming in C Language (LAB WORK)

11. Write a C program to check whether 12. Write a C program to find the sum of
the given number is prime or not? digits of a number.

#include <stdio.h> #include <stdio.h>


int main()
{ int main()
int n, i, flag = 0; {
int num, sum=0;
printf("Enter a positive integer: ");
scanf("%d",&n); /* Input a number from user */

for(i=2; i<=n/2; ++i) printf("Enter any number to find sum of its


{ digit:\n");
// condition for nonprime number scanf("%d", &num);
if(n%i==0)
{ /* Repeat till num becomes 0 */
flag=1;
break; while(num!=0)
} {
} /* Find last digit of num and add to sum */
sum += num % 10;
if (flag==0)
printf("%d is a prime number.",n); /* Remove last digit from num */
else num = num / 10;
printf("%d is not a prime number.",n); }

return 0; printf("Sum of digits = %d", sum);


}
return 0;
}

Output Output
Enter a positive integer: 13 Enter any number to find sum of its digit:
13 is a prime number. 123
Sum of digits = 6

13. Write a C program to find the reverse of a number?

#include <stdio.h> printf("Reverse of entered number is = %d\n",


int main() Reverse);
{ return 0;
int Number, Reminder, Reverse = 0; }
printf("\nPlease Enter any number to
Reverse\n");
scanf("%d", & Number); Output
while (Number > 0) Please Enter any number to Reverse
{ 4567
Reminder = Number %10; Reverse of entered number is 7654
Reverse = Reverse *10+ Reminder;
Number = Number /10;
}

4 SB, Dept. of Computer Science


Subject: Programming in C Language (LAB WORK)

14. Write a C program to generate 15. Write a C program to find the factorial
Fibonacci series upto the limit. of a number using function.

#include <stdio.h> #include <stdio.h>


#include <conio.h>
void main() int main()
{ {
int a, b, c, i, n; int num;
clrscr();
printf("Enter an integer number :");
printf("Enter the limit: "); scanf("%d",&num);
scanf("%d", &n); printf("Factorial of %d is =
%ld",num,factorial(num));
a = 0;
b = 1; return 0;
c = 0; }
printf("Fibonacci series up to the given limit
are:\n");
long int factorial(int n)
for(i=1; i<=n; i++) {
{ int i;
printf("%d, ", c); long int fact=1;

a = b; if(n==1) return fact;


b = c;
c = a + b; for(i=n;i>=1;i--)
} fact= fact * i;
getch();
} return fact;
}

Output Output
Enter the limit: 10 Enter an integer number: 7
Fibonacci series up to the given limit are: Factorial of 7 is = 5040
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

5 SB, Dept. of Computer Science


Subject: Programming in C Language (LAB WORK)

16. Write a C program to add two numbers using function.

a)Function with arguments and return value. b) Function with arguments and without return
value.
#include <stdio.h> #include <stdio.h>
int add(int a,int b); void add(int a,int b);
void main() void main()
{ {
int num1,num2,sum=0; int num1,num2;

printf("Enter two numbers\n"); printf("Enter two numbers\n");


scanf("%d%d",&num1,&num2); scanf("%d%d",&num1,&num2);
sum=add(num1,num2); add(num1,num2);
printf("Sum =%d",sum);
}
} void add(int a,int b)
int add(int a,int b) {
{ int result;
int result; result=a+b;
result=a+b; printf("Sum =%d",result);
return result; }
}
Output Output
Enter two numbers Enter two numbers
15 40
21 50
Sum=36 Sum=90

c) Function without arguments and with return d) Function without arguments and without
value. return value.
#include <stdio.h> #include <stdio.h>
int add(); void add();
void main() void main()
{ {
int sum=0; add();
sum=add(); }
printf("Sum =%d",sum); void add()
} {
int add() int result,num1,num2;
{ printf("Enter two numbers\n");
int result,num1,num2; scanf("%d%d",&num1,&num2);
printf("Enter two numbers\n"); result=num1+num2;
scanf("%d%d",&num1,&num2); printf("Sum =%d",result);
result=num1+num2; }
return result;
}
Output Output
Enter two numbers Enter two numbers
30 20
20 25
Sum = 50 Sum = 45

6 SB, Dept. of Computer Science


Subject: Programming in C Language (LAB WORK)

17. Write a C program to search an 18. Write a C program to perform sorting


element in an array. operation both ascending and descending
order.

#include<stdio.h> #include <stdio.h>


int main(void)
int main() { {
int a[30], ele, num, i; int a[10], i=0, j=0, n, t;
printf ("\n Enter the no. of elements: ");
printf("\nEnter no of elements :"); scanf ("%d", &n);
scanf("%d", &num); printf ("\n");

printf("\nEnter the values :"); for (i = 0; i <n; i++)


for (i = 0; i < num; i++) { {
scanf("%d", &a[i]); printf ("\n Enter the %dth element: ", (i+1));
} scanf ("%d", &a[i]);
}
//Read the element to be searched for (j=0 ; j<(n-1) ; j++)
{
printf("\nEnter the elements to be searched :"); for (i=0 ; i<(n-1) ; i++)
scanf("%d", &ele); {
if (a[i+1] < a[i])
//Search starts from the zeroth location {
i = 0; t = a[i];
while (i < num && ele != a[i]) { a[i] = a[i + 1];
i++; a[i + 1] = t;
} }
}
//If i < num then Match found }
if (i < num) { printf ("\n Ascending order: ");
printf("Number found at the location = %d", i for (i=0 ; i<n ; i++)
+ 1); {
} else { printf (" %d", a[i]);
printf("Number not found"); }
}
printf ("\n Descending order: ");
return (0); for (i=n ; i>0 ; i--)
} {
printf (" %d", a[i-1]);
}

/* indicate successful completion */


return 0;
}
Output Output
Enter no of elements : 5 Enter the no. of elements: 5
11 22 33 44 55
Enter the elements to be searched : 44 Enter the 1th element: 25
Number found at the location = 4 Enter the 2th element: 50
Enter the 3th element: 75
Enter the 4th element: 35
Enter the 5th element: 100

Ascending order: 25 35 50 75 100


Descending order: 100 75 50 35 25

7 SB, Dept. of Computer Science


Subject: Programming in C Language (LAB WORK)

19. Write a C program to perform matrix addition.

printf("\n");
#include<stdio.h> }
int main() { return (0);
int i, j, mat1[10][10], mat2[10][10], mat3[10][10]; }
int row1, col1, row2, col2;

printf("\nEnter the number of Rows of Mat1 : "); Output


scanf("%d", &row1); Enter the number of Rows of Mat1 : 3
printf("\nEnter the number of Cols of Mat1 : "); Enter the number of Columns of Mat1 : 3
scanf("%d", &col1);
Enter the number of Rows of Mat2 : 3
printf("\nEnter the number of Rows of Mat2 : "); Enter the number of Columns of Mat2 : 3
scanf("%d", &row2);
printf("\nEnter the number of Columns of Mat2 : "); Enter the Element a[0][0] : 1
scanf("%d", &col2); Enter the Element a[0][1] : 2
Enter the Element a[0][2] : 3
/* Before accepting the Elements Check if no of Enter the Element a[1][0] : 2
rows and columns of both matrices is equal */ Enter the Element a[1][1] : 1
Enter the Element a[1][2] : 1
if (row1 != row2 || col1 != col2) { Enter the Element a[2][0] : 1
printf("\nOrder of two matrices is not same "); Enter the Element a[2][1] : 2
exit(0); Enter the Element a[2][2] : 1
}
Enter the Element b[0][0] : 1
//Accept the Elements in Matrix 1 Enter the Element b[0][1] : 2
Enter the Element b[0][2] : 3
for (i = 0; i < row1; i++) { Enter the Element b[1][0] : 2
for (j = 0; j < col1; j++) { Enter the Element b[1][1] : 1
printf("Enter the Element a[%d][%d] : ", i, j); Enter the Element b[1][2] : 1
scanf("%d", &mat1[i][j]); Enter the Element b[2][0] : 1
} Enter the Element b[2][1] : 2
} Enter the Element b[2][2] : 1

//Accept the Elements in Matrix 2 The Addition of two Matrices is :


for (i = 0; i < row2; i++) 246
for (j = 0; j < col2; j++) { 422
printf("Enter the Element b[%d][%d] : ", i, j); 242
scanf("%d", &mat2[i][j]);
}

//Addition of two matrices


for (i = 0; i < row1; i++)
for (j = 0; j < col1; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
//Print out the Resultant Matrix
printf("\nThe Addition of two Matrices is : \n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("%d\t", mat3[i][j]);
}

8 SB, Dept. of Computer Science


Subject: Programming in C Language (LAB WORK)

20. Write a C program to perform string manipulations.

#include<stdio.h> Output
#include<stdlib.h> 1. Find Length
#include<string.h> 2. Concatenate
int main(){ 3. Compare
char string1[20], string2[20]; //string variables 4. Copy
declaration with size 20 5. Exit
int choice;
while(1){ Enter your choice: 1
printf("\n1. Find Length \n2. Concatenate \n3.
Compare \n4. Copy \n5. Exit\n"); Enter the string: alazhar
printf("Enter your choice: "); The length of string is 7
scanf("%d",&choice);
switch(choice){ 1. Find Length
case 1: 2. Concatenate
printf("Enter the string: "); 3. Compare
scanf("%s",string1); 4. Copy
printf("The length of string is %d", 5. Exit
strlen(string1)); Enter your choice: 2
break;
case 2: Enter two strings: alazhar
printf("Enter two strings: "); college
scanf("%s%s",string1,string2);
strcat(string1,string2); The concatenated string is alazharcollege
printf("The concatenated string is %s", string1);
break; 1. Find Length
case 3: 2. Concatenate
printf("Enter two strings: "); 3. Compare
scanf("%s%s",string1,string2); 4. Copy
if(strcmp(string1,string2)==0){ 5. Exit
printf("They are equal"); Enter your choice: 3
}else{
printf("They are not equal"); Enter two strings: thodupuzha
} kerala
break; They are not equal
case 4: 1. Find Length
printf("Enter a string: "); 2. Concatenate
scanf("%s",string1); 3. Compare
printf("String1 = %s\n",string1); 4. Copy
printf("After copying string1 to string 2\n"); 5. Exit
strcpy(string2,string1); Enter your choice: 4
printf("String2 = %s",string2); Enter a string: alazhar
break; String1 = alazhar
case 5: After copying string1 to string 2
exit(0); String2 = alazhar
} 1. Find Length
} 2. Concatenate
return 0; 3. Compare
} 4. Copy
5. Exit
Enter your choice: 5

9 SB, Dept. of Computer Science

You might also like