Week 1
a)
C program to find sum of individual digits of a number
#include<stdio.h>
int main()
{
int n,t,sum=0,rem;
printf("enter the number:");
scanf("%d",&n);
t=n;
while(t!=0)
{
rem=t%10;
sum=sum+rem;
t=t/10;
}
printf("sum of the digits:%d",sum);
}
OUTPUT
b) fibinacci sequence first n numbers
#include<stdio.h>
main()
{
int n,first=0,second=1,next,c;
printf("enter the no of terms of the sequence:");
scanf("%d",&n);
for(c=0;c<n;c++)
{
if(c<=1)
next=c;
else
{
next=first+second;
first=second;
second=next;
}
printf("%d\n",next);
}
return 0;
}
OUTPUT
c) C program to generate prime numbers between 1 and n
#include<stdio.h>
int main()
{
int n, i = 3, count, c;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
return 0;
}
OUTPUT
Comments
Post a Comment