Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Program Solution Notes

The document contains solutions to 5 questions on C programming concepts. The questions cover converting decimal to binary, calculating cosine series, arranging numbers in descending order using arrays, checking if a string is a palindrome, and printing a numeric pattern. The solutions provided use basic C programming constructs like loops, functions, arrays, strings, pattern printing etc. to demonstrate solutions to typical programming problems.

Uploaded by

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

Program Solution Notes

The document contains solutions to 5 questions on C programming concepts. The questions cover converting decimal to binary, calculating cosine series, arranging numbers in descending order using arrays, checking if a string is a palindrome, and printing a numeric pattern. The solutions provided use basic C programming constructs like loops, functions, arrays, strings, pattern printing etc. to demonstrate solutions to typical programming problems.

Uploaded by

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

Some

solution
of
typical c
program
Paper Name: programmming for problem solving
Paper Code: es-cs291
B. Tech, 1st year, 2nd Semester (2022-23)
Discipline: Textile Technology

Question no. 1:-


Write a program for converting a decimal number into a binary
number.
Solution:-
// convert decimal to binary

#include <stdio.h>
#include <math.h>
long long convert(int);
int main() {
int n, bin;
printf("Enter a decimal number: ");
scanf("%d", &n);
bin = convert(n);
printf("%d in decimal = %lld in binary", n, bin);
return 0;
}
long long convert(int n)
{
long long bin = 0;
int rem, i = 1;

while (n!=0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}

return bin;
}
Output:-
Enter a decimal number: 17
17 in decimal = 10001 in binary
--------------------------------
Process exited after 3.289 seconds with return value 0
Press any key to continue . . .
Question no. 2:-
Write a program to calculate the cosine series.

Solution:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
main()
{
int i, n;
float x, val, sum = 1, t = 1;
system("cls");
printf("Enter the value for x : ");
scanf("%f", &x);
printf("\nEnter the value for n : ");
scanf("%d", &n);
val = x;
x = x * 3.14159 / 180;
for(i = 1 ; i < n + 1 ; i++)
{
t = t * pow((double) (-1), (double) (2 * i - 1)) * x * x / (2 * i * (2 * i
- 1));
sum = sum + t;
}
printf("\nCosine value of %f is : %8.4f", val, sum);
getch();
}
Output:-

Enter the value for x : 60

Enter the value for n : 20

Cosine value of 60.000000 is : 0.5000


--------------------------------
Process exited after 4.895 seconds with return value 0
Press any key to continue . . .
Question no. 3:-
Write a C program using an array to accept N numbers and
arrange them in descending order.

Solution:-
#include <stdio.h>
main (){
int num[20];
int i, j, a, n;
printf("Enter number of elements in an array:- ");
scanf("%d", &n);
printf("Enter the elements:- \n");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){
if (num[i] < num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in descending order is:");
for (i = 0; i < n; ++i){
printf("%d \t", num[i]);
}
}
Output:-

Enter number of elements in an array:- 4


Enter the elements:-
9
7
5
4
The numbers in descending order is:9 7 5 4
--------------------------------
Process exited after 5.084 seconds with return value 0
Press any key to continue . . .
Question no. 4:-
Write a C program to find a given string is palindrome or not.

Solution:-
// C implementation to check if a given string is palindrome or not.
#include <stdio.h>
#include <string.h>

int main()
{
char str[100];
printf("Enter a string:",str);
scanf("%s",&str);
// Start from leftmost and rightmost corners of str
int l = 0;
int h = strlen(str) - 1;

// Keep comparing characters while they are same


while (h > l) {
if (str[l++] != str[h--]) {
printf("%s is not a palindrome string\n", str);
return 0;
// will return from here
}
}

printf("%s is a palindrome string\n", str);

return 0;
}
Output:-

Enter a string:program
program is not a palindrome string

--------------------------------
Process exited after 4.335 seconds with return value 0
Press any key to continue . . .

Question no. 5:-


Print the following pattern :-
1
10
101
1010
10101
Solution:-

/* Program to print 1-10-101-1010 */


#include<stdio.h>

/* Main function */
int main()
{
int i, j, n;
printf("Enter number of lines of pattern: ");
scanf("%d", &n);

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d", j%2);
}
printf("\n");
}

return 0;
}
Output:-
Enter number of lines of pattern: 5
1
10
101
1010
10101

--------------------------------
Process exited after 2.718 seconds with return value 0
Press any key to continue . . .

You might also like