Vous êtes sur la page 1sur 6

1.

C Program to Reverse String Without Using Library Function

#include<stdio.h> #include<string.h> void main() { char str[100],temp; int i,j=0; printf("nEnter the string :"); gets(str); i=0; j=strlen(str)-1; while(i<j) { temp=str[i]; str[i]=str[j]; str[j]=temp; i++; j--; } printf("nReverse string is :%s",str); return(0); }

2.

Armstrong number c program

Armstrong number c program: c programming code to check whether a number is armstrong or not. A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.
#include <stdio.h> int main() { int number, sum = 0, temp, remainder; printf("Enter an integer\n"); scanf("%d",&number); temp = number; while( temp != 0 ) { remainder = temp%10; sum = sum + remainder*remainder*remainder; temp = temp/10; } if ( number == sum ) printf("Entered number is an armstrong number.\n"); else printf("Entered number is not an armstrong number.\n"); return 0; }

3.

C program to print Pascal triangle

Pascal Triangle in c: C program to print Pascal triangle which you might have studied in Binomial Theorem in Mathematics. Number of rows of Pascal triangle to print is entered by the user. First four rows of Pascal triangle are shown below 1 1 1 1 2 1 1 3 3 1

#include <stdio.h> long factorial(int); int main() { int i, n, c; printf("Enter the number of rows you wish to see in pascal triangle\n"); scanf("%d",&n); for ( i = 0 ; i < n ; i++ ) { for ( c = 0 ; c <= ( n - i - 2 ) ; c++ ) printf(" "); for( c = 0 ; c <= i ; c++ ) printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c))); printf("\n"); } return 0; } long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); }

4. Source code to display Fibonacci series up to n terms


/* Displaying Fibonacci sequence up to nth term where n is entered by user. */ #include <stdio.h> int main() { int count, n, t1=0, t2=1, display=0; printf("Enter number of terms: "); scanf("%d",&n); printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */ count=2; /* count=2 because first two terms are already displayed. */ while (count<n) { display=t1+t2; t1=t2; t2=display; ++count; printf("%d+",display); } return 0; }

5. 1 22 333 4444 55555

#include <iostream> using namespace std; int main() { int upto, ndx, cdx; cout<<"number="; cin>>upto; for(ndx=1;ndx<=upto;++ndx) { for(cdx=1;cdx<=ndx;++cdx) cout<<ndx; cout<<" "; } cout<<endl; }

#include <iostream>

int main() { int n; cout << "Please enter a number"; cin >> n; for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { cout<<i; }

} }

/*Write a program to generate a Triangle. eg: 1 2 2 3 3 3 4 4 4 4 and so on as per user given number */

Vous aimerez peut-être aussi