Professional Documents
Culture Documents
Lab Assignment – 2
Name: Ajayjith.N.S
Reg.no:18BCE2332
Questions:
Solutions:
1.
CODE:
#include <stdio.h>
int main()
{
int num1, num2, num3;
int *p1, *p2, *p3;
2.
CODE:
#include<stdio.h>
int main()
{
int array[5];
int i,sum=0;
int *ptr;
for(i=0;i<5;i++)
{
//*ptr refers to the value at address
sum = sum + *ptr;
ptr++;
}
3.
CODE:
#include <stdio.h>
int main()
{
int x, y, *a, *b, temp;
temp = *b;
*b = *a;
*a = temp;
return 0;
}
OUTPUT:
4.
CODE:
#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;
printf("Enter the string:\n");
scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ' && s[i+1] != ' ')
count++;
}
printf("Number of words in given string are: %d\n", count + 1);
}
OUTPUT:
5.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
char str[50];
int i=0, j, chk;
printf("Enter a String: ");
gets(str);
while(str[i]!='\0')
{
chk=0;
if(str[i]==' ')
{
j=i;
while(str[j-1]!='\0')
{
str[j] = str[j+1];
j++;
}
chk = 1;
}
if(chk==0)
i++;
}
printf("\nString (without spaces): %s", str);
getch();
return 0;
}
OUTPUT:
6.
CODE:
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
OUTPUT:
7.
CODE:
#include <stdio.h>
int main() {
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
return 0;
}
OUTPUT:
8.
CODE:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}