You are on page 1of 13

Fibonacci series should be generated

int main()

{ unsigned int i=0,j=0,sum=1,num;

printf("nEnter the limit for the series ");

scanf("%d",&num);

while(sum<num) {

printf("%d ",sum);

i=j;

j=sum;

sum=i+j; }}

int main()

unsigned int i=0 j=0 sum=1 num;

printf("nEnter the limit for the series ");

scanf(" d" &num);

while(sum<num){

printf(" d " sum);

i=j;

j=sum;

sum=i+j;}}

-----------------

using namespace std;

void main()

int limit sum=0 i=1 j;cout<<"please enter the limit of the series ";

cin>>limit;

while(sum<=limit){

cout<<sum<<endl;
j=sum;

sum+=i;

i=j; } }

------------------------------------------------

void main(){

int a b c;

c=a+b;

a=b;

b=c;

printf("n d" a);

getch();}

-------------------------------------------------------------

void main(){

int number;

printf("Enter number till which fibonacci series");

scanf(" d" &number);

for(int first=0 second=1 temp;number>=second;temp=second second=first+second first=temp)

printf("n d" second);

getch();}

---------------------------------------------------------------------

main(){

int a b c n;

a=0; (BEST CODE, I HAD TRIED IT AND RUN OK)


b=0;

c=1;

printf( enter the value of n );

scanf( d &n);

while(n>0){

printf( the fibanocci series is d c);

a=b;
b=c;

c=a+b;

n--;}}

------------------------------------------------------------------------

int main()

int n i f1=0 f2=1 f3=0;

printf("Enter number of terms : ");

scanf(" d" &n);

if(n==1)

printf("0");

if (n==2)

printf("0 1");

if(n>=3)

for(i=3;i<=n;i++)

f3=f1+f2;

f1=f2;

f2=f3;

printf(" d" f3);}}

return 0;

--------------------

#include<conio.h>

void main()

int i g h f;

clrscr();
printf("nEnter the two values from which series should be started");

scanf(" d d" &g &h);

printf("nnEnter the value upto which fibonacci series should be generated");

scanf(" d" &f);

i=g+h;

while(i<=f)

printf("n d" i);

g=h;

h=i;

i=g+h;

}}

------------------

int main()

int num first=0 second=0 third i;

clrscr();

for(i=0;i<=num;i++)

third=first+second;

first=second;

printf(" d d d" first second third);

getch();

return 0;}

--------------------------------------------------------------------------
/*C Program to arrange 7 numbers in ascending order*/
void main()

int i,j,temp,a[7];

printf("Enter 7 integer numbers: \n");

for(i=0;i<7;i++)

scanf("%d",&a[i]);

for (i=0;i<7;i++) {

for(j=i+1;j<7;j++) {

if(a[i]>a[j]) {

temp=a[j];

a[j]=a[i];

a[i]=temp;

}}}

printf("\n\nThe 7 numbers sorted in ascending order are: \n");

for(i=0;i<7;i++)

printf("%d\t",a[i]);

getch(); }

