0% found this document useful (0 votes)
3 views13 pages

C LEVEL 2

The document contains multiple C programming tasks that involve various algorithms and operations, such as calculating Fibonacci numbers, converting binary to decimal, summing cubes of odd numbers, counting digits, generating prime numbers, and checking for strong and Kaprekar numbers. Each task includes a code snippet that demonstrates the implementation of the described functionality. The tasks cover a range of programming concepts and techniques suitable for practice and learning.

Uploaded by

aksharadeepa2006
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)
3 views13 pages

C LEVEL 2

The document contains multiple C programming tasks that involve various algorithms and operations, such as calculating Fibonacci numbers, converting binary to decimal, summing cubes of odd numbers, counting digits, generating prime numbers, and checking for strong and Kaprekar numbers. Each task includes a code snippet that demonstrates the implementation of the described functionality. The tasks cover a range of programming concepts and techniques suitable for practice and learning.

Uploaded by

aksharadeepa2006
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/ 13

TASK 1:

#include <stdio.h>
int main() {
int t0=1,t1=2,n;
int nextterm=t0+t1;
printf("enter the input:");
scanf("%d",&n);
if(n<0 || n>20)
{
printf("invalid iput");
}
else
{
if(n==0)
{
printf("%d",t0);
}
else if(n==1)
{
printf("%d",t1);
}
else
{
for(int i=2; i<=n; i++)
{
nextterm=t0+t1;
t0=t1;
t1=nextterm;
}
printf("%d",nextterm);
}
}
}
TASK 2:

#include <stdio.h>
#include<string.h>
int main() {
int rem,dec=0,base=1,i;
char binary[100];
printf("enter a binary number:");
scanf("%s",&binary);
if(binary[0]=='-')
{
printf("invalid input");
return 0;
}
for(i=0;i<strlen(binary);i++)
{
if(binary[i]!='0' && binary[i]!='1')
{
printf("invalid input");
return 0;
}
}
for (int i = strlen(binary) - 1; i >= 0; i--)
{
if (binary[i] == '1')
{
dec += base;
}
base *= 2;
}
printf("the decimal equivalent is : %d",dec);
}
TASK 3:

#include <stdio.h>
int main() {
int i,cube,n,sum;
printf("enter the limit:");
scanf("%d",&n);
if(n<0)
{
printf("invalid");
}
else
{
while(i<=n)
{
cube=i*i*i;
sum=sum+cube;
i=i+2;
}
printf("%d",sum);
}
}

TASK 4:

#include <stdio.h>
int main() {
int n,count;
printf("enter the number:");
scanf("%d",&n);
do{
count++;
n=n/10;
}while(n!=0);
printf("the number of digits : %d",count);
}
OR

#include <stdio.h>
int main() {
int n,count;
printf("enter the number:");
scanf("%d",&n);
int c=1;
if(n==0)
{
printf("the number of digits : %d",c);
return 0;
}
else if(n!=0)
{
for(int i=1;i<=n;i++)
{
count++;
n=n/10;
}
}
printf("the number of digits : %d",count);
}

TASK 6:

#include <stdio.h>
int main() {
int n;
printf("Enter the number of prime numbers to print: ");
scanf("%d", &n);

int count = 0;
int num = 2;
while (count < n) {
int i;
for (i = 2; i * i <= num; i++) {
if (num % i == 0) break;
}
if (i * i > num) {
printf("%d ", num);
count++;
}
num++;
}
printf("\n");
return 0;
}

TASK 7:

#include <stdio.h>
int main() {
int n;
float i,value,sum;
printf("enter the number:");
scanf("%d",&n);
if(n<0||n==0|| scanf("%d",&n)!=1)
printf("invalid");
else
{
for(i=1;i<=n;i++)
{
value=1/i;
sum=sum+value;
}
printf("%f\n",sum);
}
}
TASK 8:

#include <stdio.h>
int main() {
int i,j,n;
printf("enter a number:");
scanf("%d",&n);
if(n<2 || n>50 )
{
printf("invalid");
}
else
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==1||i==n||j==1||j==n)
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
}

TASK 10:

#include <stdio.h>
int main() {
int n,i,j,s,c;
printf("enter the number:");
scanf("%d",&n);
if(n<0 || n==0)
printf("invalid");
else
{
for(i=0;i<n;i++)
{
for(s=0;s<n-i-1;s++)
{
printf(" ");
}
for(j=0;j<i+1;j++)
{
if(j==0)
c=1;
else
{
c=c*(i-j+1)/j;
}
printf("%d ",c);
}
printf("\n");
}
}
}

TASK 11:

#include <stdio.h>
#include<math.h>
int main() {
int n,i,count,sum,rem;
printf("enter the number:");
scanf("%d",&n);
count=log10(n)+1;
int t=n;
if(n<0)
printf("invalid");
else
{
while(n!=0)
{
rem=n%10;
sum=sum+pow(rem,count);
n=n/10;
}
if(sum==t || t<10)
{
printf("1");
}
else
{
printf("0");
}
}
}

TASK 12:

#include <stdio.h>
int main() {
long long n,t,r,rev,mul=1,sum=0;
printf("Enter the number: ");
scanf("%lld", &n);
if(n<0)
printf("invalid");
else
{
t=n;
while(n!=0)
{
r=n%10;
rev=(rev*10)+r;
sum=sum+r;
mul=mul*r;
n=n/10;
}
if(rev==t && t!='-')
{
printf("palindrome\n");
printf("the product is %lld",mul);
}
else
{
printf("not palindrome\n");
printf("the sum is %lld",sum);
}
}
}

TASK 13:

#include<stdio.h>
int main()
{
int i,size,j,x=1;
printf("enter the number of rows:");
scanf("%d",&size);
for(i=0;i<=size;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",x);
x=1-x;
}
printf("\n");
}
}

TASK 14:

#include <stdio.h>
int main() {
int n,add=0,f,i,j,r;
printf("enter the number:");
scanf("%d",&n);
int t=n;
for(i=n;n!=0;i++)
{
r=n%10;
f=1;
for(j=1;j<=r;j++)
{
f=f*j;
}
add+=f;
n=n/10;
}
if(add==t)
printf("strong number");
else
printf("not a strong number");
}

TASK 15:

#include <stdio.h>
#include<math.h>
int main() {
int i,n,count=0,square,firstnum,secondnum;
printf("enter a number:");
scanf("%d",&n);
square=n*n;
int t=square;
while(square!=0)
{
square=square/10;
count++;
}
if(count%2!==0)
{

}
int firstnum=t/pow(10,count/2);
int secondnum=t% (int) (pow(10,count/2));
if(firstnum+secondnum==n && firstnum!=0 && secondnum!=0)
printf("kaprekar number");
else
printf("not a kaprekar number");
}

TASK 16:

#include <stdio.h>
#include<math.h>
int main() {
long long i,n,t=9,sum=0;
printf("enter the range:");
scanf("%d",&n);
if(n<0)
printf("invalid");
else
{
for(i=1;i<=n;i++)
{
printf("%lld ",t);
sum=sum+t;
t+=9*pow(10,i);
}
printf("\n the sum of the series is :%lld",sum);
}
}

#include <stdio.h>
#include<math.h>
int main() {
int n,i,j=0,count=0;
printf("enter the number:");
scanf("%d",&n);
int t=n;
while(n!=0)
{
count++;
n=n/10;
}
int r=n%10;
int x=t%(int)(pow(10,count-1));
int sum=r+x;
printf("%d\n",sum);

You might also like