1. Write a program to print the elements of an array using pointer.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int a[5]={2,7,1,9,8};
int *j;
for(i=0;i<5;i++)
{
j=&a[i];
printf("%d ",*j);
}
return 0;
}
OUTPUT:
2. Write a program to find the factorial of a given number using recursion.
CODE:
#include <stdio.h>
#include <stdlib.h>
int fact(int n)
{
int facto;
if(n==0)
facto=1;
else
facto=n*fact(n-1);
return facto;
}
int main()
{
int n;
printf("Enter the number you want to find factorial of\n");
scanf("%d",&n);
int p=fact(n);
printf("%d",p);
return 0;
}
OUTPUT:
3. Write a program to find the maximum number between two numbers.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
printf("Enter the numbers\n");
scanf("%d%d",&a,&b);
if(a>=b)
printf("%d",a);
else
printf("%d",b);
return 0;
}
INPUT: 5 9
OUTPUT: 9
4. Write a program to find the maximum and minimum element of an array.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter the size of array\n");
scanf("%d",&n);
int a[n];
printf("Enter the array elements\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int max=a[0],min=a[0];
for(int i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
printf("maximum element= %d\n minimum element= %d",max,min);
return 0;
}
INPUT:
5
8 3 4 1 9
OUTPUT:
maximum element= 9
minimum element= 1
5. Write a program to find the square of a given number in c.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter the number\n");
scanf("%d",&n);
int s=n*n;
printf("Square is %d",s);
return 0;
}
INPUT:
30
OUTPUT:
900
6. Write a program to swap two numbers using function in c.
CODE:
#include <stdio.h>
#include <stdlib.h>
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("After swapping\n a=%d \n b=%d",a,b);
}
int main()
{
int a,b;
printf("Enter the numbers\n");
scanf("%d%d",&a,&b);
swap(a,b);
return 0;
}
INPUT:
5
9
OUTPUT
9
5
7. Write a program to find the multiplication table of a number in c.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
printf("Enter the number\n");
scanf("%d",&a);
printf("The table is:\n");
for(int i=1;i<=10;i++)
{
int p=a*i;
printf("%d\n",p);
}
return 0;
}
INPUT:
5
OUTPUT
5
10
15
20
25
30
35
40
45
50
8. Write a program to check if a number is prime or not in c.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter the number\n");
scanf("%d",&n);
int flag=0;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(n==1)
printf("NO");
else if(n==2)
printf("YES");
else if(flag==0)
printf("YES");
else if(flag==1)
printf("NO");
return 0;
}
INPUT: 6
OUTPUT; NO
INPUT: 5
OUTPUT: YES
9. Write a program to print all the prime numbers less than a given number in c.
CODE:
#include <stdio.h>
#include <stdlib.h>
int ifprime(int n)
{
if(n==0 || n==1)
return 0;
else if(n==2)
return 1;
else
{
int flag=0;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
return 1;
else return 0;
}
}
int main()
{
int n;
printf("Enter the number\n");
scanf("%d",&n);
printf("prime numbers are\n");
for(int i=0;i<n;i++)
{
if(ifprime(i)==1)
printf("%d\n",i);
}
return 0;
}
INPUT:
50
OUTPUT:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
10. Write a program to print fibonacci series upto 10 in c.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=-1,b=1,c,i;
for(i=1;i<=10;i++)
{
c=a+b;
printf("%d ",c);
a=b;
b=c;
}
return 0;
}
OUTPUT:
11. Write a program to find whether a number is palindrome or not.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Enter the number\n");
int n;
scanf("%d",&n);
int rev=0,b=n;
while (n != 0)
{
int remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
if(b==rev)
printf("YES");
else
printf("NO");
return 0;
}
INPUT:
232
OUTPUT:
YES
0 Comments