Tuesday, February 1, 2011

Computer Programming 1week Programs


Week l.
a)     Write a C program to find the sum of individual digits of a positive integer.
ALGORITHM:
Step1: start
Step2: Initialize k=1,sum=0
Step3: read n
Step4: Repeat step until n!=0
Step5: k=n%10
            sum=sum+k
            n=n/10
Step6: Dispaly sum
Step7:Stop.
     FLOWCHART:

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
  int num, k=1, sum=0;
  clrscr();
  printf("Enter the number whose digits are to be added:");
  scanf("%d",&num);
while(num!=0)
{
  k=num%10;
  sum=sum+k;
  k=num/10;
  num=k;
}
printf("Sum of the digits:%d",sum);
getch();     }

OUTPUT:
Enter the digits whose digits are to be added : 1234
Sum of the Digits is : 10

b)     AIM: A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C program to generate the first n terms of the sequence.
ALGORITHM:
Step1: start
Step2: read I,x,f,f1,f2
Step3: f=0,f1=1,f2=1
Step4: do
I++
F1=f2
F2=f
F=f1+f2
While (i<=n)
Step5: print f
Step6: stop
    
FLOWCHART:

PROGRAM:
void main()
{
int i,n,f,f1,f2;
printf("enter the range");
scanf("%d",&n);
f=0;
f1=1;
f2=1;
do
{
i++;
printf(“The Fibonacci Series is :”);
printf(" %d\n ",f);
f1=f2;
f2=f;
f=f1+f2;
}
while(i<=n);
}


OUTPUT:
Enter the range 9
The Fibonacci Series is :
0 1 1 2 3 5 8 13 21



c)     AIM: Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user.
ALGORITHM:
Step1: start
Step2: read n value
Step3: for i=1 i<=n
Step4:repeat a b c d e
a)factorial equal to 0
b) for i=1,j<=1 repeat c,d
c)if i percentage j equal to zero
d) fact equal to factorial added with one
e) if factorial equal to2print as prime number
step5: display the prime no till nth num
Step6: stop

FLOWCHART:

PROGRAM:
void main()
{
int n,i,fact,j;
printf("enter the range");
scanf("%d",&n);
printf(“Prime numbers are\n”);
for(i=1;i<=n;i++)
{
fact=0;
for(j=1;j<=n;j++)
{
if(i%j==0)
fact++;
if(f==2)
printf("%d  “,i);
}
getch();
}

 OUTPUT:

Enter the range 10
Prime numbers are
3  5  7

3 comments: