0% found this document useful (0 votes)
50 views

C Programming

The document contains C program code snippets to perform various tasks like finding the largest of three integers, checking palindrome strings, determining if a number is prime or Armstrong, generating Pascal's triangle, performing matrix operations, and more. All programs take input, perform the relevant operation and print output.

Uploaded by

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

C Programming

The document contains C program code snippets to perform various tasks like finding the largest of three integers, checking palindrome strings, determining if a number is prime or Armstrong, generating Pascal's triangle, performing matrix operations, and more. All programs take input, perform the relevant operation and print output.

Uploaded by

rk751499
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.write a c program to find the largest of three integer.

#include<stdio.h>
int main()
{
int a,b,c;
printf("enter three integer:");
scanf("%d%d%d",&a,&b,&c);
if(a>=b && a>=c)
printf("%d is the largest integer",a);
else if(b>=a && b>=c)
printf("%d is the largest integer",b);
else
printf("%d is the largest integer",c);
return 0;
}
output:
enter three integer:21 24 23
24 is the largest integer

2. write a c progam to check whether the given string is palindrome or not.

#include<stdio.h>
#include<string.h>
int main()
{// palindrome string
char str[100];
int i,len;
printf("enetr string ");
gets(str);
len=strlen(str);
for(i=0;i<len/2;i++)
{ if(str[i]!=str[len-i-1])
{
printf(" not palindrome");
break;
}
}
if(i==len/2)
printf(" palindrome");
return 0;
}
output:
enter string rahul
not palindrome
enter string malayalam
palindrome

3.write a c program to find whether the given integer is


(a).a prime number
#include<stdio.h>
int main()
{
int n,i,count=0;
printf("enter any number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
printf("prime number");
else
printf("not prime number ");
return 0;
}
output:
enter any number:23
prime number
enter any number:22
not prime number

(b).an armstrong number.


#include<stdio.h>
int main()
{
// input 153= 1*1*1+5*5*5+3*3*3=153
int n,arm=0,r,c;
printf("enter any number:");
scanf("%d",&n);
c=n;
while(n>0)
{
r=n%10;
arm=(r*r*r)+arm;
n=n/10;
}
if(c==arm)
printf("armstrong number");
else
printf("not armstrong number");
return 0;
}
output:
enter any number:153
armstrong number
enter any number:123
not armstrong number

4. write c program for pascal triangle.


#include<stdio.h>
int main()
{
int i,j,k,n,value;
printf("enter the number of rows:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(k=n-1;k>=0;k--)
printf("");
value = 1;
for(j=0;j<=i;j++)
{
printf("%2d",value);
value=value*(i-j)/(j+1);
}
printf("\n");
}
return 0;
}
output:
enter the number of rows:5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

5.write a c program to find the sum and average of n integers using a linear array.

#include<stdio.h>
int main()
{
int i,n,sum=0,num;
float avg;

printf("\nEnter How many Number you want?\n");


scanf("%d",&n);

printf("\nEnter elements one by one\n");


for(i=0;i<n;++i)
{
scanf("%d",&num);
sum = sum +num;
}

avg = sum/(float)n;

printf("\nSum of %d Numbers = %d",n, sum);


printf("\nAverage of %d Numbers = %.2f",n, avg);

return 0;
}
output:
Enter How many Number you want?
4
Enter elements one by one
10 12 13 14
Sum of 4 Numbers = 49
Average of 4 Numbers = 12.25

6.write a c program to perform addition , multiplication , transpose on matrices.


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

// function to add two 3x3 matrix


void add(int m[3][3], int n[3][3], int sum[3][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
sum[i][j] = m[i][j] + n[i][j];
}
// function to multiply two 3x3 matrix
void multiply(int m[3][3], int n[3][3], int result[3][3])
{
for(int i=0; i < 3; i++)
{
for(int j=0; j < 3; j++)
{
result[i][j] = 0; // assign 0
// find product
for (int k = 0; k < 3; k++)
result[i][j] += m[i][k] * n[k][j];
}
}
}

// function to find transpose of a 3x3 matrix


void transpose(int matrix[3][3], int trans[3][3])
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
trans[i][j] = matrix[j][i];
}

// function to display 3x3 matrix


void display(int matrix[3][3])
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
printf("%d\t",matrix[i][j]);

printf("\n"); // new line


}
}

