SET A
1. Write a program to accept an integer n and display all even numbers upto n.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("\nEnter Number:");
scanf("%d",&n);
printf("\neven numbers upto %d ",n);
for(i=0;i<n;i++)
{
if(i%2==0)
printf("\n%d",i);
}
getch();
}
/*
Enter Number:20
even numbers upto 20
0
2
4
6
8
10
12
14
16
18
2. Accept two integers x and y and calculate the sum of all integers between x and y (both
inclusive)
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,i,sum=0;
clrscr();
printf("\nEnter two numbers:");
scanf("%d%d",&x,&y);
for(i=x;i<=y;i++)
{
sum=sum+i;
}
printf("\nSum of digit between %d to %d : %d",x,y,sum);
getch();
}
OUTPUT:
Enter two numbers:
2
4
Sum of digit between 2 to 4 : 9
3. Write a program to accept two integers x and n and compute xn
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n,i,ans=1;
clrscr();
printf("\nEnter number and power:");
scanf("%d%d",&x,&n);
/*for(i=0;i<n;i++)
{
ans=ans*x;
}
printf("\n%d power %d is : %d ",x,n,ans);
*/
printf("\n%d power %d is:",x,n);
while(n>0)
{
ans=ans*x;
n--;
}
printf("%d",ans);
getch();
}
4. Write a program to accept an integer and check if it is prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,flag;
clrscr();
printf("\nEnter the number:");
scanf("%d",&n);
for(i=2;i<n;i++)
{
flag=1;
if(n%i==0)
{
flag=0;
break;
}
}
if(flag==1)
printf("\n%d is prime",n);
else
printf("\n%d is not prime",n);
getch();
}
5. Write a program to accept an integer and count the number of digits in the number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,cnt=0,sum=0,d;
clrscr();
printf("\nEnter Number:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
cnt++;
}
printf("\nCount is %d",cnt);
getch();
}
OUTPUT:
Enter Number:
1234
Count is 4
7. Write a program to accept a character, an integer n and display the next n characters.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
char c;
clrscr();
printf("\nEnter charcter:");
scanf("%c",&c);
printf("\nEnter Number:");
scanf("%d",&n);
printf("\nNext %d char is :",n);
while(n>0)
{
c=c+1;
printf("\n%c",c);
n--;
}
getch();
}
OUTPUT:
Enter charcter:A
Enter Number:10
Next 10 char is :
B
C
D
E
F
G
H
I
J
K
SET B
1. Write a program to display the first n Fibonacci numbers. (1 1 2 3 5 ……)
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a=1,b=1,c;
clrscr();
printf("\nEnter Nuumber:");
scanf("%d",&n);
if(n==1)
printf("\n%d",a);
if(n>=2)
printf("\n%d\t%d",a,b);
while(n>2)
{
c=a+b;
printf("\t%d",c);
a=b;
b=c;
n--;
}
getch();
}
2. Write a program to accept real number x and integer n and calculate the sum of first n terms
of the series x+ 3x+5x+7x+…
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n,i,t=1;
float ans=0;
clrscr();
printf("\nEnter real number and terms ");
scanf("%d%d",&x,&n);
for(i=1;i<=n;i++)
{
ans=ans+t*x;
t=t+2;
}
printf("\n Sum of %d terms of series is %f",n,ans);
getch();
}
OUTPUT:
Enter real number and terms 2
5
Sum of 5.000000 terms of series is 50.000000
3. Write a program to accept real number x and integer n and calculate the sum of first n terms
of the series
1/x*1+ 2/x*x+ 3/x*x*x+ ……
#include<stdio.h>
#include<conio.h>
void main()
{
float n,x,i,x1=1, ans=0;
clrscr();
printf("\nEnter real number and terms ");
scanf("%f%f",&x,&n);
for(i=1;i<=n;i++)
{
x1=x1*x;
ans=ans+i/x1;
}
printf("\n Sum of %f terms of series is %f",n,ans);
getch();
}
OUTPUT:
Enter real number and terms 2
3
Sum of 3.000000 terms of series is 1.375000
5. Write a program, which accepts a number n and displays each digit in words. Example: 6702
Output = Six-Seven-Zero-Two. (Hint: Reverse the number and use a switch statement)
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rev=0,d;
clrscr();
printf("\nEnter the number:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
while(rev>0)
{
d=rev%10;
switch(d)
{
case 1:printf("\tOne");
break;
case 2:printf("\tTwo");
break;
case 3:printf("\tThree");
break;
case 4:printf("\tFour");
break;
case 5:printf("\tFive");
break;
case 6:printf("\tSix");
break;
case 7:printf("\tSeven");
break;
case 8:printf("\tEight");
break;
case 9:printf("\tNine");
break;
default:printf("\nNot a digit:");
}
rev=rev/10;
}
getch();
}
OUTPUT:
Enter the number:1234
One Two Three Four
SET C
2. Write a program which accepts a number and checks if the number is a palindrome (Hint
number = reverse of number)
Example: number = 3472 Output: It is not a palindrome
number = 262, Output : It is a palindrome
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rev=0,n1,d;
clrscr();
printf("\nEnter Number:");
scanf("%d",&n);
n1=n;
while(n>0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
if(n1==rev)
printf("\n%d is palindrom",n1);
else
printf("\n%d is not palindrom",n1);
getch();
}
OUTPUT:
Enter Number:121
121 is palindrom
No comments:
Post a Comment