Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
32 views12 pages

Ds Lab

The document contains C code examples for various programming assignments. The examples demonstrate basic concepts like printing values, using loops and arrays, recursion, functions, and conditional operators. Programs include calculating sums, finding factors, searching arrays, and determining if a number is even or odd.

Uploaded by

vmhsphysics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views12 pages

Ds Lab

The document contains C code examples for various programming assignments. The examples demonstrate basic concepts like printing values, using loops and arrays, recursion, functions, and conditional operators. Programs include calculating sums, finding factors, searching arrays, and determining if a number is even or odd.

Uploaded by

vmhsphysics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Assignment 1: 1) WAP to print first 10 natural numbers.

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("\n%d",i);
}
return 0;
}
Assignment 1: 2)WAP to print the sum of first 10 natural numbers.

#include<stdio.h>
#include<conio.h>
int main()
{
int i,sum=0;
for(i=1;i<=10;i++)
{
sum+=i;
}
printf("Sum: %d",sum);
return 0;
}
Assignment 1: 3)WAP to store 5 numbers ( taken input from the user ) in
an one dimensional array and display.

#include<stdio.h>
#include<conio.h>
#define max 100
int main()
{
int a[max],i,sum=0;
printf("Enter 5 elements in the array:\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Elements in the array are:\n");
for(i=0;i<5;i++)
{
printf("\t%d",a[i]);
}

return 0;
}
Assignment 1: 4)WAP to calculate sum of 5 numbers stored in an one
dimensional array.

#include<stdio.h>
#include<conio.h>
#define max 100
int main()
{
int a[max],i,sum=0;
printf("Enter 5 elements in the array:\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
sum+=a[i];
}
printf("\nSum : %d",sum);
return 0;
}
Assignment 1: 5)WAP to search a number in an array using linear search.

#include<stdio.h>
#include<conio.h>
#define max 100
int main()
{
int a[max],i,s,loc,f=0,sum=0;
printf("Enter 5 elements in the array:\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched:");
scanf("%d",&s);
for(i=0;i<5;i++)
{
if(a[i]==s)
{
f=1;
loc=i;
break;
}
}
if(f==0)
printf("\nElement not present !!!");
else
printf("\nElement present at %d position",loc+1);

return 0;
}
Assignment 1: 6)WAP to search a number in an array using Binary search.

#include <stdio.h>

int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d",&array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last)


{
if (array[middle] < search)
first = middle + 1;

else if (array[middle] == search)


{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}
Assignment 2: 4) WAP to print fibonacci series using recursion.

#include<stdio.h>
int f(int);
int main()
{
int n, i = 0, c;
printf("\nEnter the number of terms: ");
scanf("%d", &n);
printf("\nFibonacci series terms are:\n");
for (c = 1; c <= n; c++)
{
printf("%d\n", f(i));
i++;
}
return 0;
}
int f(int n)
{
if (n == 0 || n == 1)
return n;
else
return (f(n-1) + f(n-2));
}
5) Write a recursive C program to calculate sum of digits.

#include <stdio.h>
int sum (int a);
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}

int sum (int num)


{
if (num != 0)
{
return (num % 10 + sum (num / 10));
}
else
{
return 0;
}
}
Assignment 2: 6) WAP to find factorial of a number using recursion.

#include <stdio.h>
long int fact(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, fact(n));
return 0;
}
long int fact(int n)
{
if (n >= 1)
return n*fact(n-1);
else
return 1;
}
1) Write a C program to calculate the sum of two numbers using
function.

#include<stdio.h>
int sum1(int a,int b)
{
return a+b;
}
int main()
{
int num1,num2,s;
printf("\n Enter the 1st number: ");
scanf("%d",&num1);
printf("\n Enter the 2nd number: ");
scanf("%d",&num2);
s=sum1(num1,num2);
printf("\nSum: %d",s);
return 0;

}
5)Write a C program to illustrate the use of conditional operator.

#include <stdio.h>
int main()
{
int a,b,c;
printf("\nEnter a:");
scanf("%d",&a);
printf("\nEnter b (should not be same as a):");
scanf("%d",&b);
printf(a > b ? "\n a is larger" : "\n b is larger");
return 0;
}
7)Write a C program to check whether a number is even or odd.

#include<stdio.h>
int main()
{
int num;
printf("\nEnter the number: ");
scanf("%d",&num);
puts(num%2==0?"Even":"Odd");
return 0;

You might also like