Ticker

6/recent/ticker-posts

C Programming

Problem 1 swapping two number without function

#include<stdio.h>

int main()

{

int x,y,z;

scanf("%d%d",&x,&y);//x=2,y=5

x=x+y;//7

y=x-y;//7-5=2

x=x-y;//7-2=5

printf("%d %d",x,y);

return 0;

}

Problem 2 : Average of three number without function

#include<stdio.h>

int main()

{

int a,b,c;

float d;

scanf("%d%d%d",&a,&b,&c);

printf("%d %d %d ", a,b,c);

d=(a+b+c)/3.00;// explicit type casting

printf("%.2f", d);

return 0;

}

 Problem 3 : Armstrong number 
//1^3+5^3+3^3=153
#include<stdio.h>
#include<math.h>
int main()
{
int n, rem, sum=0, b;
scanf("%d",&n);
b=n;
while(n>0)
{
rem=n%10;
n=n/10;
sum=sum+pow(rem,3);
}
if(sum==b)
{
printf("Armstrong number");
}
else
{
printf("Not Armstrong Number");
}
return 0;
}

Problem 4 : Basic four mathematical operation with the help of switch case

#include<stdio.h>
int main()
{
float a,b,c;
char ch;
printf("enter the 1st no :");
scanf("%f", &a);
fflush(stdin);//clear the input buffer
printf("enter user device to perform the operation: ");
scanf("%c",&ch);
printf("enter the 2nd no :");
scanf("%f",&b);
switch(ch)
{
case'+': c=a+b;
printf("output=%f",c);
break;
case '-':c=a-b;
printf("output=%f",c);
break;
case '*':c=a*b;
printf("output=%f",c);
break;
case '/':c=a/b;
printf("output=%f",c);
break;
}
return 0;
}

Problem 5 : Check a number is prime number or not 

#include<stdio.h>
void prime(int);
int main()
{
int x;
scanf("%d",&x);
prime(x);
return 0;
}
void prime(int a)
{
int i, c=0;
for(i=1;i<=a/2;i++)
{
if(a%i==0)
{
c++;
}
}
if(c==1)
   printf("Prime");
else
printf("Not prime");
}

Problem 6 : Find Factorial of a number 

#include<stdio.h>
void fact(int);
int main()
{
int x;
scanf("%d",&x);
if(x>0)
{
fact(x);
}
else
{
printf("Enter postive number");
}
return 0;
}

void fact (int a)
{
int f=1, i;
for(i=1;i<=a;i++)
{
f=f*i;
}
printf("The factorial of %d is %d ", a,f);
}

Post a Comment

0 Comments