Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts
Saturday, March 31, 2018
ISRO Questions - C/C++ Programming
INDIAN SPACE RESEARCH ORGANISATION
Recruitment Entrance Test for Scientist/Engineer
Recruitment Entrance Test for Scientist/Engineer
Previously asked questions on C/C++ Programming
ISRO May 2017
1. What is the output of this C++ program?
#include <iostream>
using namespace std;
void square (int *x)
{
*x = (*x)++ * (*x);
}
void square (int *x, int *y)
{
*x = (*x) * --(*y);
}
int main ( )
{
int number = 30;
square(&number, &number);
cout << number;
return 0;
}
(a) 910 (b) 920 (c) 870 (d) 900
Ans: option (c)
Explanation:
Starting from main function. number variable has been initialized to 30. We pass address of number as parameters in the square function. As per the concept of function overloading in C++ the second square function will be executed.
--(*y) will be executed first since decrement operator has the higher precedence than multiplication.
Therefore,
*x = (*x) * --(*y);
*x = 30 * 29;
*x = 870
x is a pointer variable and it holds the address of the variable number. Therefore the value of 870 is now stored to number after the execution of *x = (*x) * --(*y); expression. Hence 870 will be printed.
Other points:
Other points:
- iostream is a header file. Header files are included in C/C++ because they have some inbuilt predefined functions.
- Purpose of using namespace std; - When you make a call to using namespace <some_namespace>; all symbols in that namespace will become visible without adding the namespace prefix. If you do not add that statement you need to call cout << number; like this std::cout << number; else you get the error 'cout' was not declared in this scope.
- Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. Difference in parameters can be in terms of number of parameters as we saw in the case of square function or difference can be in terms of data type also. For example refer: https://www.geeksforgeeks.org/function-overloading-c/
Friday, January 1, 2016
C program to check whether a number is an Armstrong number
/* Also known as narcissistic numbers,
* Armstrong numbers are the sum of their own digits
* to the power of the number of digits.
* Example: 407 = 4^3 + 0^3 + 7^3 = 407
*/
#include <stdio.h>
#include <math.h>
int main(void) {
int number_to_check = 153;
int copy_number = number_to_check;
int digits[10];
int i = 0, sum = 0, number_of_digits;
while(number_to_check!=0){
digits[i] = number_to_check % 10;
number_to_check = number_to_check/10;
i++;
}
number_of_digits = i;
for(i = number_of_digits-1; i >= 0; i--){
sum = sum + pow(digits[i], number_of_digits);
}
if(copy_number == sum)
printf("The supplied number is an armstrong number.");
else
printf("The supplied number is NOT an armstrong number.");
return 0;
}
C programs related to Fibonacci series
C program to find Fibonacci series without recursion
#include <stdio.h>
int main(void) {
int n = 10; // print n fibonacci series
int a = 0, b = 1, s;
printf("%d %d ", a,b);
n = n - 2; // already 2 numbers printed
while(n--){
s = a + b;
printf("%d ",a+b);
a = b;
b = s;
}
return 0;
}
Thursday, December 31, 2015
C program to add 2 numbers without using arithmetic operators
#include <stdio.h>
int main(void) {
int a = 3, b = 5, c = 0;
while(b != 0){
c = a & b; // carry
a = a ^ b;
b = c << 1;
}
printf("%d",a);
return 0;
}
Ref: http://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/
C program to print various patterns
C program to print the below pattern
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
ABCDCBA
ABCBA
ABA
A
Wednesday, December 30, 2015
C program to round numbers and decimals without using in-built functions
Suppose we want to round off 838.274. Depending on which place value we're rounding to, the final result can vary. For example,
Reference : http://www.calculatorsoup.com/calculators/math/roundingnumbers.php
C Program to round a number to the nearest thousand
- Round to the nearest hundred (838.274) is 800
- Round to the nearest ten (838.274) is 840
- Round to the nearest one (838.274) is 838
- Round to the nearest tenth (838.274) is 838.3
- Round to the nearest hundredth (838.274) is 838.27
Reference : http://www.calculatorsoup.com/calculators/math/roundingnumbers.php
C Program to round a number to the nearest thousand
#include <stdio.h>
int main(void) {
double a=3250;
int x = (a+500)/1000;
x = x*1000;
printf("%d",x); //3000
return 0;
}
C Program to swap two numbers without using a temporary variable
C Program to swap two numbers without using a temporary variable
First Method
First Method
#include <stdio.h>
int main(void) {
int a=3,b=6;
a = a + b; // a = 9
b = a - b; // b = 3
a = a - b; // a = 6
printf("a=%d,b=%d",a,b);
return 0;
}
C program to add two numbers using ++ and -- operators
First method using ++ and -- operators
#include <stdio.h>
int main(void) {
int a=3,b=6;
while(b--){
a++;
}
printf("%d",a);
return 0;
}
Wednesday, December 9, 2015
GATE Questions - C Programming
Previous GATE questions with solutions on C Programming - CS/IT
GATE-2000
1. The number of tokens in the following C statement.
(a) 3 (b) 26 (c) 10 (d) 21
Ans: option (c)
Explanation:
The smallest individual units are known as C Tokens. The keywords, identifiers, constants, string literals, and operators described in this section are examples of tokens. Punctuation characters such as brackets ([ ]), braces ({ }), parentheses ( ( ) ), and commas (,) are also tokens.
GATE-2000
1. The number of tokens in the following C statement.
printf("i = %d, &i = %x", i, &i);
is(a) 3 (b) 26 (c) 10 (d) 21
Ans: option (c)
Explanation:
The smallest individual units are known as C Tokens. The keywords, identifiers, constants, string literals, and operators described in this section are examples of tokens. Punctuation characters such as brackets ([ ]), braces ({ }), parentheses ( ( ) ), and commas (,) are also tokens.
Monday, December 7, 2015
Tricky concepts of Operators in C Programming Language
We assume that you know the basics of C Programming.
1.
int c=10,a=5,d;
d=a=c;
After the execution of above statement d will have the value 10.
Explanation: Equality operator associates Right to Left, i.e. in the above statement first c is assigned to a.
The expression a=c evaluates to the value of a after the assignment takes place. Then this value is assigned to d.
1.
int c=10,a=5,d;
d=a=c;
After the execution of above statement d will have the value 10.
Explanation: Equality operator associates Right to Left, i.e. in the above statement first c is assigned to a.
The expression a=c evaluates to the value of a after the assignment takes place. Then this value is assigned to d.
Monday, April 6, 2015
Void Pointers
Declaration of a void pointer is given below:
void *ptr1;
ptr1 is a void pointer. ptr1 is a pointer that can point to anything. Void pointers are also known as generic pointers.
void *ptr1;
ptr1 is a void pointer. ptr1 is a pointer that can point to anything. Void pointers are also known as generic pointers.
Tuesday, August 13, 2013
C Programming - Aptitude Questions
1. What values are printed out by the following C program?
#include <stdio.h>
int fun(int x, int y) {
x = 2*x + y;
return x;
}
int main(void) {
int x = 2, y = 5;
y = fun(y, x);
x = fun(y, x);
printf("%d %d\n", x, y);
return 0;
}
Wednesday, July 17, 2013
C Program to find all the prime numbers less than or equal to a given integer by Eratosthenes' method
The sieve of Eratosthenes is one of the most efficient ways to find all of the smaller primes (below 10 million or so). It is named after Eratosthenes of Cyrene, a Greek mathematician.
Here I am implementing his algorithm in C language to find all the prime numbers less than or equal to a given integer.
To check the algorithm refer: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
PROGRAM WITH COMMENTS
main(){
int l,i,temp=0,p,t;
printf("Enter the limit:");
/*
GET the limit 'l'
*/
scanf("%d",&l);
/*
Create an array store a list of consecutive integers from 2 to l
*/
int nlist[l];
/*
Store the consecutive integers from 2 to l
*/
for(i=2;i<=l;i++){
nlist[temp]=i;
temp++;
}
/*
Initially, let p equal 2, the first prime number.
Starting from p, count up in increments of p and mark (here I put 0 for marking) each of these
numbers greater than p itself in the list.
Find the first number greater than p in the list that is not marked. If there was no such number, stop.
Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.
When the algorithm terminates, all the numbers in the list that are not marked are prime.
*/
for(i=0;i<temp;i++){
if(nlist[i]!=0){
t=i;
p=nlist[i];
while((t+p)<temp){
t=t+p;
nlist[t]=0;
}
}
}
/*Printing Prime numbers*/
for(i=0;i<temp;i++){
if(nlist[i]!=0)
printf("%d, ",nlist[i]);
}
}
PROGRAM WITHOUT COMMENTS
main(){
int l,i,temp=0,p,t;
printf("Enter the limit:");
scanf("%d",&l);
int nlist[l];
for(i=2;i<=l;i++){
nlist[temp]=i;
temp++;
}
for(i=0;i<temp;i++){
if(nlist[i]!=0){
t=i;
p=nlist[i];
while((t+p)<temp){
t=t+p;
nlist[t]=0;
}
}
}
for(i=0;i<temp;i++){
if(nlist[i]!=0)
printf("%d, ",nlist[i]);
}
}
Here I am implementing his algorithm in C language to find all the prime numbers less than or equal to a given integer.
To check the algorithm refer: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
PROGRAM WITH COMMENTS
main(){
int l,i,temp=0,p,t;
printf("Enter the limit:");
/*
GET the limit 'l'
*/
scanf("%d",&l);
/*
Create an array store a list of consecutive integers from 2 to l
*/
int nlist[l];
/*
Store the consecutive integers from 2 to l
*/
for(i=2;i<=l;i++){
nlist[temp]=i;
temp++;
}
/*
Initially, let p equal 2, the first prime number.
Starting from p, count up in increments of p and mark (here I put 0 for marking) each of these
numbers greater than p itself in the list.
Find the first number greater than p in the list that is not marked. If there was no such number, stop.
Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.
When the algorithm terminates, all the numbers in the list that are not marked are prime.
*/
for(i=0;i<temp;i++){
if(nlist[i]!=0){
t=i;
p=nlist[i];
while((t+p)<temp){
t=t+p;
nlist[t]=0;
}
}
}
/*Printing Prime numbers*/
for(i=0;i<temp;i++){
if(nlist[i]!=0)
printf("%d, ",nlist[i]);
}
}
PROGRAM WITHOUT COMMENTS
main(){
int l,i,temp=0,p,t;
printf("Enter the limit:");
scanf("%d",&l);
int nlist[l];
for(i=2;i<=l;i++){
nlist[temp]=i;
temp++;
}
for(i=0;i<temp;i++){
if(nlist[i]!=0){
t=i;
p=nlist[i];
while((t+p)<temp){
t=t+p;
nlist[t]=0;
}
}
}
for(i=0;i<temp;i++){
if(nlist[i]!=0)
printf("%d, ",nlist[i]);
}
}
Sunday, December 23, 2012
Preprocessor
Before compiling a C file, a process called preprocessing is done on the source code by a program called preprocessor.
Consider the C code below:
#include <stdio.h>
void main()
{
printf("Hello World");
}
In the above program the program starts with the line #include <stdio.h>. This line is a preprocessor directive. The preprocessor processes the program before the compiler. All lines beginning with # symbol will be processed by the preprocessor. The #include directive causes the preprocessor to effectively insert the stdio.h file into the C program. When the compiler compiles the program it will see the contents of the stdio.h file instead of the preprocessor directive.
Note: The stdio.h file does not contain the actual statement of the printf function. It only has the information that the function printf exists and can accept a character string.
Consider the C code below:
#include <stdio.h>
void main()
{
printf("Hello World");
}
In the above program the program starts with the line #include <stdio.h>. This line is a preprocessor directive. The preprocessor processes the program before the compiler. All lines beginning with # symbol will be processed by the preprocessor. The #include directive causes the preprocessor to effectively insert the stdio.h file into the C program. When the compiler compiles the program it will see the contents of the stdio.h file instead of the preprocessor directive.
Note: The stdio.h file does not contain the actual statement of the printf function. It only has the information that the function printf exists and can accept a character string.
Subscribe to:
Posts (Atom)