SlideShare une entreprise Scribd logo
1  sur  10
1
Assignment #4
Subject: Programming Fundamentals
Semester: 1st
Submitted To: Sir. Junaid
Submitted By: Zohaib Zeeshan
Roll No: BSSE-F17-57
Date: 22/01/2018
Department of CS&IT
(BSSE)
University Of Sargodha Mandi Bahauddin Campus
2
Programming Fundamentals
(1). Printing text on screen:
a). Write a simple C program.
Code:
#include<stdio.h>
int main(void)
{
printf(“Welcome to C!”);
return 0;
}
Output:
b). Write a program to find sum of two integers.
Code:
#include<stdio.h>
int main()
{
int a=5;
int b=7;
int sum;
sum = a+b;
printf("The sum is = %d", sum);
}
Output:
(2). Write two code examples of if-else.
a). Find maximum between two numbers.
Code:
#include<stdio.h>
int main()
{
int num1, num2;
printf("Enter two integers to find which is maximumn");
scanf("%d%d", &num1, &num2);
if(num1 > num2){
printf("First numbers is maximumn");
}
else
{
printf("Second is maximumn");
}
return 0;
}
3
Output:
b). Write a program to check an integer is evenor odd.
Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter an integer to check even or oddn");
scanf("%d", &num);
if(num % 2 == 0)
{
printf("Evenn");
}
else
{
printf("Oddn");
}
}
Output:
(3). Write two code examples of switch statement.
a). Write a code to check an alphabet is vowel or consonant.
Code:
#include<stdio.h>
int main ()
{
char ch;
printf("Enter an alphabetn");
scanf("%c", &ch);
switch (ch)
{
case'a':
printf("a is voweln");
break;
case 'e':
printf("e is voweln");
break;
4
case 'i':
printf("i is voweln");
break;
case 'o':
printf("o is voweln");
break;
case 'u':
printf("u is woweln");
break;
case 'A':
printf("A is voweln");
break;
case 'E':
printf("E is voweln");
break;
case 'I':
printf("I is voweln");
break;
case 'O':
printf("O is voweln");
break;
case 'U':
printf("U is voweln");
break;
default:
printf("is consonant");
}
return 0;
}
Output:
b). Write a code to check number is evenor odd.
Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter a number to check even or oddn");
scanf("%d", &num);
switch(num % 2){
5
case 0:
printf("Number is Evenn");
break;
case 1:
printf("Number is Oddn");
break;
}
}
Output:
(4). Write two code examples of For Loop.
a). C program to find power ofa number using for loop.
Code:
#include<stdio.h>
int main(){
int base,exponent;
int power = 1;
int i;
printf("Enter base: n");
scanf("%d", &base);
printf("Enter exponenet: n");
scanf("%d", &exponent);
for(i=1; i<=exponent; i++){
power=power*base;
}
printf("%d ^ %d = %d", base, exponent, power);
return 0;
}
Output:
b). C program to print all even numbers from 1 to n.
Code:
#include<stdio.h>
int main(){
int i,n;
printf("Print all even numbers: n");
scanf("%d", &n);
printf("Even numbers from 1 to %d are n", n);
6
for(i=1; i<=n; i++){
if(i%2 == 0){
printf("%d ", i);
}
}
return 0;
}
Output:
(5). Write two code examples using while loop.
a). C program to print multiplication table ofa number using while loop.
Code:
#include <stdio.h>
int main()
{
int i, num;
printf("Enter number to print table: ");
scanf("%d", &num);
while(i <=10)
{
printf("%d * %d = %dn", num, i, (num*i));
i++;
}
return 0;
}
Output:
b). Write a program to genrate star pattern as shown below using while loop.
Code:
#include<stdio.h>
int main()
{
int i,j;
7
i=1;
while(i<=5){
printf("");
j=1;
while(j<=i)
{
printf("*");
j++;
}
printf("n");
i++;
}
return 0;
}
Output:
(6).Write two code examples using do while loop.
a). Value of a using do while loop.
Code:
#include <stdio.h>
int main(){
int a = 0;
// do loop execution
do {
printf("value of a: %dn", a);
a++;
}
while( a <= 5 );
return 0;
}
Output:
b). C program to print the table of 5 from 1 to 10.
Code:
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("5 * %d = %dn",i,5*i);
8
i++;
}
while(i<=10);
return 0;
}
Output:
(7). Write two code examples of Functions.
a). C program to find cube ofa number using function.
Code:
#include <stdio.h>
/* Function declaration */
int cube(int num);
int main(){
int num;
int c;
printf("Enter any number: ");
scanf("%d", &num);
c = cube(num);
printf("Cube of %d is %d", num, c);
return 0;
}
int cube(int num)
{
return (num * num * num);
}
Output:
b). Find factorial of a number using function.
Code:
#include<stdio.h>
int factorial(int);
int main(){
int fact;
int numbr;
printf("Enter a number: ");
scanf("%d",&numbr);
9
fact= factorial(numbr);
printf("Factorial of %d is: %d",numbr,fact);
return 0;
}
int factorial(int n){
int i;
int factorial;
factorial =1;
for(i=1;i<=n;i++)
factorial=factorial*i;
return(factorial);
}
Output:
(8). Write two code examples of Array.
a). Write a program to find repeated elements using array.
Code:
#include<stdio.h>
int main(){
int i,arr[20],j,num;
printf("Enter size of array: ");
scanf("%d",&num);
printf("Enter any %d elements in array: ",num);
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
printf("Repeated elements are: n");
for(i=0; i<num; i++)
{
for(j=i+1;j<num;j++)
{
if(arr[i]==arr[j])
{
printf("%dn",arr[i]);
}
}
}
return 0;
}
Output:
10
b). Find largest element using array.
Code:
#include <stdio.h>
int main()
{
int array[50], size, i, largest;
printf("Enter the size of the array: n");
scanf("%d", &size);
printf("Enter %d elements of the array: n", size);
for(i=0; i<size; i++){
scanf("%d", &array[i]);}
largest = array[0];
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("The largest element is : %dn", largest);
return 0;
}
Output:

