Program Solution Notes
Program Solution Notes
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
#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:-
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:-
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;
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 . . .
/* 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 . . .