/*

Sample Output:

Enter 7 integer numbers:

6
3

The 7 numbers sorted in ascending order are:

2 3 4 6 7 8 9

------------------------------------------------------------------------------------

void revprint(int i){

while (i) {

printf ("%d", i%10);

i /= 10; }

putchar ('\n'); }

int revint(int i) {

int newnum = 0;

while (i) {

newnum = (newnum*10) + (i%10);

i /= 10; }

return newnum; }

int main(void) {

printf ("By revprint: 123 becomes ");

revprint(123);

printf ("By revint: %d becomes %d\n", 123, revint(123));

return 0;}

Output

By revprint: 123 becomes 321

----------------------------------------------------------------------------------

What is the c program to count the no of digits in the given number?


void main()
{
unsigned long int num;
int i=0;
clrscr();
printf("Enter the digit\n");
scanf("%lu",&num);
while(num!=0)
{
num=num/10;
++i;
}
printf("Length=%d",i); }

Palindrome programme
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0,m;
clrscr();
printf("enter any no");
scanf("%d",&n);
m=n;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(m==n)
printf("the no is palindrome");
else
printf("no is not palindrome");
getch();
}

output:
Enter How Many Times:10
1010101010

#include<stdio.h>
#include<conio.h>
void main()
{
int j,n,i=1;
clrscr();
printf("Enter How Many Times:");
scanf("%d",&n);
for(j=1;j<=n;j++)
{
printf("%d",i);
if(i==1)
i=0;
else
i=1;
}
getch();
}

output:
Enter How Many Times:10
1010101010

/* What is a C program to print the elements of an array in reverse order?

#include<stdio.h>
#include<conio.h>
void main()
{
int a[],n;
printf("enter array size");
scanf("%d",&n);
printf("enter elements of an array");
for(i=0;i<n;i++)
{
scanf("%d",a[i]);
}
for(i=1;i<=n;i++)
{
printf("%d",a[n-i]);
}
getch();
}

write a c programe which reads an integer value from the keyboard then it
displays the number in words?
#include<stdio.h>
#include<conio.h>
void main(void)
{
int rev,n,
clrscr();
printf("\n enter any number");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=temp%10;
rev+=rem*10;
temp=temp/10;
choice=rev%10;
switch(choice)
{
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
case 4:
printf("four");
break;
case 5:
print("five");
break;
case 6:
printf("six");
break;
case 7:
printf("seven");
break:
case 8:
printf("eight");
break:
case 9:
printf("nine");
}
getch();

http://www.scribd.com/doc/32570145/C-Interview-Programs-to-Print-Number-Patterns#

C Interview Programs to print number patterns


1. Write a program to display the multiplication table of a given number.
Program: Multiplication table of a given number
view source
print ?
01 #include <stdio.h>
02 int main() {
03
int num, i = 1;
04
printf("\n Enter any Number:");
05
scanf("%d", &num);
06
printf("Multiplication table of %d: \n", num);
07
while (i <= 10) {
08
printf("\n %d x %d = %d", num, i, num * i);
09
i++;
10
}
11
return 0;
12 }
Download Code
Output:
Enter any Number:5
5 x 1 = 5

5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Explanation: We need to multiply the given number (i.e. the number for which we want
the multiplication table) with value of ‘i’ which increments from 1 to 10.
Back to top
2. Write C program to print the following pattern:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Program:
view source
print ?
01 #include<stdio.h>
02 int main() {
03
int i, j, k, c = 5;
04
for (i = 1; i <= 5; i++) {
05
/* k is taken for spaces */
06
for (k = 1; k <= c; k++) {
07
/* blank space */
08
printf(" ");
09
}
10
for (j = 1; j <= i; j++) {
11
/* %2d ensures that the number is printed in
12 two spaces for alignment and the numbers are printed in the order. */
13
printf("%2d", i);
14
}
15
printf("\n");
16
/*c is decremented by 1 */
17
c--;
18
}
19
return 0;
20 }
Download Code
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Explanation: Here ‘i’ loop is used for printing the numbers in the respective rows and
‘k’ loop is used for providing spaces. ‘j’ loop prints the numbers. ‘c’ is decremented for
numbers to be displayed in alternate columns.
Back to top
3. Write C program to print the following pattern:
1

1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Program:
view source
print ?
01 #include<stdio.h>
02 int main() {
03
/* c taken for columns */
04
int i, j, c = 9, m, k;
05
for (i = 1; i <= 5; i++) {
06
/* k is used for spaces */
07
for (k = 1; k <= c; k++) {
08
printf(" ");
09
}
10
for (j = 1; j <= i; j++) {
printf("%2d",
j);
}
for (m = j - 2; m > 0; m--) {
11
/* %2d ensures that the number
12
* is printed in two spaces
13
* for alignment */
14
printf("%2d", m);
15
}
16
printf("\n");
17
/* c is decremented by 2 */
18
c = c - 2;
19
}
20
return 0;
21 }

Download Code
Output:
1

1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Explanation: Here ‘i’ loop is used for printing numbers in rows and ‘k’ loop is used for
providing spaces. ‘j’ loop is used for printing numbers in increasing order. ‘m’ loop is
used for printing numbers in reverse order.
Back to top
4. Write a C program to display the following format:
------
a b
------
1 5
2 4
3 3
4 2
5 1
------
Program:
view source
print ?
01 #include<stdio.h>
02 int main() {
03
int i = 1, j = 5;
04
printf("----------\n");
05
printf("a \t b \n");
06
printf("----------\n");
07
/* logic: while loop repeats
08
* 5 times i.e. until
09
* the condition i<=5 fails */
10
while (i <= 5) {
11
/* i and j value printed */
12
printf("%d \t %d\n", i, j);
13
/* i and j value incremented
14
by 1 for every iteration */

You might also like