Contenu connexe

Tendances

Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
emailharmeet
 
C programs
C programsC programs
C programs
Minu S
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
alish sha
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
srinath v
 

Tendances (20)

Practical no 6
Practical no 6Practical no 6
Practical no 6
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
3. user input and some basic problem
3. user input and some basic problem3. user input and some basic problem
3. user input and some basic problem
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Programming egs
Programming egs Programming egs
Programming egs
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
C important questions
C important questionsC important questions
C important questions
 
C Programming
C ProgrammingC Programming
C Programming
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
C programs
C programsC programs
C programs
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
 

Similaire à Programming fundamentals

Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 

Similaire à Programming fundamentals (20)

C lab
C labC lab
C lab
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
C Programming
C ProgrammingC Programming
C Programming
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Itp practical file_1-year
Itp practical file_1-yearItp practical file_1-year
Itp practical file_1-year
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
 
C file
C fileC file
C file
 
Progr3
Progr3Progr3
Progr3
 
C
CC
C
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docx
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C Language Programs
C Language Programs C Language Programs
C Language Programs
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 
comp2
comp2comp2
comp2
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
C programs
C programsC programs
C programs
 

Plus de Zaibi Gondal (8)

Modal Verbs
Modal VerbsModal Verbs
Modal Verbs
 
Parts of speech1
Parts of speech1Parts of speech1
Parts of speech1
 
Wirless Security By Zohaib Zeeshan
Wirless Security By Zohaib ZeeshanWirless Security By Zohaib Zeeshan
Wirless Security By Zohaib Zeeshan
 
C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer record
 
Backup data
Backup data Backup data
Backup data
 
Functional english
Functional englishFunctional english
Functional english
 
application of electronics in computer
application of electronics in computerapplication of electronics in computer
application of electronics in computer
 
Model Verbs
Model VerbsModel Verbs
Model Verbs
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

