C Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

C PROGRAMS

1. Find the sum and reverse of a given number.


#include <stdio.h>
int main() {
int num,d,sum,rev;
printf("Enter a number:");
scanf("%d",&num);
sum=rev=0;
while(num!=0)
{
d=num%10;
sum=sum+d;
rev=rev*10+d;
num=num/10;

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

int num, a=-1,b=1,c;

printf("Enter a number: ");

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>

int fibonacci(int num)

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;

printf("Enter the number");

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;

printf("Enter the string:\n");


scanf("%[^\n]str", str);
for (x = 0;str[x] != '\0';x++)
{
if (str[x] == ' ' && str[x+1] != ' ')
y++;
}
printf("Number of words in given string are: %d\n", y + 1);
}
Output
Enter the string:
hello world
Number of words in given string are: 2
5. Check whether a number is prime or not
#include<stdio.h>
int main()
{
int i,n,flag=0;
printf("enter a positive integer");
scanf("%d",&n);
if(n==0||n==1)
flag=1;
for(i=2;i<=n/2;++i)
{
if(n%i==0)

{
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

Enter matrix elements:


Enter element a11: 1
Enter element a12: 5
Enter element a21: 9
Enter element a22: 3
Entered matrix:
1 5
9 3
Transpose of the matrix:
1 9
5 3
7. Find the sum of series 1+(1/2)2+(1/3)2…………….. to 0.0001% accuracy.
#include <stdio.h>
#include<math.h>
int main() {
int n,i;
double sum=0.0,ser;
printf("enter n");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
ser=1/pow(i,i);
sum+=ser;
}
printf("Sum=%.5f",sum);
return 0;
}
Output
enter n:3
Sum=1.28704
8. Create a pattern with the number N
Eg: N=39174 Pattern: 3 9 1 7 4
91 7 4
17 4
7 4
4
#include <stdio.h>
void printPattern(int n) {
char str[20];
sprintf(str, "%d", n);
int len = strlen(str);
for (int i = 0; i < len; i++) {
for (int j = 0; j < i; j++) {
printf(" ");
}
for (int j = i; j < len; j++) {
printf("%c ", str[j]);
}

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

The short form is:


Cs
10. Find the currency denomination of the given currency.
#include <stdio.h>
int main() {
int notes[8]={500,100,50,20,10,5,2,1};
int amount,temp;
printf("Enter the amount:");
scanf("%d",&amount);
temp=amount;
for(int i=0;i<8;i++)
{
printf("\n%d notes is:%d",notes[i],temp/notes[i]);
temp=temp%notes[i];
}
return 0;
}
Output
Enter the amount:1253
500 notes is:2
100 notes is:2
50 notes is:1
20 notes is:0
10 notes is:0
5 notes is:0
2 notes is:1
1 notes is:1
11. Find the Armstrong numbers within a range
#include<stdio.h>
void main()
{
int num,x,sum,temp;
int stno,enno;
printf("Input starting number of range:");
scanf("%d",&stno);
printf("\nInput ending number of range:");
scanf("%d",&enno);
printf("\n Armstrong number in a given range are:");
for(num=stno;num<=enno;num++)
{
temp=num;
sum=0;
while(temp!=0)
{
x=temp%10;
temp=temp/10;
sum=sum+(x*x*x);
}
if(sum==num)
printf("\n%d\n",num);
}
printf("\n");
}
Output
Input starting number of range:1
Input ending number of range:1000
Armstrong number in a given range are:
1

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;

printf("Enter a string: ");


gets(string);

for(length = 0; string[length] != '\0'; length++);

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


if(string[i] != string[length-i-1]) {
flag = 1;
break;
}
}

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;

while (i < n1 && j < n2)


{
if (a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}

while (i < n1)


c[k++] = a[i++];

while (j < n2)


c[k++] = b[j++];

printf("Final array after merging: ");


for(int i = 0; i < n3 ; i++)
printf(" %d ",c[i]);
return 0;
}
Output
Enter the size of first array: 2
Enter the array elements: 1 2
Enter the size of second array: 2
Enter the array elements: 0 8
Final array after merging: 0 1 2 8
18. Fill upper triangle with 1,lower triangle with -1 and diagonal elements with 0.
#include<stdio.h>
int main()
{
int a[20][20],m,n,i,j;
printf("Enter the rows and columns:\n");
scanf("%d %d",&m,&n);
printf("Enter the elements:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(i<j)
{
a[i][j]=1;
}
else if(i>j)
{
a[i][j]=-1;
}
else
{
a[j][j]=0;
}
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}

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

Enter the numbers:2 5 4 6 3

Average of prime numbers=3.333333


20. Store and read data from a text file

You might also like