// main function
int main()
{
// matrix
int a[][3] = { {5,6,7}, {8,9,10}, {3,1,2} };
int b[][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
int c[3][3];

// print both matrix


printf("First Matrix:\n");
display(a);
printf("Second Matrix:\n");
display(b);

// variable to take choice


int choice;

// menu-driven
do
{
// menu to choose the operation
printf("\nChoose the matrix operation,\n");
printf("----------------------------\n");
printf("1. Addition\n");
printf("2. Multiplication\n");
printf("3. Transpose\n");
printf("4. Exit\n");
printf("----------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
add(a, b, c);
printf("Sum of matrix: \n");
display(c);
break;
case 2:
multiply(a, b, c);
printf("Multiplication of matrix: \n");
display(c);
break;
case 3:
printf("Transpose of the first matrix: \n");
transpose(a, c);
display(c);
printf("Transpose of the second matrix: \n");
transpose(b, c);
display(c);
break;
case 4:
printf("Thank You.\n");
exit(0);
default:
printf("Invalid input.\n");
printf("Please enter the correct input.\n");
}
}while(1);

return 0;
}
output:
First Matrix:
5 6 7
8 9 10
3 1 2
Second Matrix:
1 2 3
4 5 6
7 8 9

Choose the matrix operation,


----------------------------
1. Addition
2. Multiplication
3. Transpose
4. Exit
----------------------------
Enter your choice: 1
Sum of matrix:
6 8 10
12 14 16
10 9 11

Choose the matrix operation,


----------------------------
1. Addition
2. Multiplication
3. Transpose
4. Exit
----------------------------
Enter your choice: 2
Multiplication of matrix:
78 96 114
114 141 168
21 27 33

Choose the matrix operation,


----------------------------
1. Addition
2. Multiplication
3. Transpose
4. Exit
----------------------------
Enter your choice: 3
Transpose of the first matrix:
5 8 3
6 9 1
7 10 2
Transpose of the second matrix:
1 4 7
2 5 8
3 6 9

Choose the matrix operation,


----------------------------
1. Addition
2. Multiplication
3. Transpose
4. Exit
----------------------------
Enter your choice:

7.write a c program to find a fibonacci series of iterative methods using user -


defined function.
#include<stdio.h>
int main()
{ // 0 1 1 2 3 5 8 ....n
int n,a=0,b=1,c,i;
printf("eneter number of term...");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d ",a);
c=a+b;
a=b;
b=c;
}
return 0;
}
output:
eneter number of term...20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

8.write a c program to find factorial of n by recursion using user-defined


function.
#include<stdio.h>
int main()
{
int i,n,fact=1;
printf("factorial of nth term:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("factorial of nth term is %d=%d",n,fact);
return 0;

}
output:factorial of nth term:5
factorial of nth term is 5=120

9.write a c program to perform following operation by using user defined function .


(a).concatenation
#include<stdio.h>
#include<string.h>
int main()
{
int len1,len2,i;
char s1[30],s2[30];
printf("enter first name:");
gets(s1);
printf("enter last name:");
gets(s2);
len1=strlen(s1);
len2=strlen(s2);
for(i=0;i<=len2;i++)
{
s1[len1+i]=s2[i];
}
printf("string after concatenation %s",s1);
return 0;
}
output:
enter first name:rahul
enter last name:kumar
string after concatenation rahulkumar

(b).reverse
#include<stdio.h>
int main()
{
// input 234....>432
int n,r;
printf("enter number:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
printf("%d",r);
n=n/10;
}
return 0;
}
output:
enter number:123
321

(c).string matching
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
int value; // declaration of integer variable
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
// comparing both the strings using strcmp() function
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("strings are not same");
return 0;
}
output:
Enter the first string : rahul
Enter the second string : kumar
strings are not same

11.write a c program to interchange two values using


(a). call by value
#include<stdio.h>
void swap(int,int);
int main()
{
int a,b;
printf("enter two no:");
scanf("%d%d",&a,&b);
swap(a,b);
printf("main function a=%d b=%d",a,b);
return 0;
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("calling function x=%d y=%d ",x,y);
}
output:
enter two no: 1 2
calling function x=2 y=1 main function a=1 b=2

(b). call by reference


#include<stdio.h>
void swap(int *p,int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
printf("p=%d q=%d",*p,*q);

}
int main()
{
int a,b;
printf("enter two no:");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("a=%d b=%d",a,b);
return 0;
}
output:
enter two no:1 2
p=2 q=1a=2 b=1

You might also like