Welcome to namasudra.blogspot.com. Glad to see you here!
Showing posts with label C_and_Cpp. Show all posts
Thursday, March 14, 2019
Friday, February 8, 2019
Splitting Currency Notes
Q. Write a program in C to split a given amount of money into currency notes of different types using switch statement.
Solution:
Solution:
Tuesday, February 5, 2019
Fibonacci Series (in C)
Write a program in C to generate the first n terms of the Fibonacci Series.
Code:
#include <stdio.h>
int main()
{
int i,n,term1=0,term2=1,next;
printf("Enter number of terms: ");
scanf("%d",&n);
printf("The first %d terms of Fibonacci Series are:\n",n);
for(i=0;i<n;i++){
printf("%d, ",term1);
next = term1+term2;
term1 = term2;
term2 = next;
}
return 0;
}
Code:
#include <stdio.h>
int main()
{
int i,n,term1=0,term2=1,next;
printf("Enter number of terms: ");
scanf("%d",&n);
printf("The first %d terms of Fibonacci Series are:\n",n);
for(i=0;i<n;i++){
printf("%d, ",term1);
next = term1+term2;
term1 = term2;
term2 = next;
}
return 0;
}
Binary Search
Write a program in C to search an element in an array using binary search.
To search for an element in an array using Binary Search the array should be sorted.
Code:
#include <stdio.h>
int main()
{
int i,first,last,middle,n,element,array[100];
// read number of elements
printf("Enter number of elements to enter in the array (max 100):");
scanf("%d",&n);
// input elements in the array
printf("Enter %d elements one by one in ascending order:\n",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
// read element to search for
printf("Enter the element to be searched in the array:");
scanf("%d",&element);
// algorithm for binary search
first = 0;
last = n-1;
middle = (first+last)/2;
while(first<=last){
if(element > array[middle])
first = middle+1;
else if(element < array[middle])
last = middle-1;
else{
printf("%d found at position %d",element,middle+1);
break;
}
middle = (first+last)/2;
}
if(first>last)
printf("%d not found in the array!",element);
return 0;
}
To search for an element in an array using Binary Search the array should be sorted.
Code:
#include <stdio.h>
int main()
{
int i,first,last,middle,n,element,array[100];
// read number of elements
printf("Enter number of elements to enter in the array (max 100):");
scanf("%d",&n);
// input elements in the array
printf("Enter %d elements one by one in ascending order:\n",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
// read element to search for
printf("Enter the element to be searched in the array:");
scanf("%d",&element);
// algorithm for binary search
first = 0;
last = n-1;
middle = (first+last)/2;
while(first<=last){
if(element > array[middle])
first = middle+1;
else if(element < array[middle])
last = middle-1;
else{
printf("%d found at position %d",element,middle+1);
break;
}
middle = (first+last)/2;
}
if(first>last)
printf("%d not found in the array!",element);
return 0;
}
Multiplication Table
Write a program in C to print multiplication table.
Code:
#include <stdio.h>
int main()
{
int num,i,prod;
printf("Enter an integer: ");
scanf("%d", &num);
printf("\nThe multiplication table of %d is:",num);
for(i=1;i<=10;i++){
prod = num * i;
printf("\n %d X %d = %d",num,i,prod);
}
return 0;
}
Code:
#include <stdio.h>
int main()
{
int num,i,prod;
printf("Enter an integer: ");
scanf("%d", &num);
printf("\nThe multiplication table of %d is:",num);
for(i=1;i<=10;i++){
prod = num * i;
printf("\n %d X %d = %d",num,i,prod);
}
return 0;
}
Factorial using function
Write a program in C to find the factorial of a number using function.
Code:
#include <stdio.h>
long factorial(long x);
int main()
{
int num;
long fact;
printf("Enter an integer: ");
scanf("%d", &num);
fact = factorial(num);
printf("The factorial of %d is %ld", num, fact);
return 0;
}
long factorial(long x){
if(x == 0) return 1;
return x * factorial(x - 1);
}
Code:
#include <stdio.h>
long factorial(long x);
int main()
{
int num;
long fact;
printf("Enter an integer: ");
scanf("%d", &num);
fact = factorial(num);
printf("The factorial of %d is %ld", num, fact);
return 0;
}
long factorial(long x){
if(x == 0) return 1;
return x * factorial(x - 1);
}
Greatest between 3 numbers (Method 2)
Write a program in C to find the greatest of three numbers.
Code:
#include <stdio.h>
int main()
{
int a,b,c,max;
printf("Enter 3 integers:\n");
scanf("%d %d %d",&a,&b,&c);
max = a;
if(b>max) max = b;
if(c>max) max = c;
printf("%d is the greatest number.",max);
return 0;
}
Code:
#include <stdio.h>
int main()
{
int a,b,c,max;
printf("Enter 3 integers:\n");
scanf("%d %d %d",&a,&b,&c);
max = a;
if(b>max) max = b;
if(c>max) max = c;
printf("%d is the greatest number.",max);
return 0;
}
Greatest between 3 numbers (Method 1)
Write a program in C to find the greatest of three numbers.
Code:
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter 3 integers:\n");
scanf("%d %d %d",&a,&b,&c);
if(a>b){
if(a>c){
printf("%d is the greatest number.",a);
}
else{
printf("%d is the greatest number.",c);
}
}
else{
if(b>c){
printf("%d is the greatest number.",b);
}
else{
printf("%d is the greatest number.",c);
}
}
return 0;
}
Code:
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter 3 integers:\n");
scanf("%d %d %d",&a,&b,&c);
if(a>b){
if(a>c){
printf("%d is the greatest number.",a);
}
else{
printf("%d is the greatest number.",c);
}
}
else{
if(b>c){
printf("%d is the greatest number.",b);
}
else{
printf("%d is the greatest number.",c);
}
}
return 0;
}
Monday, February 4, 2019
A Class Example (C++)
Q. Define a class student with the following specification:
admno interger
sname 20 characters
eng, math, science float (marks in 3 subjects)
total float
ctotal() A function to calculate
eng + math + science marks
Public member fucntions of the class student:
takedata() function to accept values for admno,
sname, marks in eng, math, science and
invoke ctotal() to calculate total.
showdata() function to display
Give detailed function definitions also.
Solution:
#include <iostream>
using namespace std;
class student{
int admno;
char sname[20];
float eng, math, science;
float total;
void ctotal();
public:
void takedata();
void showdata();
};
void student::ctotal(){
total = eng + math + science;
}
void student::takedata(){
cout << "Enter Admission Number: ";
cin >> admno;
cout << "Enter Name of Student: ";
cin >> sname;
cout << "Enter marks in English: ";
cin >> eng;
cout << "Enter marks in Mathematics: ";
cin >> math;
cout << "Enter marks in Science: ";
cin >> science;
ctotal();
}
void student::showdata(){
cout << "\nAdmission Number: " << admno;
cout << "\nName of Student: " << sname;
cout << "\nMarks in English: " << eng;
cout << "\nMarks in Mathematics: " << math;
cout << "\nMarks in Science: " << science;
cout << "\nTotal Marks: " << total;
}
.
admno interger
sname 20 characters
eng, math, science float (marks in 3 subjects)
total float
ctotal() A function to calculate
eng + math + science marks
Public member fucntions of the class student:
takedata() function to accept values for admno,
sname, marks in eng, math, science and
invoke ctotal() to calculate total.
showdata() function to display
Give detailed function definitions also.
Solution:
#include <iostream>
using namespace std;
class student{
int admno;
char sname[20];
float eng, math, science;
float total;
void ctotal();
public:
void takedata();
void showdata();
};
void student::ctotal(){
total = eng + math + science;
}
void student::takedata(){
cout << "Enter Admission Number: ";
cin >> admno;
cout << "Enter Name of Student: ";
cin >> sname;
cout << "Enter marks in English: ";
cin >> eng;
cout << "Enter marks in Mathematics: ";
cin >> math;
cout << "Enter marks in Science: ";
cin >> science;
ctotal();
}
void student::showdata(){
cout << "\nAdmission Number: " << admno;
cout << "\nName of Student: " << sname;
cout << "\nMarks in English: " << eng;
cout << "\nMarks in Mathematics: " << math;
cout << "\nMarks in Science: " << science;
cout << "\nTotal Marks: " << total;
}
.
Subscribe to:
Posts (Atom)