0% found this document useful (0 votes)
18 views5 pages

PART B Programs 8879821

This document contains 10 programming problems and their solutions. The problems cover checking if a number is a palindrome, finding the square root of a number, checking if a number is prime, counting vowels and consonants in a string, implementing selection sort, calculating a factorial using recursion, performing a linear search, copying one string to another, swapping two numbers using pointers, and reading and writing members of a union. Solutions are provided in C code.

Uploaded by

Anshul
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)
18 views5 pages

PART B Programs 8879821

This document contains 10 programming problems and their solutions. The problems cover checking if a number is a palindrome, finding the square root of a number, checking if a number is prime, counting vowels and consonants in a string, implementing selection sort, calculating a factorial using recursion, performing a linear search, copying one string to another, swapping two numbers using pointers, and reading and writing members of a union. Solutions are provided in C code.

Uploaded by

Anshul
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/ 5

Computer programming lab

Part B programs
1. WAP to check a number is palindrome or not
Sol: Refer the lab manual(page no. 17) for the program

2. WAP to find square root of a given number without using built in function.
Sol: Refer the lab manual(page no. 42) for the program

3. WAP to check whether the no. is prime or not.


Sol: Refer the lab manual(page no. 25) for the program

4. WAP to Print the number of vowels and consonants present in the string.
Sol:

#include <stdio.h>
#include<string.h>
int main()
{
char s[1000];
int i,vowels=0,consonants=0;
printf("Enter the string : ");
gets(s);
for(i=0;s[i];i++)
{
if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122))
{
if(s[i]=='a'|| s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'
||s[i]=='U')
vowels++;
else
consonants++;
}
}
printf("vowels = %d\n",vowels);
printf("consonants = %d\n",consonants);
return 0;
}

5. WAP to implement selection sort


Sol:
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elements");
scanf("%d", &n);
printf("Enter %d Numbers", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);

Prof.Vidya Y, CSE,RNSIT 1
Computer programming lab

for(i = 0; i < n - 1; i++)


{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("Sorted Array:");
for(i = 0; i < n; i++)
printf("%d\n", a[i]);
return 0;
}

6. Program to print factorial of a number using recursion


Sol:
#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;
}

7. WAP to implement linear search


Sol:
#include <stdio.h>
int main()
{
int a[100], search, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);

Prof.Vidya Y, CSE,RNSIT 2
Computer programming lab

printf("Enter %d integer(s)\n", n);

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


scanf("%d", &a[i]);

printf("Enter a number to search\n");


scanf("%d", &search);

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


{
if (a[i] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", search);

return 0;
}

8. WAP to copy the contents of one string to another and print the count of no. of characters copied.
Sol:
#include<stdio.h>
int main()
{
char str1[80], str2[80];
int i;

printf("Input a string");
scanf("%s", str2);

for(i=0; str2[i]!='\0'; i++)


str1[i]=str2[i];
str1[i]='\0';

printf("copied string: %s", str1);


printf("\nNumber of characters copied = %d\n", i);
return 0;
}

Prof.Vidya Y, CSE,RNSIT 3
Computer programming lab

9. WAP to swap 2 numbers using pointers.


Sol:
include <stdio.h>
void swap(int*, int*);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);


swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}

void swap(int *a, int *b)


{
int temp;
temp = *b;
*b = *a;
*a = temp;
}

10. WAP to read and write members of union.

#include <stdio.h>
#include <string.h>

union student
{
char name[20];
char addr[20];
float sal;
int id;
};

int main()
{
union student record1;

// assigning values to record1 union variable

strcpy(record1.name, "Rahul");
strcpy(record1.addr, "chennai");
record1.sal = 500000;
record1.id = 123;

Prof.Vidya Y, CSE,RNSIT 4
Computer programming lab

printf("Union record1 values example\n");

printf(" Name : %s \n", record1.name);


printf(" Address : %s \n", record1.addr);
printf(" Salary : %f \n\n", record1.sal);
printf(" ID : %d \n", record1.id);

return 0;
}

Prof.Vidya Y, CSE,RNSIT 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy