C programs Example's - Passionate Geekz

Breaking

Where you can unleash your inner geek.

Sunday, 15 September 2019

C programs Example's

C program to print 0-100 numbers

#include <stdio.h>int main(){    int n,i;    printf("enter the count");    scanf("%d",&n);    for (i=0;i<=n;i++)    {            printf("%d",i);    }    }

Output

enter the count 20

0123456789111121314151617181920

C program to check even or odd

#include <stdio.h>int main(){    int n;    printf("enter number to  check even or odd");    scanf("%d",&n);    if(n%2==0)    {        printf("number is even");    }    else    {        printf("number is odd");    }    }

output

enter number to check even or odd 5

number is odd

C program to check prime or not

#include<stdio.h>int main(){int i,n,prime=0;printf("enter the number");scanf("%d",&n);for(i=2;i<n;i++){if(n%i==0){prime=1;break;}}if(prime==0){printf("Number is prime");}else{printf("Number is not prime");}return 0;}

Output

enter the number 43

number is prime

Write a program to calculate the Factorial.
Factorial
Example

#include<stdio.h>int main(){int i,n,f=1;printf("enter the number");scanf("%d",&n);for(i=1;i<=n;i++){f=f*i;}printf("Factorial of number is=%d",f);}

Output
enter the number 5
Factorial of number is=120
 
3. Write a program to check whether the given number is palindrome or not.

Example

#include<stdio.h>int main(){int r,n,p=0,temp;printf("enter the number");scanf("%d",&n);temp=n;while(n!=0){r=n%10;p=p*10+r;n=n/10;}if(p==temp){printf("Number is palindrome");}else{printf("Number is not palindrome");}return 0;}

Output
enter the number 111
number is palindrome
 
4. Write a program to check whether the number is Armstrong or not.
Armstrong Number
Example

#include<stdio.h>int main(){int n,r,p=0,temp;printf("enter the number ");scanf("%d",&n);temp=n;while(n>0){r=n%10;p=p+(r*r*r);n=n/10;}if(temp==p){printf("Number is Armstrong");}else{printf("Number is not Armstrong");}return 0;}

Output
enter the number 153
Number is Armstrong
 
5. Write a program to display the Fibonacci Series.
Fibonacci Series
Example

#include<stdio.h> int main() { int i=0,j=1,k,p,n; printf("Enter the number of elements:"); scanf("%d",&n); printf("\n%d %d",i,j); for(p=2;p<n;++p)//loop starts from 2 because 0 and 1 are already printed { k=i+j; printf(" %d",k); i=j; j=k; } return 0; } 

Output
Enter the number of elements:12
0 1 1 2 3 5 8 13 21 34 55 89

No comments:

Post a Comment