0% found this document useful (0 votes)
4 views6 pages

Basic Program

The document contains various C programming code snippets that demonstrate different algorithms, including checking if a number is even or odd, calculating factorials, checking leap years, summing digits, checking for palindromes, finding prime numbers, and converting temperatures. Each code snippet is structured with input prompts and outputs, showcasing the use of loops and conditional statements. The document serves as a collection of basic programming exercises for learning C language fundamentals.

Uploaded by

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

Basic Program

The document contains various C programming code snippets that demonstrate different algorithms, including checking if a number is even or odd, calculating factorials, checking leap years, summing digits, checking for palindromes, finding prime numbers, and converting temperatures. Each code snippet is structured with input prompts and outputs, showcasing the use of loops and conditional statements. The document serves as a collection of basic programming exercises for learning C language fundamentals.

Uploaded by

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

/* Check a number is even or odd */ /* Calculate factorial using for loop */

#include<stdio.h>
#include<stdio.h> int main()
int main() {
{ int a,f,i;
int a; printf("Enter a number: ");
printf("Enter a number: "); scanf("%d",&a);
scanf("%d",&a); f=1;
if(a%2==0) for(i=1;i<=a;i++)
printf("Even Number"); f = f * i;
else printf("Factorial: %d",f);
printf("Odd Number"); return 0;
return 0; }
}
/* Check Leap Year */
/* Calculate factorial using while loop */ #include<stdio.h>
#include<stdio.h> int main()
int main() {
{ int y;
int a,f,i; printf("Enter a year: ");
printf("Enter a number: "); scanf("%d",&y);
scanf("%d",&a); if(y%4==0)
f=1; printf("Leap Year");
i=1; else
while(i<=a) printf("Not a Leap Year");
{ return 0;
f = f * i; }
i++;
}
printf("Factorial: %d",f);
return 0;
}

--------------------------------------------------------------
/* calculate power using while loop */ /* Sum of digits using while loop */
#include<stdio.h> #include <stdio.h>
int main() int main()
{ {
int a, b, i, p; int n, s = 0;
printf("Enter value of a: ");
scanf("%d",&a); printf("Please enter a number: ");
printf("Enter value of b: "); scanf("%d",&n);
scanf("%d",&b);
p=1; while (n > 0)
i=1; {
while(i<=b) s = s + n%10;
{ n = n/10;
p = p * a; }
i++; printf("Sum of digits is: %d", s);
} return 0;
printf("Power : %d",p); }
return 0;
} ------------------------------------------------------

----------------------------------------------------- /* Sum of digits using for loop */


#include <stdio.h>
/* Calculate power using for loop */ int main()
#include<stdio.h> {
int main() int n, s;
{ printf("Please enter a number: ");
int a, b, i, p; scanf("%d",&n);
printf("Enter value of a: "); for(s=0;n>0;n=n/10)
scanf("%d",&a); {
printf("Enter value of b: "); s = s + n%10;
scanf("%d",&b); }
p=1; printf("Sum of digits is: %d", s);
for(i=1;i<=b;i++) return 0;
p = p * a; }
printf("Power : %d",p);
return 0;
}
/* Palindrome number using while loop */ /* Fibonacci Series using while loop */
#include <stdio.h> // 0 1 1 2 3 5 8 13...
int main() #include<stdio.h>
{ int main()
int n, r = 0,t; {
printf("Please enter a number: "); int n,i,a,b,c;
scanf("%d",&n); printf("Enter a number: ");
t=n; scanf("%d",&n);
while (n > 0) i=1;
{ a=0;
r = r * 10; b=1;
r = r + n%10; while(i<=n)
n = n/10; {
} printf("%d ",a);
if(t==r) c = a + b;
printf("Number is Palindrome"); a = b;
else b = c;
printf("Number is not Palindrome"); i++;
}
return 0; return 0;
} }
/* Palindrome number using for loop */ /* Fibonacci series using for loop */
#include <stdio.h> // 0 1 1 2 3 5 8 13...
int main() #include<stdio.h>
{ int main()
int n, r, t; {
printf("Please enter a number: "); int n,i,a,b,c;
scanf("%d",&n); printf("Enter a number: ");
t=n; scanf("%d",&n);
for(r=0;n>0;n=n/10) a=0;
{ b=1;
r = r * 10; for(i=1;i<=n;i++)
r = r + n%10; {
} printf("%d ",a);
if(t==r) c = a + b;
printf("Number is Palindrome"); a = b;
else b = c;
printf("Number is not Palindrome"); }
return 0; return 0;
} }
/* Prime Number using while loop */ /* Prime number using for loop */

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int a,i,f; int a,i,f;
printf("Enter a number: "); printf("Enter a number: ");
scanf("%d",&a); scanf("%d",&a);
f=0; f=0;
i=2; for(i=2;i <= a/2;i++)
while(i <= a/2) {
{ if(a%i == 0)
if(a%i == 0) {
{ f=1;
f=1; break;
break; }
} }
i++; if(f==0)
} printf("Prime Number")
if(f==0) else
printf("Prime Number") printf("Not Prime Number");
else return 0;
printf("Not Prime Number"); }
return 0;
}
/* Check Armstrong Number */ /* convert celcius to farenheit */

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int n, sum = 0, t, r; float c,f;
printf("Please enter a number: "); printf("Type celcius: ");
scanf("%d",&n); scanf("%f",&c);
f = c * 9/5 + 32;
for(t=n;t>0;t=t/10) printf("farenheit: %f",f);
{ return 0;
r = t%10; }
sum = sum + r*r*r;
} /* convert farenheit to celcius */
if ( n == sum ) #include<stdio.h>
printf("%d is an armstrong number.",n); int main()
else {
printf("%d is not an armstrong number.",n); float c,f;
return 0; printf("Type farenheit : ");
} scanf("%f",&f);
c = (f - 32) * 5/9;
/* Print Armstrong Numbers upto N */ printf("celcius: %f",c);
return 0;
#include<stdio.h> }
int main()
{ /* Find largest value among three variable using conditional operator */
int i,j,sum,n;
printf("Please enter the value of N: "); #include<stdio.h>
scanf("%d",&n); int main()
for(i=2;i<=500;i++) {
{ int a,b,c,d;
for(j=i,sum=n;j>=1;j=j/10)
sum=sum+(j%10)*(j%10)*(j%10); printf("Type values of A,B and C : ");
if(sum==i) scanf("%d %d %d",&a,&b,&c);
printf("%d is Armstrong.\n",i); d=(a>=b?a>=c?a:c:b>=c?b:c);
} printf("Greatest value : %d",d);
return 0; return 0;
} }
/* Find largest among 3 variables using nested if */ /* Find largest value between two variables - single line - Conditional
#include <stdio.h> Operator */
int main()
{ #include<stdio.h>
int a,b,c; int main()
printf("Enter 3 numbers: \n"); {
scanf("%d %d %d",&a,&b,&c); int a,b,d;
if(a>=b) printf("Type values of A and B : ");
{ scanf("%d %d",&a,&b);
if(a>=c) d=(a>=b?a:b);
printf("%d is largest",a); printf("Greatest value : %d",d);
else return 0;
printf("%d is largest",c); }
}
else if(b>=c) /* Print 1 to 10 using while loop */
printf("%d is largest",b); #include<stdio.h>
else int main()
printf("%d is largest",c); {
return 0; int i=1;
} while(i <= 10)
{
/* Largest value among three variables */ printf("%d\n",i);
#include<stdio.h> i++;
int main() }
{ return 0;
int a,b,c; }
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
printf("Enter value of c: ");
scanf("%d",&c);
if(a >= b && a >= c)
printf("%d is largest",a);
else if(b >= a && b >= c)
printf("%d is largest",b);
else if(c >= a && c >= b)
printf("%d is largest",c);
return 0;
}

You might also like