CS 203 Assignment 1
Answer all 4 Ques ons
1. Your friend is asking you to count the numbers that are multiples of both 3 and 5 between two given numbers
(both inclusive). You suggest writing a program to do the same where you will take two numbers as input via the
console and output the count of numbers that are multiples of both 3 and 5
Note:
Input: Two integer number greater than 0 where the first number is less than the second number
Output: Count of numbers that are multiples of both 3 and 5 between given numbers (both inclusive)
Sample input
1
15
Output
1
Input (not shown to students)
10
1000
Output
66
Ans:
#include <stdio.h>
int main() {
int num1, num2;
scanf("%d", &num1);
scanf("%d", &num2);
int i = 0,count = 0;
for (i = num1;i <= num2;i++)
{
if (i%3 ==0 && i%5 == 0)
count++;
}
prin ("%d \n",count);
return 0;
}
2. Your teacher asks you to count the number of vowels in a given word. If a vowel repeats more than once, you
need to count it only once. Write a program to do the same. You can assume all lower-case le ers, no spaces in the
word and the maximum word length to be 50 characters.
Note:
Input: Word is taken as input from the user via the console
Output: Number of unique vowel characters in the input word
Refer to the sample input and output below for an example.
Sample Input
hello
Output
2
Input (not shown to students)
university
Output
3
, Ans:
#include <stdio.h>
#include <string.h>
int main() {
char a[50];
scanf("%s", a);
int len = 0, count= 0;
len = strlen(a);
int i = 0;
int vowel[] = {0,0,0,0,0};
for (i=0;i<len;i++)
{
if (a[i] == 'a')
vowel[0]++;
else if (a[i] == 'e')
vowel[1]++;
else if (a[i] == 'i')
vowel[2]++;
else if (a[i] == 'o')
vowel[3]++;
else if (a[i] == 'u')
vowel[4]++;
}
for (i=0;i< 5;i++)
{
if (vowel[i] > 0)
{
count++;
}
}
prin ("%d \n",count);
return 0;
}
3. Write a program that accepts two 2x2 matrices of integers and prints their product which will be a 2x2 matrix of
integers.
Note:
Input: The elements of the first matrix row-wise, followed by the elements of the
second matrix, also row-wise, with one element in each line
Output: The elements of resul ng matrix, row-wise,
with the elements of each row separated by a tab
Refer to the sample input and output below for an example.
Sample Input
1
1
2
2
1
1
2
2
Output
3 3
Answer all 4 Ques ons
1. Your friend is asking you to count the numbers that are multiples of both 3 and 5 between two given numbers
(both inclusive). You suggest writing a program to do the same where you will take two numbers as input via the
console and output the count of numbers that are multiples of both 3 and 5
Note:
Input: Two integer number greater than 0 where the first number is less than the second number
Output: Count of numbers that are multiples of both 3 and 5 between given numbers (both inclusive)
Sample input
1
15
Output
1
Input (not shown to students)
10
1000
Output
66
Ans:
#include <stdio.h>
int main() {
int num1, num2;
scanf("%d", &num1);
scanf("%d", &num2);
int i = 0,count = 0;
for (i = num1;i <= num2;i++)
{
if (i%3 ==0 && i%5 == 0)
count++;
}
prin ("%d \n",count);
return 0;
}
2. Your teacher asks you to count the number of vowels in a given word. If a vowel repeats more than once, you
need to count it only once. Write a program to do the same. You can assume all lower-case le ers, no spaces in the
word and the maximum word length to be 50 characters.
Note:
Input: Word is taken as input from the user via the console
Output: Number of unique vowel characters in the input word
Refer to the sample input and output below for an example.
Sample Input
hello
Output
2
Input (not shown to students)
university
Output
3
, Ans:
#include <stdio.h>
#include <string.h>
int main() {
char a[50];
scanf("%s", a);
int len = 0, count= 0;
len = strlen(a);
int i = 0;
int vowel[] = {0,0,0,0,0};
for (i=0;i<len;i++)
{
if (a[i] == 'a')
vowel[0]++;
else if (a[i] == 'e')
vowel[1]++;
else if (a[i] == 'i')
vowel[2]++;
else if (a[i] == 'o')
vowel[3]++;
else if (a[i] == 'u')
vowel[4]++;
}
for (i=0;i< 5;i++)
{
if (vowel[i] > 0)
{
count++;
}
}
prin ("%d \n",count);
return 0;
}
3. Write a program that accepts two 2x2 matrices of integers and prints their product which will be a 2x2 matrix of
integers.
Note:
Input: The elements of the first matrix row-wise, followed by the elements of the
second matrix, also row-wise, with one element in each line
Output: The elements of resul ng matrix, row-wise,
with the elements of each row separated by a tab
Refer to the sample input and output below for an example.
Sample Input
1
1
2
2
1
1
2
2
Output
3 3