C Programming Lab Manual – 4

Write a C program to calculate sum of two numbers from user using third variable.

Function Main
    Declare Integer a
    Declare Integer b
    Declare Integer sum
    
    Output "Enter first number: "
    Input a
    Output "Enter second number: "
    Input b
    Assign sum = a + b
    Output "The sum is: " sum
End

#include <stdio.h>
int main() {
    int a;
    int b;
    int sum;
    
    printf("Enter first number: ");
    scanf("%d",&a);
    printf("Enter second number: ");
    scanf("%d",&b);
    sum = a + b;
    printf("The sum is: %d", sum);
    return 0;
}


Enter first number: 5
Enter second number: 10
The sum is: 15

C Programming Lab Manual – 3

Write a C program to generate all the numbers of the limit between 1 and n is a value supplied by count.

Read n value
Initialize count 0
for i 1 to n
for j 1 to i
if i mod j is equal to 0
then increment count
then print count.
#include<stdio.h>
 
int main()
{
int i, j, n, count=0;
 
printf("Enter the limit:");
scanf("%d", &n);
printf("The limit numbers are:\n");
for(i=1;i<=n;i++)
{
 for(j=1;j<=i;j++)
 {
    if(i%j==0)
    {
    count++;
    }
      
 } 
 printf("%d\n", count);
}
return 0;
}
Enter the limit:5
The limit numbers are:
1
3
5
8
10

C Programming Lab Manual – 2

Write a C program to generate the first n terms of the Fibonacci sequence.

Read the number of terms n
Initialize a = 0, b = 1 
print a and b values
for i = 3 to n
increment the i value
c = a+b
print c value
a = b
b = c
#include<stdio.h>
int main()
{
int a=0,b=1,c,n,i;
printf("Enter no. of terms:");
scanf("%d", &n);
printf("The Fibonacci sequence is:");
printf("%d%d", a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf("%d",c);
a=b;
b=c;
}
return 0;
getchar();
}

Enter no. of terms:5

The Fibonacci sequence is:01123

C Programming Lab Manual – 1

Write a C program to find the sum of individual digits of a positive integer.

Read the number n
Initialize sum = 0
while n > 0
d = n%10
sum = sum+d
n = n/10
print sum

#include<stdio.h>
int main()
{
int n, sum=0,d;
printf("Enter any integer:");
scanf("%d", &n);
while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
printf("sum of individual digits is: %d",sum);
return 0;
}
Enter any integer:1234

sum of individual digits is:10