C Programs
C Programs
C Programs
}
printf("\n sum = %d",sum);
printf("\n reverse = %d",rev);
return 0;
}
Output
Enter a number:1234
sum = 10
reverse = 4321
2. First n Fibonacci numbers.
Method 1
#include <stdio.h>
int main()
scanf("%d",&num);
printf("Fibonacci series: ");
for(int i=0;i<num;i++)
c=a+b;
printf("%d, ",c);
a=b;
b=c;
return 0;
}
Output
Enter a number: 5
Fibonacci series: 0, 1, 1, 2, 3, 5,
Method 2
#include <stdio.h>
static int a = 0, b = 1, c;
if(num > 0)
c = a + b;
a = b;
b = c;
printf("%d, ",c);
fibonacci(num-1);
int main()
int num;
scanf("%d",&num);
printf("0, 1, ");
fibonacci(num-2);
return 0;
}
Output
Enter the number5
0, 1, 1, 2, 3,
3. Create a pyramid using ‘*’
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the 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 the number of rows: 5
*
**
***
****
*****
4. Find the number of words in a sentence
#include <stdio.h>
#include <string.h>
void main()
{
char str[200];
int y = 0, x;
{
flag=1;
break;
}
}
if(flag==0)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
return 0;
}
Output
enter a positive integer5
5 is a prime number
enter a positive integer6
6 is not a prime number
6. Perform matrix transpose
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
Output
Enter rows and columns: 2
2
printf("\n");
}
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Pattern:\n");
printPattern(n);
return 0;
}
Output
Enter a number: 1456
Pattern:
1456
456
56
6
9. Display the short form of a string. Eg Computer Science:CS
// Online C compiler to run C program online
#include <stdio.h>
#include<string.h>
int main() {
int i,j=0,m=0;
char name[50],abrname[50];
printf("Enter the string:");
gets(name);
for(i=0;i<strlen(name);i++)
{
abrname[m]=name[j];
if(name[i]==' ')
{
m=m+1;
j=i;
abrname[m]=name[j];
j++;
}
}
abrname[m+1]='\0';
printf("\n The short form is:\n");
puts(abrname);
return 0;
}
Output
Enter the string: computer science
153
370
371
407
12. find the factorial of a number using recursion
#include<stdio.h>
long fact(int);
void main()
{
int x;
printf("Enter a number:");
scanf("%d",&x);
printf("\n Factorial of %d is %d",x,fact(x));
}
long fact(int n)
{
if(n==0)
return 1;
else
return (n*fact(n-1));
}
Output
Enter a number:6
Factorial of 6 is 720
13. Check for the palindrome string
#include <stdio.h>
int main() {
char string[100];
int i, length, flag = 0;
if(flag == 0)
printf("%s is a palindrome string.\n", string);
else
printf("%s is not a palindrome string.\n", string);
return 0;
}
Output
Enter a string: malayalam
malayalam is a palindrome string.
Enter a string: hello
hello is not a palindrome string.
14. Check for leap year
#include<stdio.h>
void main()
{
int year;
printf("Enter a year:");
scanf("%d",&year);
if(year%4==0&&year%100!=0||year%400==0)
printf("\n%d is a leap year",year);
else
printf("\n %d is not a leap year",year);
}
Output
Enter a year:2023
2023 is not a leap year
15. Write odd and even numbers in separate text files.
#include<stdio.h>
void main()
{
FILE *f1,*f2,*f3;
int number,i,n;
printf("Contents of DATA file\n\n");
f1=fopen("DATA","w");
printf("\nEnter a limit:");
scanf("%d",&n);
printf("\nEnter the elements\n");
for(i=1;i<=n;i++)
{
scanf("%d",&number);
putw(number,f1);
}
fclose(f1);
f1=fopen("DATA","r");
f2=fopen("ODD","w");
f3=fopen("EVEN","w");
while((number=getw(f1))!=EOF)
{
if(number%2==0)
putw(number,f3);
else
putw(number,f2);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD","r");
f3=fopen("EVEN","r");
printf("\n\nContent of ODD File\n\n");
while((number=getw(f2))!=EOF)
printf("%d",number);
printf("\n\nContent of ODD File\n\n");
while((number=getw(f3))!=EOF)
printf("%d",number);
fclose(f2);
fclose(f3);
}
Output
Contents of DATA file
Enter the limit:3
Enter the elements:1 2 3
Content of ODD file:1 3
Content of Even file:2
16. Base conversion of numbers
#include<stdio.h>
void main()
{
int b,n,i,r,digit,p,count=0;
char a[100];
printf("\n Enter the decimal number:");
scanf("%d",&n);
printf("\n Enter the base to be converted:");
scanf("%d",&b);
p=n;
do
{
r=p%b;
digit='0'+r;
if(digit>'9')
digit=digit=digit+7;
a[count]=digit;
count++;
p=p/b;
}while(p!=0);
printf("\n Base %d equivalent of number %d is",b,n);
for(i=count-1;i>=0;--i)
printf("%c",a[i]);
printf("\n");
}
Output
Enter the decimal number:2
Enter the base to be converted:2
Base 2 equivalent of number 2 is10
17. Merge two numeric array in sorted order
#include <stdio.h>
int main()
{
int n1,n2,n3;
int a[100], b[100], c[200];
printf("Enter the size of first array: ");
scanf("%d",&n1);
printf("Enter the array elements: ");
for(int i = 0; i < n1; i++)
scanf("%d", &a[i]);
printf("Enter the size of second array: ");
scanf("%d",&n2);
printf("Enter the array elements: ");
for(int i = 0; i < n2; i++)
scanf("%d", &b[i]);
n3 = n1 + n2;
int i = 0, j = 0, k = 0;
return 0;
}
Output
Enter the rows and columns:
22
Enter the elements:1
5
96
7
The matrix:
0 1
-1 0
19. Find the average of prime numbers in a group of N numbers using function .
#include<stdio.h>
void main()
{
int isprime(int);
int i,n,num,sum=0,c=0;
float avg;
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the numbers:");
for(i=0;i<n;++i)
{
scanf("%d",&num);
if(isprime(num))
{
sum=sum+num;
c++;
}
}
avg=(float)sum/c;
printf("\n Average of prime numbers=%f",avg);
}
int isprime(int n)
{
int i;
if(n<=1)
return 0;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
Output
Enter the limit:5