Programming fundamentals

  • 1. 1 Assignment #4 Subject: Programming Fundamentals Semester: 1st Submitted To: Sir. Junaid Submitted By: Zohaib Zeeshan Roll No: BSSE-F17-57 Date: 22/01/2018 Department of CS&IT (BSSE) University Of Sargodha Mandi Bahauddin Campus
  • 2. 2 Programming Fundamentals (1). Printing text on screen: a). Write a simple C program. Code: #include<stdio.h> int main(void) { printf(“Welcome to C!”); return 0; } Output: b). Write a program to find sum of two integers. Code: #include<stdio.h> int main() { int a=5; int b=7; int sum; sum = a+b; printf("The sum is = %d", sum); } Output: (2). Write two code examples of if-else. a). Find maximum between two numbers. Code: #include<stdio.h> int main() { int num1, num2; printf("Enter two integers to find which is maximumn"); scanf("%d%d", &num1, &num2); if(num1 > num2){ printf("First numbers is maximumn"); } else { printf("Second is maximumn"); } return 0; }
  • 3. 3 Output: b). Write a program to check an integer is evenor odd. Code: #include<stdio.h> int main() { int num; printf("Enter an integer to check even or oddn"); scanf("%d", &num); if(num % 2 == 0) { printf("Evenn"); } else { printf("Oddn"); } } Output: (3). Write two code examples of switch statement. a). Write a code to check an alphabet is vowel or consonant. Code: #include<stdio.h> int main () { char ch; printf("Enter an alphabetn"); scanf("%c", &ch); switch (ch) { case'a': printf("a is voweln"); break; case 'e': printf("e is voweln"); break;
  • 4. 4 case 'i': printf("i is voweln"); break; case 'o': printf("o is voweln"); break; case 'u': printf("u is woweln"); break; case 'A': printf("A is voweln"); break; case 'E': printf("E is voweln"); break; case 'I': printf("I is voweln"); break; case 'O': printf("O is voweln"); break; case 'U': printf("U is voweln"); break; default: printf("is consonant"); } return 0; } Output: b). Write a code to check number is evenor odd. Code: #include<stdio.h> int main() { int num; printf("Enter a number to check even or oddn"); scanf("%d", &num); switch(num % 2){
  • 5. 5 case 0: printf("Number is Evenn"); break; case 1: printf("Number is Oddn"); break; } } Output: (4). Write two code examples of For Loop. a). C program to find power ofa number using for loop. Code: #include<stdio.h> int main(){ int base,exponent; int power = 1; int i; printf("Enter base: n"); scanf("%d", &base); printf("Enter exponenet: n"); scanf("%d", &exponent); for(i=1; i<=exponent; i++){ power=power*base; } printf("%d ^ %d = %d", base, exponent, power); return 0; } Output: b). C program to print all even numbers from 1 to n. Code: #include<stdio.h> int main(){ int i,n; printf("Print all even numbers: n"); scanf("%d", &n); printf("Even numbers from 1 to %d are n", n);
  • 6. 6 for(i=1; i<=n; i++){ if(i%2 == 0){ printf("%d ", i); } } return 0; } Output: (5). Write two code examples using while loop. a). C program to print multiplication table ofa number using while loop. Code: #include <stdio.h> int main() { int i, num; printf("Enter number to print table: "); scanf("%d", &num); while(i <=10) { printf("%d * %d = %dn", num, i, (num*i)); i++; } return 0; } Output: b). Write a program to genrate star pattern as shown below using while loop. Code: #include<stdio.h> int main() { int i,j;
  • 7. 7 i=1; while(i<=5){ printf(""); j=1; while(j<=i) { printf("*"); j++; } printf("n"); i++; } return 0; } Output: (6).Write two code examples using do while loop. a). Value of a using do while loop. Code: #include <stdio.h> int main(){ int a = 0; // do loop execution do { printf("value of a: %dn", a); a++; } while( a <= 5 ); return 0; } Output: b). C program to print the table of 5 from 1 to 10. Code: #include<stdio.h> int main() { int i=1; do { printf("5 * %d = %dn",i,5*i);
  • 8. 8 i++; } while(i<=10); return 0; } Output: (7). Write two code examples of Functions. a). C program to find cube ofa number using function. Code: #include <stdio.h> /* Function declaration */ int cube(int num); int main(){ int num; int c; printf("Enter any number: "); scanf("%d", &num); c = cube(num); printf("Cube of %d is %d", num, c); return 0; } int cube(int num) { return (num * num * num); } Output: b). Find factorial of a number using function. Code: #include<stdio.h> int factorial(int); int main(){ int fact; int numbr; printf("Enter a number: "); scanf("%d",&numbr);
  • 9. 9 fact= factorial(numbr); printf("Factorial of %d is: %d",numbr,fact); return 0; } int factorial(int n){ int i; int factorial; factorial =1; for(i=1;i<=n;i++) factorial=factorial*i; return(factorial); } Output: (8). Write two code examples of Array. a). Write a program to find repeated elements using array. Code: #include<stdio.h> int main(){ int i,arr[20],j,num; printf("Enter size of array: "); scanf("%d",&num); printf("Enter any %d elements in array: ",num); for(i=0;i<num;i++) { scanf("%d",&arr[i]); } printf("Repeated elements are: n"); for(i=0; i<num; i++) { for(j=i+1;j<num;j++) { if(arr[i]==arr[j]) { printf("%dn",arr[i]); } } } return 0; } Output:
  • 10. 10 b). Find largest element using array. Code: #include <stdio.h> int main() { int array[50], size, i, largest; printf("Enter the size of the array: n"); scanf("%d", &size); printf("Enter %d elements of the array: n", size); for(i=0; i<size; i++){ scanf("%d", &array[i]);} largest = array[0]; for (i = 1; i < size; i++) { if (largest < array[i]) largest = array[i]; } printf("The largest element is : %dn", largest); return 0; } Output: